結果
| 問題 |
No.789 範囲の合計
|
| コンテスト | |
| ユーザー |
bal4u
|
| 提出日時 | 2019-05-04 20:28:05 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,628 bytes |
| コンパイル時間 | 784 ms |
| コンパイル使用メモリ | 31,232 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-06-23 06:55:32 |
| 合計ジャッジ時間 | 2,324 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 WA * 3 RE * 2 |
コンパイルメッセージ
main.c: In function 'in':
main.c:8:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
8 | #define gc() getchar_unlocked()
| ^~~~~~~~~~~~~~~~
main.c:15:24: note: in expansion of macro 'gc'
15 | int n = 0, c = gc();
| ^~
ソースコード
// 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 bsch(int v, int r) {
int m, l = 0;
while (l+1 < r) {
m = (l+r)/2;
if (x[m] == v) break;
if (x[m] < v) l = m; 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);
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;
}
bal4u