結果

問題 No.1300 Sum of Inversions
ユーザー nawawannawawan
提出日時 2020-11-28 04:04:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 411 ms / 2,000 ms
コード長 3,473 bytes
コンパイル時間 994 ms
コンパイル使用メモリ 91,408 KB
実行使用メモリ 22,632 KB
最終ジャッジ日時 2023-10-09 23:27:11
合計ジャッジ時間 11,831 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 308 ms
21,772 KB
testcase_04 AC 293 ms
21,700 KB
testcase_05 AC 237 ms
13,364 KB
testcase_06 AC 350 ms
22,232 KB
testcase_07 AC 336 ms
22,052 KB
testcase_08 AC 377 ms
22,496 KB
testcase_09 AC 376 ms
22,288 KB
testcase_10 AC 187 ms
12,736 KB
testcase_11 AC 191 ms
12,924 KB
testcase_12 AC 297 ms
21,836 KB
testcase_13 AC 290 ms
21,712 KB
testcase_14 AC 411 ms
22,504 KB
testcase_15 AC 362 ms
22,352 KB
testcase_16 AC 320 ms
22,016 KB
testcase_17 AC 173 ms
12,860 KB
testcase_18 AC 206 ms
13,068 KB
testcase_19 AC 256 ms
21,704 KB
testcase_20 AC 259 ms
21,556 KB
testcase_21 AC 262 ms
21,628 KB
testcase_22 AC 223 ms
13,068 KB
testcase_23 AC 350 ms
21,980 KB
testcase_24 AC 238 ms
13,384 KB
testcase_25 AC 199 ms
12,992 KB
testcase_26 AC 196 ms
13,056 KB
testcase_27 AC 228 ms
13,132 KB
testcase_28 AC 380 ms
22,320 KB
testcase_29 AC 246 ms
21,504 KB
testcase_30 AC 367 ms
22,240 KB
testcase_31 AC 234 ms
13,260 KB
testcase_32 AC 248 ms
13,272 KB
testcase_33 AC 23 ms
6,220 KB
testcase_34 AC 37 ms
6,272 KB
testcase_35 AC 206 ms
22,496 KB
testcase_36 AC 212 ms
22,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>
#include <stack>
using namespace std;
const int MOD = 998244353;
struct segK{//非再帰
    int n;
    long long MIN;
    int size;
    vector<long long> dat;
    segK(int n_){
        n = 1;
        MIN = 0;
        size = n_;
        while(n < n_) n *= 2;
        dat.resize(2 * n);
        for(int i = 1; i < 2 * n; i++) dat[i] = MIN;
    }
    void update(int k, long long a){
        k += n;
        dat[k] += a;
        while(k > 0){
            k >>= 1;
            dat[k] = (dat[k << 1 | 0] + dat[k << 1 | 1]) % MOD;
        }
    }
    long long query(int l, int r){
        long long res = 0;
        l += n;
        r += n;
        while(r > l){
            if(l & 1) res = res + dat[l++];
            if(r & 1) res = res + dat[--r];
            l >>= 1;
            r >>= 1;
        }
        return res % MOD;
    }
    int right_bin(int l, long long v) {//l番目の値vより小さくて、l以下で最も大きいインデックスを返す
        if(l == size) return l;
        l += n;
        do {
            while (l % 2 == 0) l >>= 1;
            if (dat[l] >= v) {
                while (l < n) {
                    l = (2 * l);
                    if (dat[l] < v) {
                        l++;
                    }
                }
                return l - n;
            }
            l++;
        } while ((l & -l) != l);
        return size;
    }
    int left_bin(int r, long long v) {
        if(r == 0) return r;
        r += n;
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (dat[r] <= v) {
                while (r < n) {
                    r = (2 * r + 1);
                    if (dat[r] > v) {
                        r--;
                    }
                }
                return r + 1 - n;
            }
        } while ((r & -r) != r);
        return 0;
    }
};
template<class T>
vector<T> press(vector<T> &a){
    vector<T> temp = a;
    sort(temp.begin(), temp.end());
    temp.erase(unique(temp.begin(), temp.end()), temp.end());
    for(int i = 0; i < a.size(); i++){
        a[i] = lower_bound(temp.begin(), temp.end(), a[i]) - temp.begin();
    }
    return temp;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    cin >> N;
    vector<long long> a(N);
    for(int i = 0; i < N; i++) cin >> a[i];
    vector<long long> za = press(a);
    int m = za.size();
    segK seg1(m), seg2(m), seg3(m), seg4(m);
    for(int i = 0; i < N; i++){
        seg3.update(a[i], za[a[i]]);
        seg4.update(a[i], 1);
    }
    long long ans = 0;
    for(int i = 0; i < N; i++){
        if(i == 0 || i == N - 1){
            seg1.update(a[i], za[a[i]]);
            seg2.update(a[i], 1);
            seg3.update(a[i], -za[a[i]]);
            seg4.update(a[i], -1);
        }
        else{
            seg1.update(a[i], za[a[i]]);
            seg2.update(a[i], 1);
            seg3.update(a[i], -za[a[i]]);
            seg4.update(a[i], -1);
            long long s = seg1.query(a[i] + 1, m), s1 = seg2.query(a[i] + 1, m);
            long long t = seg3.query(0, a[i]), t1 = seg4.query(0, a[i]);
            //cout << s << ' ' << t << endl;
            if(t1 != 0 && s1 != 0){
                ans += s * t1 % MOD + t * s1 % MOD + za[a[i]] * t1 % MOD * s1 % MOD;
                ans %= MOD;
            }
        }
    }
    cout << ans << endl;
}
0