結果

問題 No.728 ギブ and テイク
ユーザー ats5515
提出日時 2018-08-24 22:08:46
言語 C++11
(gcc 4.8.5)
結果
AC  
実行時間 469 ms
コード長 3,027 Byte
コンパイル時間 600 ms
使用メモリ 24,176 KB
最終ジャッジ日時 2019-05-04 21:02:47

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
00_sample1.txt AC 24 ms
12,348 KB
00_sample2.txt AC 24 ms
12,348 KB
00_sample3.txt AC 24 ms
12,352 KB
0_small_case1.txt AC 24 ms
12,352 KB
0_small_case2.txt AC 26 ms
12,348 KB
0_small_case3.txt AC 25 ms
12,576 KB
0_small_case4.txt AC 25 ms
12,348 KB
0_small_case5.txt AC 25 ms
12,352 KB
0_small_case6.txt AC 24 ms
12,360 KB
0_small_case7.txt AC 24 ms
12,348 KB
0_small_case8.txt AC 24 ms
12,360 KB
0_small_case9.txt AC 25 ms
12,364 KB
0_small_case10.txt AC 25 ms
12,376 KB
1_middle_case1.txt AC 47 ms
13,072 KB
1_middle_case2.txt AC 62 ms
13,248 KB
1_middle_case3.txt AC 38 ms
12,776 KB
1_middle_case4.txt AC 60 ms
13,304 KB
1_middle_case5.txt AC 54 ms
13,100 KB
2_large_case1.txt AC 323 ms
19,816 KB
2_large_case2.txt AC 343 ms
20,428 KB
2_large_case3.txt AC 408 ms
21,832 KB
2_large_case4.txt AC 365 ms
20,888 KB
2_large_case5.txt AC 146 ms
15,688 KB
2_large_case6.txt AC 101 ms
14,480 KB
2_large_case7.txt AC 266 ms
18,712 KB
2_large_case8.txt AC 270 ms
18,696 KB
2_large_case9.txt AC 142 ms
15,576 KB
2_large_case10.txt AC 469 ms
23,128 KB
3_challenge_case1.txt AC 277 ms
24,176 KB
3_challenge_case2.txt AC 24 ms
12,348 KB
3_challenge_case3.txt AC 25 ms
12,344 KB
3_challenge_case4.txt AC 24 ms
12,344 KB
3_challenge_case5.txt AC 24 ms
12,348 KB
テストケース一括ダウンロード

ソースコード

diff #
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <cstdint>
#include <cstring>
using namespace std;
#define repi(i,a,b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i,n) repi(i,0,n)

template<int N> class FID {
	static const int bucket = 512, block = 16;
	static char popcount[];
	int n, B[N / bucket + 10];
	unsigned short bs[N / block + 10], b[N / block + 10];

public:
	FID() {}
	FID(int n, bool s[]) : n(n) {
		if (!popcount[1]) for (int i = 0; i < (1 << block); i++) popcount[i] = __builtin_popcount(i);

		bs[0] = B[0] = b[0] = 0;
		for (int i = 0; i < n; i++) {
			if (i%block == 0) {
				bs[i / block + 1] = 0;
				if (i%bucket == 0) {
					B[i / bucket + 1] = B[i / bucket];
					b[i / block + 1] = b[i / block] = 0;
				}
				else b[i / block + 1] = b[i / block];
			}
			bs[i / block] |= short(s[i]) << (i%block);
			b[i / block + 1] += s[i];
			B[i / bucket + 1] += s[i];
		}
		if (n%bucket == 0) b[n / block] = 0;
	}

	int count(bool val, int r) { return val ? B[r / bucket] + b[r / block] + popcount[bs[r / block] & ((1 << (r%block)) - 1)] : r - count(1, r); }
	int count(bool val, int l, int r) { return count(val, r) - count(val, l); }
};
template<int N> char FID<N>::popcount[1 << FID<N>::block];

template<class T, int N, int D> class wavelet {
	int n, zs[D];
	FID<N> dat[D];

	int freq_dfs(int d, int l, int r, T val, T a, T b) {
		if (l >= r or val >= b) return 0;
		if (d == D) return a <= val ? r - l : 0;
		T nv = 1LL << (D - d - 1) | val, nnv = ((1ULL << (D - d - 1)) - 1) | nv;
		if (nnv < a) return 0;
		if (a <= val and nnv < b) return r - l;
		int lc = dat[d].count(1, l), rc = dat[d].count(1, r);
		return freq_dfs(d + 1, l - lc, r - rc, val, a, b) + freq_dfs(d + 1, lc + zs[d], rc + zs[d], nv, a, b);
	}
public:
	wavelet() {}
	wavelet(int n, T seq[]) : n(n) {
		T f[N], l[N], r[N];
		bool b[N];
		memcpy(f, seq, sizeof(T)*n);
		for (int d = 0; d < D; d++) {
			int lh = 0, rh = 0;
			for (int i = 0; i < n; i++) {
				bool k = (f[i] >> (D - d - 1)) & 1;
				if (k) r[rh++] = f[i];
				else l[lh++] = f[i];
				b[i] = k;
			}
			dat[d] = FID<N>(n, b);
			zs[d] = lh;
			swap(l, f);
			memcpy(f + lh, r, rh * sizeof(T));
		}
	}
	int freq(int l, int r, T a, T b) { return freq_dfs(0, l, r, 0, a, b); }
};

const int _N = 300100, D = 40;

wavelet<long long, _N, D> a;

#define int long long
int ri[300100];
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cerr << (int)3e9 << endl;
	int N;
	cin >> N;
	vector<int> A(N);
	vector<int> L(N);
	vector<int> R(N);
	int res = 0;
	for (int i = 0; i < N; i++) {
		cin >> A[i];
	}
	for (int i = 0; i < N; i++) {
		cin >> L[i] >> R[i];
	}
	for (int i = 0; i < N; i++) {
		ri[i] = A[i] + R[i];
	}
	a = wavelet<long long, _N, D>(N, ri);
	for (int i = 0; i < N; i++) {
		int le = lower_bound(A.begin(), A.end(), A[i] - L[i]) - A.begin();
		res += a.freq(le, i, A[i], (int)3e9 + 5);
	}
	cout << res << endl;
}
0