結果
問題 | No.1300 Sum of Inversions |
ユーザー | ocvret_ |
提出日時 | 2020-11-27 22:59:57 |
言語 | C++14 (gcc 13.2.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 709 ms / 2,000 ms |
コード長 | 2,169 bytes |
コンパイル時間 | 1,983 ms |
コンパイル使用メモリ | 186,240 KB |
実行使用メモリ | 42,032 KB |
最終ジャッジ日時 | 2023-10-09 20:58:46 |
合計ジャッジ時間 | 19,705 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge14 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
4,380 KB |
testcase_01 | AC | 2 ms
4,376 KB |
testcase_02 | AC | 2 ms
4,376 KB |
testcase_03 | AC | 539 ms
38,332 KB |
testcase_04 | AC | 518 ms
38,080 KB |
testcase_05 | AC | 416 ms
24,108 KB |
testcase_06 | AC | 618 ms
39,808 KB |
testcase_07 | AC | 583 ms
39,336 KB |
testcase_08 | AC | 653 ms
40,508 KB |
testcase_09 | AC | 649 ms
40,292 KB |
testcase_10 | AC | 333 ms
22,512 KB |
testcase_11 | AC | 337 ms
22,748 KB |
testcase_12 | AC | 522 ms
38,488 KB |
testcase_13 | AC | 518 ms
38,036 KB |
testcase_14 | AC | 709 ms
41,484 KB |
testcase_15 | AC | 647 ms
40,392 KB |
testcase_16 | AC | 555 ms
38,536 KB |
testcase_17 | AC | 323 ms
22,452 KB |
testcase_18 | AC | 391 ms
23,384 KB |
testcase_19 | AC | 467 ms
37,040 KB |
testcase_20 | AC | 482 ms
37,152 KB |
testcase_21 | AC | 482 ms
37,176 KB |
testcase_22 | AC | 420 ms
23,912 KB |
testcase_23 | AC | 617 ms
39,912 KB |
testcase_24 | AC | 433 ms
24,384 KB |
testcase_25 | AC | 360 ms
23,228 KB |
testcase_26 | AC | 360 ms
23,148 KB |
testcase_27 | AC | 402 ms
23,812 KB |
testcase_28 | AC | 664 ms
40,628 KB |
testcase_29 | AC | 468 ms
37,296 KB |
testcase_30 | AC | 653 ms
40,380 KB |
testcase_31 | AC | 410 ms
24,368 KB |
testcase_32 | AC | 438 ms
24,588 KB |
testcase_33 | AC | 45 ms
4,588 KB |
testcase_34 | AC | 98 ms
4,584 KB |
testcase_35 | AC | 383 ms
42,032 KB |
testcase_36 | AC | 400 ms
41,596 KB |
ソースコード
#include<bits/stdc++.h> // #include<atcoder/all> using namespace std; // using namespace atcoder; using ll = long long; using ull = unsigned long long; using P = pair<int,int>; #define rep(i,n) for(ll i = 0;i < (ll)n;i++) #define ALL(x) (x).begin(),(x).end() #define MOD 1000000007 template<typename T> class Segtree{ public: using F = function<T(T,T)>; int n; F f; T ti; vector<T> dat; Segtree(){}; Segtree(F f,T ti): f(f),ti(ti){} void init(int N){ n = 1; while(n < N)n <<= 1; dat.assign(n << 1,ti); } void build(const vector<T> &v){ int N = v.size(); init(N); rep(i,N)dat[n+i] = v[i]; for(int i = n-1;i >= 0;i--)dat[i] = f(dat[(i << 1)|0],dat[(i << 1)|1]); } void set(int k,T x){ dat[k+=n] = x; while(k >>= 1){ dat[k] = f(dat[(k << 1)|0],dat[(k << 1)|1]); } } T get(int l,int r){ T vl = ti,vr = ti; for(l += n,r += n;l < r;l >>= 1,r >>= 1){ if(l & 1)vl = f(vl,dat[l++]); if(r & 1)vr = f(dat[--r],vr); } return f(vl,vr); } }; const int mod = 998244353; int main(){ int n; cin >> n; vector<int> v(n); vector<int> w(n); rep(i,n)cin >> v[i],w[i] = v[i]; sort(ALL(w)); w.erase(unique(ALL(w)),w.end()); map<int,int> mp; int m = w.size(); rep(i,m)mp[w[i]] = i; auto f = [&](pair<ll,ll> a,pair<ll,ll> b){return pair<ll,ll>{(a.first+b.first)%mod,(a.second+b.second)%mod};}; Segtree<pair<ll,ll>> one(f,{0,0}),two(f,{0,0}),three(f,{0,0}); one.build(vector<pair<ll,ll>>(m,{0,0})); two.build(vector<pair<ll,ll>>(m,{0,0})); three.build(vector<pair<ll,ll>>(m,{0,0})); rep(i,n){ int k = mp[v[i]]; { pair<ll,ll> x = one.get(k,k+1); x.first += v[i];x.second++; x.first %= mod; one.set(k,x); } { pair<ll,ll> x = two.get(k,k+1); pair<ll,ll> y = one.get(k+1,m); two.set(k,{(x.first+y.first+y.second*v[i]%mod)%mod,(x.second+y.second)%mod}); } { pair<ll,ll> x = three.get(k,k+1); pair<ll,ll> y = two.get(k+1,m); three.set(k,{(x.first+y.first+y.second*v[i]%mod)%mod,(x.second+y.second)%mod}); } } cout << three.get(0,m).first << "\n"; return 0; }