版本比较
动态开点线段树
正文
增加 9 行删除 9 行
42 行未改动4344454647474849504950515253545556575859606160616263646566676667686970716 行未改动7879808182838283848586879 行未改动42 行未改动 struct node{//左右孩子,延迟标记,区间和 int ls,rs; ll add,sum; }t[Q*100];//一共q次询问,每次最多新开2*logn个节点,所以空间复杂度是O(qlogn)
int tot=0,root=0;//总结点个数,根节点编号 void Pushdown(int x,int l,int r){//下推标记
void Pushdown(int x,int l,int r){//下推标记 int &lx=t[x].ls,&rx=t[x].rs,mid=(l+r)>>1; if(!lx) lx = ++tot; t[lx].w+=t[x].w; t[lx].sum+=(mid-l+1)*t[x].w; if(!rx) rx = ++tot; t[rx].w+=t[x].w; t[rx].sum+=(r-mid)*t[x].w; t[x].w=0; }
void Pushup(int x){ int &lx=t[x].ls,&rx=t[x].rs; t[x].sum=t[lx].sum+t[rx].sum; }
void update(int &x,int l,int r,ll v,int L,int R) {//注意x需要引用传递,因为你可能需要加入新节点 if(!x) x = ++tot; if(l>=L&&r<=R) {6 行未改动 if(L<=mid) update(t[x].ls,l,mid,v,L,R); if(R>mid) update(t[x].rs,mid+1,r,v,L,R); Pushup(x); }
ll query(int &x,int l,int r,int L,int R){ if(!x) return 0; ll res=0; if(l>=L&&r<=R) return t[x].sum;9 行未改动