結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 53 ms
4,376 KB
testcase_03 AC 32 ms
4,380 KB
testcase_04 AC 49 ms
4,376 KB
testcase_05 AC 36 ms
4,380 KB
testcase_06 AC 39 ms
4,380 KB
testcase_07 AC 24 ms
4,376 KB
testcase_08 AC 33 ms
4,380 KB
testcase_09 AC 28 ms
4,376 KB
testcase_10 AC 60 ms
4,376 KB
testcase_11 AC 42 ms
4,376 KB
testcase_12 AC 43 ms
4,380 KB
testcase_13 AC 0 ms
4,376 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, j;
	
	for (i = 0, j = 1; j < n; j++) {
		while (j < n && a[j] == a[i]) j++;
		if (j < n) if (++i != j) a[i] = a[j];
	}
	return i+1;
}

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

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]) {
			a = lower_bound(cmd[i][1]);
			b = lower_bound(cmd[i][2]);
			ans += sum(b+1);
			if (a > 0) ans -= sum(a);
		} else {
			a = lower_bound(cmd[i][1]);
			add(a+1, cmd[i][2]);
		}
	}
	printf("%lld\n", ans);
	return 0;
}
0