結果

問題 No.121 傾向と対策:門松列(その2)
ユーザー kyunakyuna
提出日時 2019-07-29 18:33:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,122 ms / 5,000 ms
コード長 1,555 bytes
コンパイル時間 1,211 ms
コンパイル使用メモリ 84,368 KB
実行使用メモリ 22,624 KB
最終ジャッジ日時 2023-09-15 12:00:38
合計ジャッジ時間 7,327 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
4,376 KB
testcase_01 AC 46 ms
4,376 KB
testcase_02 AC 5 ms
4,376 KB
testcase_03 AC 497 ms
11,104 KB
testcase_04 AC 1,122 ms
22,624 KB
testcase_05 AC 496 ms
10,820 KB
testcase_06 AC 426 ms
10,776 KB
testcase_07 AC 481 ms
10,724 KB
testcase_08 AC 543 ms
10,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
using namespace std;

template<typename T>
int compressed(vector<T> &a) {
    vector<T> b = a;
    sort(begin(b), end(b)); b.erase(unique(begin(b), end(b)), end(b));
    for (T &ai: a) ai = lower_bound(begin(b), end(b), ai) - begin(b);
    return b.size();
}

template<typename Abel> struct BIT {
    const Abel UNITY_SUM = 0;
    vector<Abel> dat;
    BIT(int n) : dat(n, UNITY_SUM) { }  // [0, n)
    inline void add(int i, Abel x) {
        while (i < dat.size()) { dat[i] += x; i |= i + 1; }
    }
    inline Abel sum(int i) {    // [0, i]
        Abel res = UNITY_SUM;
        while (i >= 0) { res += dat[i]; i = (i & (i + 1)) - 1; }
        return res;
    }
    inline Abel sum(int a, int b) { return sum(b - 1) - sum(a - 1); }   // [a, b)
    /* debug */
    Abel operator[](int i) { return sum(i, i + 1); }
    void dump() {
        for (int i = 0; i < dat.size(); ++i) cout << sum(i, i + 1) << ",";
        cout << endl;
    }
};

int main() {
    int n; cin >> n;
    vector<int> a(n);
    for (int &ai: a) cin >> ai;
    int sz = compressed(a);
    BIT<long long> left(sz), right(sz);
    for (int ai: a) right.add(ai, 1);
    long long ans = 0, same = 0;
    for (int ai: a) {
        ans += left.sum(0, ai) * right.sum(0, ai);
        ans += left.sum(ai + 1, sz) * right.sum(ai + 1, sz);
        ans -= same - left[ai] * right[ai];
        left.add(ai, 1); same += right[ai];
        right.add(ai, -1); same -= left[ai];
    }
    cout << ans << endl;
    return 0;
}
0