結果

問題 No.789 範囲の合計
コンテスト
ユーザー akakimidori
提出日時 2019-02-08 21:44:21
言語 C(gnu17)
(gcc 15.2.0)
コンパイル:
gcc-15 -O2 -std=gnu17 -Wno-error=implicit-function-declaration -Wno-error=implicit-int -Wno-error=incompatible-pointer-types -Wno-error=int-conversion -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
AC  
実行時間 163 ms / 1,000 ms
コード長 1,029 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 191 ms
コンパイル使用メモリ 39,012 KB
最終ジャッジ日時 2026-02-22 02:28:17
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#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