結果

問題 No.728 ギブ and テイク
ユーザー ats5515ats5515
提出日時 2018-08-24 22:05:50
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,966 bytes
コンパイル時間 1,788 ms
コンパイル使用メモリ 86,592 KB
実行使用メモリ 15,672 KB
最終ジャッジ日時 2023-09-05 11:51:40
合計ジャッジ時間 5,719 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
10,408 KB
testcase_01 AC 16 ms
10,324 KB
testcase_02 AC 15 ms
10,636 KB
testcase_03 AC 15 ms
10,468 KB
testcase_04 AC 15 ms
10,824 KB
testcase_05 AC 14 ms
10,600 KB
testcase_06 AC 15 ms
10,660 KB
testcase_07 AC 14 ms
10,412 KB
testcase_08 AC 15 ms
10,396 KB
testcase_09 AC 14 ms
10,440 KB
testcase_10 AC 15 ms
10,480 KB
testcase_11 AC 14 ms
10,612 KB
testcase_12 AC 16 ms
10,496 KB
testcase_13 AC 32 ms
10,880 KB
testcase_14 AC 44 ms
10,708 KB
testcase_15 AC 26 ms
10,576 KB
testcase_16 AC 41 ms
10,748 KB
testcase_17 AC 39 ms
10,724 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 16 ms
11,484 KB
testcase_30 AC 14 ms
10,700 KB
testcase_31 AC 15 ms
10,644 KB
testcase_32 AC 16 ms
11,092 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 = 30;

wavelet<int, _N, D> a;

int ri[300100];
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	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<int, _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)2e9 + 5);
	}
	cout << res << endl;
}
0