結果
問題 | No.1300 Sum of Inversions |
ユーザー | 👑Zack Ni |
提出日時 | 2020-11-30 14:03:06 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 349 ms / 2,000 ms |
コード長 | 2,246 bytes |
コンパイル時間 | 911 ms |
コンパイル使用メモリ | 89,216 KB |
実行使用メモリ | 15,064 KB |
最終ジャッジ日時 | 2024-09-13 02:21:08 |
合計ジャッジ時間 | 10,096 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
ソースコード
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <map> using namespace std; int lsb(int x){ return x & (-x); } typedef long long ll; #define MODX 998244353 class FenwickTree{ public: ll * B; int n; FenwickTree(int n){ this->n = n; this->B = new ll[n+3]; for(int i = 0; i <= n+1; ++i) this->B[i] = 0; } ll getsum(int index){ ll sum = 0; index = index + 1; while(index > 0){ sum = (this->B[index] + sum) % MODX; index -= lsb(index); } return sum; } void update(int index, ll val){ index = index + 1; while(index <= n){ this->B[index] = (this->B[index] + val ) %MODX; index += lsb(index); } } }; ll add(ll x, ll y){ return (x+y)%MODX; } ll mul(ll x, ll y){ return (x*y)%MODX; } void CoordinateCompress(vector<int> u, vector<int> & v){ sort(u.begin(),u.end()); map<int,int> mpitoind; int n = u.size(); for(int i = 0; i< n; ++i) mpitoind[u[i]] = i; for(int i = 0; i < n; ++i) v[i] = mpitoind[v[i]]; } int main() { int n; cin >> n; vector<int> a; vector<int> b; for(int i = 0; i< n; ++i){ int x; cin >> x; a.push_back(x); b.push_back(x); } CoordinateCompress(a,a); FenwickTree * presum = new FenwickTree(n+2); FenwickTree * f = new FenwickTree(n+2); FenwickTree * g = new FenwickTree(n+2); FenwickTree * c = new FenwickTree(n+2); FenwickTree * gp = new FenwickTree(n+2); for(int i = n-1; i >= 0; --i){ ll nf = 1, ng = 0, nc = 0, ngp = 0; if(a[i] - 1 >= 0){ ll frequency = f->getsum(a[i]-1); ll pres = presum->getsum(a[i]-1); ng = add( mul(frequency, b[i]) , pres); nc = frequency; ll frequency2 = c->getsum(a[i]-1); ll pres2 = g->getsum(a[i]-1); ngp = add( mul(frequency2, b[i]) , pres2); } presum->update(a[i], b[i]); f->update(a[i], nf); g->update(a[i], ng); c->update(a[i], nc); gp->update(a[i], ngp); } cout << gp->getsum(n); return 0; }