1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| struct node{ int l,r,sum,cnt; }tree[MAXN*20]; void insert(int &x,int y,int l,int r,int p){ x=++ntt; tree[x]=tree[y]; if(l==r){ tree[x].sum+=p; tree[x].cnt++; return ; } int mid=(l+r)>>1; if(p<=mid) insert(tree[x].l,tree[y].l,l,mid,p); else insert(tree[x].r,tree[y].r,mid+1,r,p); tree[x].sum=tree[tree[x].l].sum+tree[tree[x].r].sum; tree[x].cnt=tree[tree[x].l].cnt+tree[tree[x].r].cnt; } int Sum(int x,int y,int l,int r,int L,int R){ if(L>R) return 0; if(L<=l&&r<=R){ return tree[x].sum-tree[y].sum; } int mid=(l+r)>>1,res=0; if(L<=mid) res+=Sum(tree[x].l,tree[y].l,l,mid,L,R); if(R>mid) res+=Sum(tree[x].r,tree[y].r,mid+1,r,L,R); return res; } int Cnt(int x,int y,int l,int r,int L,int R){ if(L>R) return 0; if(l>r) return 0; if(L<=l&&r<=R){ return tree[x].cnt-tree[y].cnt; } int mid=(l+r)>>1,res=0; if(L<=mid) res+=Cnt(tree[x].l,tree[y].l,l,mid,L,R); if(R>mid) res+=Cnt(tree[x].r,tree[y].r,mid+1,r,L,R); return res; }
|