結果
問題 | No.1300 Sum of Inversions |
ユーザー |
![]() |
提出日時 | 2020-11-02 23:53:59 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 807 ms / 2,000 ms |
コード長 | 2,317 bytes |
コンパイル時間 | 2,282 ms |
コンパイル使用メモリ | 209,768 KB |
最終ジャッジ日時 | 2025-01-15 19:12:41 |
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
ソースコード
#include <bits/stdc++.h>#define rep(i,n) for (int i = 0; i < (n); ++i)using namespace std;using ll = long long;using PL = pair<long long, long long>;const long long mod = 998244353LL;vector<ll> node;vector<ll> nodf;int sz = 1;void init(int n){while (sz < n) sz *= 2;node.resize(2 * sz - 1);nodf.resize(2 * sz - 1);rep(i,2 * sz - 1) {node[i] = 0LL; nodf[i] = 0LL;}}void update(int a, ll b){a += sz - 1;node[a] += b;node[a] %= mod;while (a > 0){a = (a - 1) / 2;node[a] = node[2 * a + 1] + node[2 * a + 2];node[a] %= mod;}}void updatf(int a, ll b){a += sz - 1;nodf[a] += b;nodf[a] %= mod;while (a > 0){a = (a - 1) / 2;nodf[a] = nodf[2 * a + 1] + nodf[2 * a + 2];nodf[a] %= mod;}}ll query1(int a, int b, int k=0, int l=0, int r=sz){if (b <= l || r <= a) return 0LL;if (a <= l && r <= b) return (node[k] % mod);ll vl = query1(a,b,2*k+1,l,(l+r)/2);ll vr = query1(a,b,2*k+2,(l+r)/2,r);return ((vl + vr) % mod);}ll query2(int a, int b, int k=0, int l=0, int r=sz){if (b <= l || r <= a) return 0LL;if (a <= l && r <= b) return (nodf[k] % mod);ll vl = query2(a,b,2*k+1,l,(l+r)/2);ll vr = query2(a,b,2*k+2,(l+r)/2,r);return ((vl + vr) % mod);}int main(){int n; cin >> n;vector<ll> ar(n);rep(i,n) cin >> ar[i];reverse(ar.begin(),ar.end());vector<ll> al = ar;sort(al.begin(),al.end());al.erase(unique(al.begin(),al.end()),al.end());map<ll,int> mp;int si = al.size();rep(i,si) mp[al[i]] = i;init(si);vector<PL> dp(n);rep(i,n){int l = mp[ar[i]];ll a = query1(0,l,0,0,sz);ll b = query2(0,l,0,0,sz);dp[i].first = a;dp[i].second = (b + (ar[i] * a % mod)) % mod;update(l,1LL);updatf(l,ar[i]);}vector<PL> ep(n);init(si);rep(i,n){int l = mp[ar[i]];ll a = query1(0,l,0,0,sz);ll b = query2(0,l,0,0,sz);ep[i].first = a;ep[i].second = (b + (ar[i] * a % mod)) % mod;update(l,dp[i].first);updatf(l,dp[i].second);}ll ans = 0LL;rep(i,n){ans += ep[i].second;ans %= mod;}cout << ans << endl;}