結果

問題 No.789 範囲の合計
ユーザー akakimidoriakakimidori
提出日時 2019-02-08 21:44:21
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 154 ms / 1,000 ms
コード長 1,029 bytes
コンパイル時間 900 ms
コンパイル使用メモリ 28,836 KB
実行使用メモリ 33,284 KB
最終ジャッジ日時 2023-09-11 04:10:41
合計ジャッジ時間 2,938 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 143 ms
27,228 KB
testcase_03 AC 57 ms
4,380 KB
testcase_04 AC 154 ms
30,308 KB
testcase_05 AC 114 ms
25,944 KB
testcase_06 AC 119 ms
27,264 KB
testcase_07 AC 52 ms
4,384 KB
testcase_08 AC 125 ms
33,284 KB
testcase_09 AC 113 ms
30,364 KB
testcase_10 AC 116 ms
18,508 KB
testcase_11 AC 98 ms
26,708 KB
testcase_12 AC 99 ms
26,712 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 0 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long int int64;

typedef struct node{
  int sum;
  struct node *l,*r;
} node;

node* newNode(void){
  node *t=(node *)calloc(1,sizeof(node));
  t->sum=0;
  t->l=t->r=NULL;
  return t;
}

node* add(node *t,int l,int r,int index,int v){
  if(index<l || r<=index) return t;
  if(t==NULL) t=newNode();
  t->sum+=v;
  if(l+1==r) return t;
  int m=(l+r)/2;
  if(index<m){
    t->l=add(t->l,l,m,index,v);
  } else {
    t->r=add(t->r,m,r,index,v);
  }
  return t;
}

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

void run(void){
  int n;
  scanf("%d",&n);
  node *t=NULL;
  int l=0;
  int r=1000000001;
  int64 ans=0;
  while(n--){
    int op,x,y;
    scanf("%d%d%d",&op,&x,&y);
    if(op==0){
      t=add(t,l,r,x,y);
    } else {
      ans+=sum(t,l,r,x,y+1);
    }
  }
  printf("%lld\n",ans);
}

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