結果

問題 No.789 範囲の合計
ユーザー bal4ubal4u
提出日時 2019-05-04 20:54:04
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 57 ms / 1,000 ms
コード長 1,578 bytes
コンパイル時間 744 ms
コンパイル使用メモリ 30,188 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-05 11:47:33
合計ジャッジ時間 1,880 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 0 ms
4,380 KB
testcase_02 AC 50 ms
4,376 KB
testcase_03 AC 29 ms
4,376 KB
testcase_04 AC 48 ms
4,376 KB
testcase_05 AC 33 ms
4,376 KB
testcase_06 AC 37 ms
4,376 KB
testcase_07 AC 23 ms
4,376 KB
testcase_08 AC 31 ms
4,376 KB
testcase_09 AC 28 ms
4,380 KB
testcase_10 AC 57 ms
4,376 KB
testcase_11 AC 40 ms
4,380 KB
testcase_12 AC 40 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:8:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    8 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:15:24: 備考: in expansion of macro ‘gc’
   15 |         int n = 0, c = gc();
      |                        ^~

ソースコード

diff #

// yukicoder: No.789 範囲の合計
// 2019.5.4 bal4u

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

#if 1
#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)
#else
#define gc() getchar()
#define pc(c) putchar(c)
#endif
int in() {   // 非負整数の入力
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

// Bit木 library
#define MAX 100010
int bit[MAX << 1]; int bn;
int sum(int i) {
	int s = 0;
	while (i > 0) s += bit[i], i -= i & -i;
	return s;
}
void add(int i, int x) {
	while (i <= bn) bit[i] += x, i += i & -i;
}

int cmd[MAX][3], n;
int x[MAX << 1], sz;
int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; }

int uniq(int *a, int n) {
	int i = 0, j;
	a[n] = a[n-1] + 1;
	for (j = 1; j < n; j++) {
		while (a[j] == a[i]) j++;
		a[++i] = a[j];
	}
	if (a[i] < a[n]) i++;
	return i;
}

int bsch(int v, int r) {
	int m, l = 0;
	while (l < r) {
		m = (l+r)/2;
		if (x[m] == v) break;
		if (x[m] < v) l = m+1; else r = m;
	}
	return m;
}

int main()
{
	int i, k, a, b;
	long long ans;
	n = in(), sz = 0;
	for (i = 0; i < n; i++) {
		cmd[i][0] = k = gc() & 1, gc();
		cmd[i][1] = a = in(), cmd[i][2] = b = in();
		if (k) x[sz++] = a, x[sz++] = b;
		else x[sz++] = a;
	}
	qsort(x, sz, sizeof(int), cmp);
	sz = uniq(x, sz);
	
	ans = 0, bn = sz+1;
	for (i = 0; i < n; i++) {
		if (cmd[i][0]) {
			b = bsch(cmd[i][2], sz);
			a = bsch(cmd[i][1], b+1);
			ans += sum(b+1);
			if (a > 0) ans -= sum(a);
		} else {
			a = bsch(cmd[i][1], sz);
			add(a+1, cmd[i][2]);
		}
	}
	printf("%lld\n", ans);
	return 0;
}
0