結果

問題 No.1282 Display Elements
ユーザー face4face4
提出日時 2020-12-05 17:42:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 124 ms / 2,000 ms
コード長 1,273 bytes
コンパイル時間 873 ms
コンパイル使用メモリ 78,272 KB
実行使用メモリ 5,492 KB
最終ジャッジ日時 2023-10-13 22:21:46
合計ジャッジ時間 4,075 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,356 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 111 ms
5,444 KB
testcase_16 AC 44 ms
4,352 KB
testcase_17 AC 92 ms
4,880 KB
testcase_18 AC 56 ms
4,452 KB
testcase_19 AC 86 ms
4,788 KB
testcase_20 AC 81 ms
4,804 KB
testcase_21 AC 122 ms
5,440 KB
testcase_22 AC 93 ms
4,856 KB
testcase_23 AC 124 ms
5,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct BIT{
    // 1-origin
    vector<int> v;
    int n;

    BIT(int _n):
        v(vector<int>(_n+1, 0)), n(_n) {}

    int sum(int i){
        int s = 0;
        while(i > 0){
            s += v[i];
            i -= lsb(i);
        }
        return s;
    }
    
    int sum(int l, int r){
        return sum(r) - sum(l-1);
    }

    void add(int i, int x){
        while(i <= n){
            v[i] += x;
            i += lsb(i);
        }
    }

private:
    // least significant bit
    int lsb(int i){
        return i & -i;
    }
};

typedef long long ll;

int main(){
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for(int i = 0; i < n; i++)  cin >> a[i];
    sort(a.begin(), a.end());
    for(int i = 0; i < n; i++)  cin >> b[i];
    vector<int> x;
    for(int i = 0; i < n; i++)  x.push_back(a[i]), x.push_back(b[i]);
    sort(x.begin(), x.end());
    x.erase(unique(x.begin(), x.end()), x.end());
    int m = x.size();
    BIT bit(m);
    ll ans = 0;
    for(int i = 0; i < n; i++){
        bit.add(lower_bound(x.begin(), x.end(), b[i])-x.begin()+1, 1);
        ans += bit.sum(lower_bound(x.begin(), x.end(), a[i])-x.begin());
    }
    cout << ans << endl;
    return 0;
}
0