結果
問題 | No.1300 Sum of Inversions |
ユーザー | ir5 |
提出日時 | 2024-06-21 13:24:39 |
言語 | C++23(gcc13) (gcc 13.2.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 337 ms / 2,000 ms |
コード長 | 5,463 bytes |
コンパイル時間 | 2,938 ms |
コンパイル使用メモリ | 177,740 KB |
実行使用メモリ | 20,512 KB |
最終ジャッジ日時 | 2024-06-21 13:24:55 |
合計ジャッジ時間 | 13,297 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 229 ms
16,476 KB |
testcase_04 | AC | 255 ms
16,044 KB |
testcase_05 | AC | 180 ms
13,912 KB |
testcase_06 | AC | 276 ms
18,168 KB |
testcase_07 | AC | 257 ms
17,408 KB |
testcase_08 | AC | 296 ms
18,888 KB |
testcase_09 | AC | 299 ms
18,880 KB |
testcase_10 | AC | 154 ms
12,064 KB |
testcase_11 | AC | 151 ms
12,232 KB |
testcase_12 | AC | 223 ms
16,332 KB |
testcase_13 | AC | 225 ms
15,940 KB |
testcase_14 | AC | 337 ms
20,176 KB |
testcase_15 | AC | 282 ms
18,716 KB |
testcase_16 | AC | 245 ms
16,740 KB |
testcase_17 | AC | 145 ms
11,796 KB |
testcase_18 | AC | 164 ms
13,000 KB |
testcase_19 | AC | 222 ms
14,780 KB |
testcase_20 | AC | 198 ms
15,032 KB |
testcase_21 | AC | 196 ms
14,884 KB |
testcase_22 | AC | 186 ms
13,888 KB |
testcase_23 | AC | 273 ms
17,960 KB |
testcase_24 | AC | 217 ms
14,304 KB |
testcase_25 | AC | 162 ms
12,728 KB |
testcase_26 | AC | 154 ms
12,524 KB |
testcase_27 | AC | 181 ms
13,644 KB |
testcase_28 | AC | 304 ms
19,368 KB |
testcase_29 | AC | 204 ms
14,820 KB |
testcase_30 | AC | 295 ms
18,780 KB |
testcase_31 | AC | 180 ms
13,948 KB |
testcase_32 | AC | 184 ms
14,364 KB |
testcase_33 | AC | 178 ms
20,380 KB |
testcase_34 | AC | 233 ms
20,500 KB |
testcase_35 | AC | 218 ms
20,364 KB |
testcase_36 | AC | 227 ms
20,512 KB |
ソースコード
#include <algorithm> #include <cassert> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <iostream> #include <numeric> #include <vector> #include <map> #include <set> #include <queue> #include <functional> #include <iomanip> #include <ranges> using namespace std; using ll = long long; class range {private: struct I{int x;int operator*(){return x;}bool operator!=(I& lhs){return x<lhs.x;}void operator++(){++x;}};I i,n; public:range(int n_):i({0}),n({n_}){}range(int i_,int n_):i({i_}),n({n_}){}I& begin(){return i;}I& end(){return n;}}; template<typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p){ return os << "{" << p.first << ", " << p.second << "}"; } template<typename T> ostream& operator<<(ostream& os, const vector<T>& obj) { os << "{"; for (const auto& e : obj) os << e << ", "; return os << "}"; } template<typename T> ostream& operator<<(ostream& os, const set<T>& obj) { os << "set{"; for (const auto& e : obj) os << e << ", "; return os << "}"; } template<typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& obj) { os << "map{"; for (const auto& e : obj) os << e << ", "; return os << "}"; } template<typename T> void take(vector<T>& vec, int n) { vec.resize(n); for (int i = 0; i < n; ++i) cin >> vec[i]; } #ifdef ONLINE_JUDGE #define dump(expr) ; #else #define dump(expr) { cerr << "\033[33m#L" << __LINE__ << ": " << expr << "\033[39m" << endl; } #endif const ll mod = 998244353; struct mint { ll x; mint(ll x_ = 0) : x((x_ % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint &a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint &a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint &a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint &a) const { mint res(*this); return res += a; } mint operator-(const mint &a) const { mint res(*this); return res -= a; } mint operator*(const mint &a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } mint inv() const { return pow(mod - 2); } mint &operator/=(const mint &a) { return (*this) *= a.inv(); } mint operator/(const mint &a) const { mint res(*this); return res /= a; } friend ostream &operator<<(ostream &os, const mint &m) { os << m.x; return os; } friend istream &operator>>(istream &is, mint &m) { is >> m.x; return is; } }; // point update, range query segment tree // based on https://maspypy.com/segment-tree-%e3%81%ae%e3%81%8a%e5%8b%89%e5%bc%b71 template <typename T> struct SegmentTree { // array (xs[0], ..., xs[n - 1]) int n; vector<T> xs; function<T(T, T)> fun; // fold function (x @ y) T unit; // (unit @ x) = (x @ unit) = x for all x SegmentTree(int n_, function<T(T, T)> fun_, T unit_) : n(n_), xs(2 * n, unit_), fun(fun_), unit(unit_) {} void build(vector<T> seq) { assert((int)seq.size() == n); for (int i : range(n)) xs[n + i] = seq[i]; for (int i = n - 1; i >= 1; --i) xs[i] = fun(xs[i << 1], xs[(i << 1) | 1]); } void setval(int i, T x) { // xs[i] := x i += n; xs[i] = x; while (i > 1) { i >>= 1; xs[i] = xs[i] = fun(xs[i << 1], xs[(i << 1) | 1]); } } T fold(int l, int r) { // xs[l] @ xs[l + 1] @ ... @ xs[r - 1] l += n; r += n; T vl = unit; T vr = unit; while (l < r) { if (l & 1) { vl = fun(vl, xs[l]); l++; } if (r & 1) { r--; vr = fun(xs[r], vr); } l >>= 1; r >>= 1; } return fun(vl, vr); } }; template <typename T> SegmentTree<T> sum_segment_tree_factory(int n) { auto fun = [](T a, T b) -> T { return a + b;}; return SegmentTree<T>(n, fun, 0); } namespace solver { int n; vector<ll> vs; void read() { cin >> n; take(vs, n); } using RetType = mint; RetType run() { vector<pair<ll, int>> vp(n); for (int i : range(n)) vp[i] = {vs[i], i}; ranges::sort(vp); vector<mint> sum1(n), count1(n); { auto seg_sum = sum_segment_tree_factory<mint>(n), seg_count = sum_segment_tree_factory<mint>(n); for (int i = n - 1; i >= 0; --i) { auto p = vp[i]; sum1[p.second] = seg_sum.fold(0, p.second); count1[p.second] = seg_count.fold(0, p.second); seg_sum.setval(p.second, p.first); seg_count.setval(p.second, 1); } } dump(sum1) dump(count1) vector<mint> sum2(n), count2(n); { auto seg_sum = sum_segment_tree_factory<mint>(n), seg_count = sum_segment_tree_factory<mint>(n); for (int i : range(n)) { auto p = vp[i]; sum2[p.second] = seg_sum.fold(p.second, n); count2[p.second] = seg_count.fold(p.second, n); seg_sum.setval(p.second, p.first); seg_count.setval(p.second, 1); } } mint res = 0; for (int j : range(n)) { mint add = sum1[j] * count2[j] + sum2[j] * count1[j] + mint(vs[j]) * count1[j] * count2[j]; res += add; } return res; } } // namespace template <typename F> void run(F f) { if constexpr (std::is_same_v<decltype(f()), void>) f(); else cout << f() << endl; } int main(int argc, char** argv) { cerr << fixed << setprecision(12); cout << fixed << setprecision(12); int testcase = 1; if (argc > 1) testcase = atoi(argv[1]); while (testcase--) { solver::read(); } run(solver::run); }