結果

問題 No.728 ギブ and テイク
ユーザー bal4ubal4u
提出日時 2019-08-19 11:58:52
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 175 ms / 3,000 ms
コード長 1,531 bytes
コンパイル時間 196 ms
コンパイル使用メモリ 32,384 KB
実行使用メモリ 18,024 KB
最終ジャッジ日時 2024-04-14 06:25:19
合計ジャッジ時間 5,038 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
7,916 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
7,916 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 10 ms
6,944 KB
testcase_14 AC 16 ms
6,944 KB
testcase_15 AC 7 ms
7,964 KB
testcase_16 AC 15 ms
8,120 KB
testcase_17 AC 12 ms
6,940 KB
testcase_18 AC 121 ms
14,348 KB
testcase_19 AC 132 ms
14,868 KB
testcase_20 AC 157 ms
15,592 KB
testcase_21 AC 139 ms
15,600 KB
testcase_22 AC 54 ms
7,880 KB
testcase_23 AC 34 ms
7,028 KB
testcase_24 AC 101 ms
13,128 KB
testcase_25 AC 101 ms
13,424 KB
testcase_26 AC 50 ms
8,572 KB
testcase_27 AC 175 ms
18,024 KB
testcase_28 AC 119 ms
17,400 KB
testcase_29 AC 2 ms
7,912 KB
testcase_30 AC 1 ms
6,944 KB
testcase_31 AC 2 ms
6,940 KB
testcase_32 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'in':
main.c:11:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
   11 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:17:24: note: in expansion of macro 'gc'
   17 |         int n = 0, c = gc();
      |                        ^~

ソースコード

diff #

// yukicoder: No.728 ギブ and テイク
// bal4u 2019.8.19

#include <stdio.h>
#include <stdlib.h>

typedef long long ll;

//// 入出力関係
#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif

int in() { // 非負整数の入力
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
	return n;
}

#define MAX 300005

//// bit木 1-indexedに注意
int bit[MAX], bitn;
void add(int i, int v) { while (i <= bitn) bit[i] += v, i += i & -i; }
void add1(int i) { while (i <= bitn) bit[i]++, i += i & -i; }
int sum(int i) { int s = 0; while (i > 0) s += bit[i], i -= i & -i; return s; }

typedef struct { int v, i, id; } T;
T tbl[2*MAX]; int M;
int A[MAX], L[MAX], R[MAX]; int N;

// a[i]>key という条件を満たす最小のi
int upper_bound(int x) {
	int m, l = 0, r = N;
	while (l+1 < r) {
		m = (l+r) >> 1;
		if (A[m] <= x) l = m; else r = m;
	}
	return A[l] <= x? r: l;
}

int cmp(const void *u, const void *v) {
	int t;
	if (t = ((T *)u)->v - ((T *)v)->v) return t;
	return ((T *)u)->id - ((T *)v)->id;
}

int main()
{
	int i, j;
	ll ans;
	
	N = in();
	for (i = 0; i < N; i++) A[i] = in();
	j = 0; for (i = 0; i < N; i++) {
		L[i] = A[i]-in(), R[i] = A[i]+in();
		tbl[j].i = i, tbl[j].id = 1, tbl[j++].v = A[i];
		tbl[j].i = i, tbl[j++].v = L[i];
	}
	M = N << 1;	qsort(tbl, M, sizeof(T), cmp);
	
	ans = 0, bitn = N;
	for (j = 0; j < M; j++) {
		i = tbl[j].i;
		if (!tbl[j].id) add1(i+1);
		else ans += sum(upper_bound(R[i])) - sum(i+1);
	}
	printf("%lld\n", ans);
	return 0;
}
0