結果
問題 | No.1300 Sum of Inversions |
ユーザー | 🍮かんプリン |
提出日時 | 2020-11-28 16:20:51 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 649 ms / 2,000 ms |
コード長 | 9,124 bytes |
コンパイル時間 | 1,676 ms |
コンパイル使用メモリ | 139,100 KB |
実行使用メモリ | 22,180 KB |
最終ジャッジ日時 | 2024-09-12 23:09:58 |
合計ジャッジ時間 | 17,473 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,944 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 468 ms
19,824 KB |
testcase_04 | AC | 441 ms
19,680 KB |
testcase_05 | AC | 353 ms
14,080 KB |
testcase_06 | AC | 548 ms
20,840 KB |
testcase_07 | AC | 504 ms
20,552 KB |
testcase_08 | AC | 582 ms
18,484 KB |
testcase_09 | AC | 568 ms
21,340 KB |
testcase_10 | AC | 281 ms
12,928 KB |
testcase_11 | AC | 283 ms
11,584 KB |
testcase_12 | AC | 460 ms
16,628 KB |
testcase_13 | AC | 452 ms
19,452 KB |
testcase_14 | AC | 649 ms
22,180 KB |
testcase_15 | AC | 588 ms
21,296 KB |
testcase_16 | AC | 479 ms
20,056 KB |
testcase_17 | AC | 271 ms
12,800 KB |
testcase_18 | AC | 326 ms
12,336 KB |
testcase_19 | AC | 399 ms
18,804 KB |
testcase_20 | AC | 405 ms
18,872 KB |
testcase_21 | AC | 401 ms
18,956 KB |
testcase_22 | AC | 363 ms
14,080 KB |
testcase_23 | AC | 558 ms
20,848 KB |
testcase_24 | AC | 367 ms
13,492 KB |
testcase_25 | AC | 302 ms
13,568 KB |
testcase_26 | AC | 305 ms
13,312 KB |
testcase_27 | AC | 336 ms
13,952 KB |
testcase_28 | AC | 623 ms
21,616 KB |
testcase_29 | AC | 405 ms
18,808 KB |
testcase_30 | AC | 587 ms
18,584 KB |
testcase_31 | AC | 357 ms
14,744 KB |
testcase_32 | AC | 358 ms
14,464 KB |
testcase_33 | AC | 48 ms
7,936 KB |
testcase_34 | AC | 106 ms
7,936 KB |
testcase_35 | AC | 396 ms
16,060 KB |
testcase_36 | AC | 414 ms
16,076 KB |
コンパイルメッセージ
main.cpp: In function 'int compression(std::vector<_Tp>&)': main.cpp:285:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions] 285 | for(auto [k,_] : mp) mp[k] = now++; | ^
ソースコード
#pragma region include #include <iostream> #include <iomanip> #include <stdio.h> #include <vector> #include <algorithm> #include <cmath> #include <map> #include <set> #include <string> #include <bitset> #include <complex> #include <deque> #include <functional> #include <limits> #include <list> #include <numeric> #include <queue> #include <stack> #include <utility> #include <array> #include <random> #include <tuple> #include <unordered_map> #include <unordered_set> #include <assert.h> using namespace std; typedef long long ll; #define dump(x) // // cerr << #x << " = " << (x) << " [" << __LINE__ << ":" << __FUNCTION__ << "] " << endl; // vector出力 template < typename T > ostream& operator<<(ostream& os, vector< T >& v) { os << "{"; for (int i = 0; i < (int)v.size(); i++) { os << v[i] << (i < v.size() - 1 ? ", " : ""); } os << "}"; return os; } // pair出力 template < typename T, typename U > ostream& operator<<(ostream& os, const pair< T, U >& p) { return os << "(" << p.first << ", " << p.second << ")"; } // map出力 template < typename T, typename U > ostream& operator<<(ostream& os, const map< T, U >& map_var) { os << "{"; for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << "(" << itr->first << ", " << itr->second << ")"; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } // set 出力 template < typename T > ostream& operator<<(ostream& os, const set< T >& set_var) { os << "{"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr; ++itr; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } // multiset 出力 template < typename T > ostream& operator<<(ostream& os, const multiset< T >& set_var) { os << "{"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr; ++itr; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } #pragma endregion ///////////////////////////////////////// template< int MOD > struct mint { public: long long x; mint(long long x = 0) :x((x%MOD+MOD)%MOD) {} mint(std::string &s) { long long z = 0; for (int i = 0; i < s.size(); i++) { z *= 10; z += s[i] - '0'; z %= MOD; } this->x = z; } 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) { long long n = MOD - 2; mint u = 1, b = a; while (n > 0) { if (n & 1) { u *= b; } b *= b; n >>= 1; } return *this *= u; } mint operator+(const mint &a) const { mint res(*this); return res += a; } mint operator-() const {return mint() -= *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; } friend std::ostream& operator<<(std::ostream &os, const mint &n) { return os << n.x; } friend std::istream &operator>>(std::istream &is, mint &n) { long long x; is >> x; n = mint(x); return is; } bool operator==(const mint &a) const { return this->x == a.x; } bool operator!=(const mint &a) const { return this->x != a.x; } mint pow(long long k) const { mint ret = 1; mint p = this->x; while (k > 0) { if (k & 1) { ret *= p; } p *= p; k >>= 1; } return ret; } }; constexpr int MOD = 998244353; // verify : https://onlinejudge.u-aizu.ac.jp/problems/DSL_2_A template < class Monoid > struct SegmentTree { private: using Func = std::function< Monoid(Monoid, Monoid) >; Func F; Monoid UNITY; int n; std::vector< Monoid > node; int _binary_search(int a, int b, const std::function<bool(Monoid)> &f, Monoid &s, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; if (r <= a || b <= l) return n; if (a <= l && r <= b && !f(F(s,node[k]))) { s = F(s,node[k]); return n; } if (l == r - 1) {s = F(s,node[k]); return l;} int ret = _binary_search(a, b, f, s, 2 * k + 1, l, (r - l) / 2 + l); return ret != n ? ret : _binary_search(a, b, f, s, 2 * k + 2, (r - l) / 2 + l, r); } public: SegmentTree() {} SegmentTree(const std::vector< Monoid > &v, const Func f, const Monoid &unity) { F = f; UNITY = unity; int sz = v.size(); n = 1; while (n < sz) n <<= 1; node.resize(n * 2 - 1, UNITY); for (int i = 0; i < sz; i++) node[i + n - 1] = v[i]; build(); } // val で埋める SegmentTree(int m, const Monoid &val, const Func f, const Monoid &unity) { F = f; UNITY = unity; n = 1; while (n < m) n <<= 1; node.resize(n * 2 - 1, UNITY); if (val != UNITY) { for (int i = 0; i < m; i++) node[i + n - 1] = val; build(); } } // 最後に build が必要 void set(int k, const Monoid &x) { node[n + k - 1] = x; } void build() { for (int i = n - 2; i >= 0; i--) node[i] = F(node[2 * i + 1], node[2 * i + 2]); } void update_query(int x, const Monoid &val) { if (x >= n || x < 0) return; x += n - 1; node[x] = val; while (x > 0) { x = (x - 1) >> 1; node[x] = F(node[2 * x + 1], node[2 * x + 2]); } } // [l,r) Monoid get_query(int l, int r) { Monoid L = UNITY, R = UNITY; if (l < 0) l = 0; if (r < 0) return UNITY; if (l >= n) return UNITY; if (r >= n) r = n; for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) L = F(L, node[l++ - 1]); if (r & 1) R = F(node[--r - 1], R); } return F(L, R); } // [l,r)を満たすkでf(get_query(l,k+1))=trueとなる最小のk // 無ければ-1 int binary_search(int l, int r, const std::function<bool(Monoid)> &f) { Monoid s = UNITY; int ret = _binary_search(l,r,f,s); return ret != n ? ret : -1; } Monoid operator[](int x) const { return node[n + x - 1]; } int size() { return n; } void print() { for (int i = 0; i < n; i++) { std::cout << i << "\t: " << node[n + i - 1] << std::endl; } } }; // 数列の座標圧縮 要素の最大値を返す template<typename T> int compression(vector<T> &v) { map<T,T> mp; for (auto &val : v) mp[val] = 0; int now = 0; for(auto [k,_] : mp) mp[k] = now++; for (auto &val : v) val = mp[val]; return now; } int main() { int n;cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } auto b = a; int max_e = compression(b); mint<MOD> ans = 0; // 左 { SegmentTree<mint<MOD>> seg1(max_e,0,[](mint<MOD> a, mint<MOD> b){return a+b;},0); SegmentTree<mint<MOD>> seg2(max_e,0,[](mint<MOD> a, mint<MOD> b){return a+b;},0); for (int i = n-1; i >= 0; i--) { seg1.update_query(b[i],seg1[b[i]] + 1); seg2.update_query(b[i],seg2[b[i]]+seg1.get_query(0,b[i])); ans += seg2.get_query(0,b[i]) * a[i]; } } // 右 { SegmentTree<mint<MOD>> seg1(max_e,0,[](mint<MOD> a, mint<MOD> b){return a+b;},0); SegmentTree<mint<MOD>> seg2(max_e,0,[](mint<MOD> a, mint<MOD> b){return a+b;},0); for (int i = 0; i < n; i++) { seg1.update_query(b[i],seg1[b[i]] + 1); seg2.update_query(b[i],seg2[b[i]]+seg1.get_query(b[i]+1,max_e)); ans += seg2.get_query(b[i]+1,max_e) * a[i]; } } // 中 { SegmentTree<mint<MOD>> seg1(max_e,0,[](mint<MOD> a, mint<MOD> b){return a+b;},0); SegmentTree<mint<MOD>> seg2(max_e,0,[](mint<MOD> a, mint<MOD> b){return a+b;},0); vector<mint<MOD>> lef(n), rig(n); for (int i = 0; i < n; i++) { seg1.update_query(b[i],seg1[b[i]] + 1); lef[i] = seg1.get_query(b[i]+1,max_e); } for (int i = n-1; i >= 0; i--) { seg2.update_query(b[i],seg2[b[i]] + 1); rig[i] = seg2.get_query(0,b[i]); } for (int i = 0; i < n; i++) { ans += lef[i] * rig[i] * a[i]; } } cout << ans << endl; return 0; } /////////////////////////////////////////