1
0
Fork 0
cp-templates/trees/segtreed.old.cc

25 lines
632 B
C++
Raw Normal View History

2023-12-02 03:38:03 +00:00
constexpr int MAXN=1e5+10;
2023-12-02 06:54:37 +00:00
int cnt,root;
2023-12-16 02:02:23 +00:00
int d[MAXN*2],ls[MAXN*2],rs[MAXN*2]; // minimum size is q*log(N)
2023-12-02 03:38:03 +00:00
void update(int s,int t,int &p,int x,int c){ // add constant c to #x
if(!p)p=++cnt; // create node
if(s==t){
d[p]+=c;
return;
}
int m=s+(t-s>>1);
2023-12-02 03:57:02 +00:00
if(x<=m)update(s,m,ls[p],x,c);
else update(m+1,t,rs[p],x,c);
2023-12-02 03:38:03 +00:00
d[p]=d[ls[p]]+d[rs[p]];
}
int query(int s,int t,int p,int l,int r){ // query range [l,r]
if(!p)return 0;
if(l<=s&&t<=r)return d[p];
int m=s+(t-s>>1),sum=0;
2023-12-02 03:57:02 +00:00
if(l<=m)sum+=query(s,m,ls[p],l,r);
if(r>m) sum+=query(m+1,t,rs[p],l,r);
2023-12-02 03:38:03 +00:00
return sum;
}