結果

問題 No.789 範囲の合計
ユーザー akakimidoriakakimidori
提出日時 2019-05-25 04:47:30
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 148 ms / 1,000 ms
コード長 1,253 bytes
コンパイル時間 751 ms
コンパイル使用メモリ 29,808 KB
実行使用メモリ 33,376 KB
最終ジャッジ日時 2023-10-17 17:40:39
合計ジャッジ時間 3,531 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 132 ms
27,292 KB
testcase_03 AC 44 ms
4,348 KB
testcase_04 AC 148 ms
30,492 KB
testcase_05 AC 108 ms
26,136 KB
testcase_06 AC 111 ms
27,328 KB
testcase_07 AC 38 ms
4,348 KB
testcase_08 AC 115 ms
33,376 KB
testcase_09 AC 105 ms
30,492 KB
testcase_10 AC 112 ms
18,692 KB
testcase_11 AC 87 ms
26,748 KB
testcase_12 AC 87 ms
26,744 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef int32_t i32;
typedef int64_t i64;

typedef struct segment_node {
  struct segment_node *left;
  struct segment_node *right;
  i32 sum;
} node;

node* add (node *t, i32 l, i32 r, i32 x, i32 y) {
  if (t == NULL) {
    t = (node *) calloc (1, sizeof (node));
    *t = (node) {NULL, NULL, 0};
  }
  t->sum += y;
  if (l + 1 == r) {
    return t;
  }
  i32 m = (l + r) / 2;
  if (x < m) {
    t->left = add (t->left, l, m, x, y);
  } else {
    t->right = add (t->right, m, r, x, y);
  }
  return t;
}

i32 query (node *t, i32 l, i32 r, i32 x, i32 y) {
  if (t == NULL || y <= l || r <= x) {
    return 0;
  }
  if (x <= l && r <= y) {
    return t->sum;
  }
  i32 m = (l + r) / 2;
  return query (t->left, l, m, x, y) + query (t->right, m, r, x, y);
}

void run (void) {
  i32 n;
  scanf ("%" SCNi32, &n);
  const i32 l = 0;
  const i32 r = 1000000000 + 1;
  node *s = NULL;
  i64 ans = 0;
  while (n--) {
    i32 t, x, y;
    scanf ("%" SCNi32 "%" SCNi32 "%" SCNi32, &t, &x, &y);
    if (t == 0) {
      s = add (s, l, r, x, y);
    } else {
      ans += query (s, l, r, x, y + 1);
    }
  }
  printf ("%" PRIi64 "\n", ans);
}

int main (void) {
  run();
  return 0;
}
0