結果

問題 No.1282 Display Elements
ユーザー ぷら
提出日時 2021-03-13 01:30:22
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 123 ms / 2,000 ms
コード長 1,280 bytes
コンパイル時間 1,831 ms
コンパイル使用メモリ 175,900 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-10-14 15:05:47
合計ジャッジ時間 4,500 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <typename T>
struct BinaryIndexedTree {
    int N;
    vector<T>bit;
    
    BinaryIndexedTree() {};
    
    BinaryIndexedTree(int x) {
        N = x;
        bit.resize(x+1);
    }
    
    void add(int x,T y) {
        while(x <= N) {
            bit[x] += y;
            x += x&-x;
        }
    }
    
    T sum(int x) {
        T res = 0;
        while(x) {
            res += bit[x];
            x -= x&-x;
        }
        return res;
    }
    
    T sum(int l, int r) {//[l,r]
        return sum(r)-sum(l-1);
    }
};

int main() {
    int N;
    cin >> N;
    vector<int>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());
    sort(c.begin(),c.end());
    c.erase(unique(c.begin(),c.end()),c.end());
    BinaryIndexedTree<int>bit(c.size());
    long long ans = 0;
    for(int i = 0; i < N; i++) {
        int it1 = lower_bound(c.begin(),c.end(),a[i])-c.begin();
        int it2 = lower_bound(c.begin(),c.end(),b[i])-c.begin();
        it1++;
        it2++;
        bit.add(it2,1);
        ans += bit.sum(1,it1-1);
    }
    cout << ans << endl;
}
0