結果

問題 No.1282 Display Elements
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2024-04-04 12:24:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 233 ms / 2,000 ms
コード長 1,618 bytes
コンパイル時間 2,463 ms
コンパイル使用メモリ 215,876 KB
実行使用メモリ 20,608 KB
最終ジャッジ日時 2024-04-04 12:24:20
合計ジャッジ時間 6,005 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 1 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 3 ms
6,676 KB
testcase_15 AC 182 ms
19,200 KB
testcase_16 AC 56 ms
9,768 KB
testcase_17 AC 177 ms
16,520 KB
testcase_18 AC 76 ms
11,464 KB
testcase_19 AC 35 ms
8,192 KB
testcase_20 AC 34 ms
8,192 KB
testcase_21 AC 217 ms
20,608 KB
testcase_22 AC 48 ms
8,192 KB
testcase_23 AC 233 ms
20,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

template <typename T>
map<T, T> compress(vector<T> &A){

    map<T, T> comp;
    int N = A.size(), i=0;
    for (int i=0; i<N; i++) comp[A[i]];

    for (auto &e : comp){
        e.second = i;
        i++;
    }

    return comp;
}

template<class T> struct BIT {
    private:
        vector<T> bit; int N;
        T _sum(int r){
            r++; T res = 0;
            while(r > 0) res += bit[r-1], r -= r & -r;
            return res;
        }

    public:
        BIT (int n){ N = n; bit.resize(N);}
        BIT (vector<T> &a) : BIT(a.size()) { for (int i=0; i<N; i++) add(i, a[i]);}
        void add(int loc, T val){
            loc++;
            while(loc <= N) bit[loc-1] += val, loc += loc & -loc;
        }
        void change(int loc, T val){add(loc, -get(loc)+val);}
        T sum(int l, int r) {return _sum(r) - _sum(l-1);}
        T get(int l) {return sum(l, l);}
        void clear() {for (int i=0; i<N; i++) bit[i] = 0;}
};

int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    //大きいものを後に提示する
    ll N, ans=0;
    cin >> N;
    vector<ll> a(N), b(N), c;
    for (int i=0; i<N; i++) cin >> a[i], c.push_back(a[i]);
    for (int i=0; i<N; i++) cin >> b[i], c.push_back(b[i]);
    sort(a.begin(), a.end());
    map<ll, ll> mp = compress(c);
    for (int i=0; i<N; i++){
        a[i] = mp[a[i]];
        b[i] = mp[b[i]];
    }

    BIT<ll> bit(N*2);
    for (int i=0; i<N; i++){
        bit.add(b[i], 1);
        ans += bit.sum(0, a[i]-1);
    }
    cout << ans << endl;

    return 0;
}
0