結果

問題 No.1282 Display Elements
ユーザー 👑 platinumplatinum
提出日時 2020-08-22 18:37:53
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 1,951 bytes
コンパイル時間 1,037 ms
コンパイル使用メモリ 81,668 KB
実行使用メモリ 6,268 KB
最終ジャッジ日時 2023-08-19 13:07:34
合計ジャッジ時間 4,274 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 118 ms
6,028 KB
testcase_16 AC 47 ms
4,560 KB
testcase_17 AC 97 ms
5,536 KB
testcase_18 AC 60 ms
4,564 KB
testcase_19 AC 90 ms
6,224 KB
testcase_20 AC 85 ms
6,268 KB
testcase_21 AC 129 ms
6,016 KB
testcase_22 AC 100 ms
6,156 KB
testcase_23 AC 130 ms
6,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <cassert>
#define rep(i,n) for(int i=0; i<(int)(n); i++)

using namespace std;
using LL = long long;
const int Max_N = 1e5;
const int Max_Fac = 1e9;

class FenwickTree {
private:
    int bit_size;
    vector<LL> bit;

public:
    FenwickTree(int siz): bit_size(siz), bit(siz + 1) {}

    void add(int num, LL val) {
        for(int i = num + 1; i <= bit_size; i += i & -i) {
            bit[i] += val;
        }
    }

    LL sum(int end) {
        LL res = 0;
        for(int i = end + 1; i > 0; i -= i & -i) {
            res += bit[i];
        }
        return res;
    }

    LL sum(int begin, int end) {
        return sum(end - 1) - sum(begin - 1);
    }

    void init(LL val) {
        for(int i = 1; i <= bit_size; i++) {
            add(i, val);
        }
    }

    int lower_bound(LL val) {
        if(val <= 0) return 0;
        int i = 0, n = 1;
        while(n * 2 <= bit_size) n *= 2;
        for(int k = n; k > 0; k /= 2) {
            if(i + k <= bit_size && bit[i + k] < val) {
                val -= bit[i + k];
                i += k;
            }
        }
        return i + 1;
    }
};

int main(){
	int N;
	cin >> N;
	assert(1<=N and N<=Max_N);
	vector<int> a(N), b(N);
	rep(i,N){
		int A;
		cin >> A;
		assert(1<=A and A<=Max_Fac);
		a[i]=A;
	}
	rep(i,N){
		int B;
		cin >> B;
		assert(1<=B and B<=Max_Fac);
		b[i]=B;
	}
	vector<int> c;
	rep(i,N) c.emplace_back(a[i]), c.emplace_back(b[i]);
	sort(c.begin(),c.end());
	c.erase(unique(c.begin(),c.end()),c.end());
	rep(i,N){
		a[i]=lower_bound(c.begin(),c.end(),a[i])-c.begin();
		b[i]=lower_bound(c.begin(),c.end(),b[i])-c.begin();
	}
	sort(a.begin(),a.end());
	LL ans=0;
	FenwickTree ft(2*N);
	rep(i,N){
		ft.add(b[i],1);
		LL res=ft.sum(a[i]-1);
		ans+=res;
	}
	cout << ans << endl;

	return 0;
}
0