結果

問題 No.1282 Display Elements
ユーザー oevloevl
提出日時 2020-11-06 23:09:14
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 126 ms / 2,000 ms
コード長 1,239 bytes
コンパイル時間 2,051 ms
コンパイル使用メモリ 204,904 KB
実行使用メモリ 6,768 KB
最終ジャッジ日時 2023-09-29 19:28:41
合計ジャッジ時間 4,343 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,876 KB
testcase_01 AC 3 ms
4,876 KB
testcase_02 AC 3 ms
4,876 KB
testcase_03 AC 3 ms
4,972 KB
testcase_04 AC 3 ms
4,972 KB
testcase_05 AC 4 ms
4,800 KB
testcase_06 AC 3 ms
4,848 KB
testcase_07 AC 3 ms
4,840 KB
testcase_08 AC 3 ms
4,896 KB
testcase_09 AC 3 ms
4,804 KB
testcase_10 AC 4 ms
4,976 KB
testcase_11 AC 4 ms
5,032 KB
testcase_12 AC 3 ms
4,932 KB
testcase_13 AC 4 ms
4,804 KB
testcase_14 AC 4 ms
4,800 KB
testcase_15 AC 116 ms
6,768 KB
testcase_16 AC 47 ms
5,636 KB
testcase_17 AC 96 ms
6,248 KB
testcase_18 AC 60 ms
5,748 KB
testcase_19 AC 92 ms
6,768 KB
testcase_20 AC 87 ms
6,672 KB
testcase_21 AC 126 ms
6,684 KB
testcase_22 AC 100 ms
6,712 KB
testcase_23 AC 126 ms
6,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<class T> class Fenwick {
private:
    vector<T> dat;
    int n;
public:
    Fenwick(int _n) : dat(_n + 1), n(_n) {}
    // sum of [1, i]
    T sum(int i) {
        T s = 0;
        while(i > 0) s += dat[i], i -= i & -i;
        return s;
    }
    // sum of [l, r]
    T sum(int l, int r) {
        return l > r ? 0 : sum(r) - sum(l - 1);
    }
    void add(int i, T x) {
        while(i <= n) dat[i] += x, i += i & -i;
    }
};

const int m = 5e5;

int main() {
    int n; cin >> n;
    vector<int> a(n), b(n);
    vector<int> c;
    for(auto &e : a) {
        cin >> e;
        c.push_back(e);
    }
    for(auto &e : b) {
        cin >> e;
        c.push_back(e);
    }
    sort(a.begin(), a.end());
    sort(c.begin(), c.end());
    c.erase(unique(c.begin(), c.end()), c.end());
    for(int i = 0; i < n; ++i) {
        a[i] = lower_bound(c.begin(), c.end(), a[i]) - c.begin() + 2;
    }
    for(int i = 0; i < n; ++i) {
        b[i] = lower_bound(c.begin(), c.end(), b[i]) - c.begin() + 2;
    }
    long long ans = 0;
    Fenwick<int> f(m);
    for(int i = 0; i < n; ++i) {
        f.add(b[i], 1);
        ans += f.sum(a[i] - 1);
    }
    cout << ans << '\n';
    return 0;
}
0