結果

問題 No.1282 Display Elements
ユーザー 👑 platinumplatinum
提出日時 2020-08-22 18:37:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 132 ms / 2,000 ms
コード長 1,951 bytes
コンパイル時間 975 ms
コンパイル使用メモリ 82,188 KB
実行使用メモリ 6,620 KB
最終ジャッジ日時 2024-05-06 20:12:22
合計ジャッジ時間 3,703 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 120 ms
6,304 KB
testcase_16 AC 48 ms
5,376 KB
testcase_17 AC 97 ms
5,668 KB
testcase_18 AC 58 ms
5,376 KB
testcase_19 AC 91 ms
6,616 KB
testcase_20 AC 86 ms
6,492 KB
testcase_21 AC 132 ms
6,492 KB
testcase_22 AC 103 ms
6,492 KB
testcase_23 AC 123 ms
6,620 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