結果

問題 No.1300 Sum of Inversions
ユーザー 👑Zack Ni👑Zack Ni
提出日時 2020-11-30 14:03:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 412 ms / 2,000 ms
コード長 2,246 bytes
コンパイル時間 897 ms
コンパイル使用メモリ 88,584 KB
実行使用メモリ 15,004 KB
最終ジャッジ日時 2023-10-11 02:50:19
合計ジャッジ時間 11,423 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 298 ms
11,968 KB
testcase_04 AC 286 ms
11,856 KB
testcase_05 AC 208 ms
10,476 KB
testcase_06 AC 342 ms
12,964 KB
testcase_07 AC 322 ms
12,772 KB
testcase_08 AC 362 ms
13,828 KB
testcase_09 AC 365 ms
13,496 KB
testcase_10 AC 159 ms
9,072 KB
testcase_11 AC 164 ms
9,032 KB
testcase_12 AC 296 ms
11,980 KB
testcase_13 AC 271 ms
11,644 KB
testcase_14 AC 412 ms
14,556 KB
testcase_15 AC 356 ms
13,632 KB
testcase_16 AC 298 ms
12,176 KB
testcase_17 AC 154 ms
8,812 KB
testcase_18 AC 193 ms
9,888 KB
testcase_19 AC 244 ms
10,924 KB
testcase_20 AC 252 ms
11,320 KB
testcase_21 AC 251 ms
11,048 KB
testcase_22 AC 216 ms
10,408 KB
testcase_23 AC 349 ms
13,152 KB
testcase_24 AC 217 ms
10,608 KB
testcase_25 AC 174 ms
9,684 KB
testcase_26 AC 167 ms
9,360 KB
testcase_27 AC 199 ms
10,076 KB
testcase_28 AC 388 ms
13,900 KB
testcase_29 AC 235 ms
10,992 KB
testcase_30 AC 354 ms
13,492 KB
testcase_31 AC 216 ms
10,532 KB
testcase_32 AC 224 ms
10,792 KB
testcase_33 AC 67 ms
12,516 KB
testcase_34 AC 119 ms
12,648 KB
testcase_35 AC 192 ms
14,820 KB
testcase_36 AC 209 ms
15,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0