結果

問題 No.1300 Sum of Inversions
ユーザー nawawannawawan
提出日時 2020-11-27 23:16:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,253 bytes
コンパイル時間 1,458 ms
コンパイル使用メモリ 91,296 KB
実行使用メモリ 21,828 KB
最終ジャッジ日時 2023-10-09 21:08:12
合計ジャッジ時間 8,943 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 113 ms
21,764 KB
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

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];
        }
    }
    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;
    }
    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;
    }
};
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<int> ind(N);
    for(int i = 0; i < N; i++) ind[i] = i;
    sort(ind.begin(), ind.end(), [&](int i, int j){
        return A[i] < A[j];
    });
    segK seg1(N), seg2(N), seg3(N), seg4(N);
    for(int i = 0; i < N; i++) {
        seg2.update(i, A[i]);
        seg4.update(i, 1);
    }
    long long ans = 0;
    for(int i = 0; i < N; i++){
        if(ind[i] == 0 || ind[i] == N - 1) {
            seg1.update(ind[i], A[ind[i]]);
            seg3.update(ind[i], 1);
            seg2.update(ind[i], 0);
            seg4.update(ind[i], 0);
        }
        else{
            seg1.update(ind[i], A[ind[i]]);
            seg2.update(ind[i], 0);
            seg3.update(ind[i], 1);
            seg4.update(ind[i], 0);
            long long s = seg1.query(ind[i] + 1, N);
            long long t = seg2.query(0, ind[i]);
            long long s1 = seg3.query(ind[i] + 1, N);
            long long t1 = seg4.query(0, ind[i]);
            if(s != 0 && t != 0){
                ans += s * t1 % MOD + t * s1 % MOD + A[ind[i]] * (s1 * t1 % MOD) % MOD;
                ans %= MOD;
            }
        }
    }
    cout << ans << endl;
}
0