結果

問題 No.1300 Sum of Inversions
ユーザー noya2noya2
提出日時 2020-11-02 23:53:59
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 768 ms / 2,000 ms
コード長 2,317 bytes
コンパイル時間 3,024 ms
コンパイル使用メモリ 215,500 KB
実行使用メモリ 33,128 KB
最終ジャッジ日時 2023-09-29 14:18:52
合計ジャッジ時間 21,630 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 531 ms
27,912 KB
testcase_04 AC 509 ms
27,928 KB
testcase_05 AC 409 ms
20,728 KB
testcase_06 AC 644 ms
30,004 KB
testcase_07 AC 601 ms
29,472 KB
testcase_08 AC 681 ms
31,180 KB
testcase_09 AC 675 ms
31,188 KB
testcase_10 AC 330 ms
18,396 KB
testcase_11 AC 332 ms
18,712 KB
testcase_12 AC 535 ms
27,764 KB
testcase_13 AC 525 ms
27,392 KB
testcase_14 AC 768 ms
32,764 KB
testcase_15 AC 678 ms
30,920 KB
testcase_16 AC 572 ms
28,556 KB
testcase_17 AC 318 ms
18,060 KB
testcase_18 AC 375 ms
19,648 KB
testcase_19 AC 456 ms
25,868 KB
testcase_20 AC 477 ms
26,128 KB
testcase_21 AC 472 ms
26,064 KB
testcase_22 AC 412 ms
20,648 KB
testcase_23 AC 637 ms
30,076 KB
testcase_24 AC 435 ms
21,352 KB
testcase_25 AC 351 ms
19,244 KB
testcase_26 AC 341 ms
19,144 KB
testcase_27 AC 389 ms
20,444 KB
testcase_28 AC 690 ms
31,720 KB
testcase_29 AC 472 ms
26,240 KB
testcase_30 AC 683 ms
30,888 KB
testcase_31 AC 422 ms
20,920 KB
testcase_32 AC 424 ms
21,252 KB
testcase_33 AC 44 ms
12,580 KB
testcase_34 AC 96 ms
12,400 KB
testcase_35 AC 396 ms
33,116 KB
testcase_36 AC 418 ms
33,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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