标签:math clu https dfs序 splay turn show modify ott
板子
单点修改 区间查询(求和)
1 //单点修改,区间查询(加) 2 #include<iostream> 3 #include<cstdio> 4 #include<cctype> 5 #define re register int 6 using namespace std; 7 const int MAXN=4e5+5; 8 long long val[MAXN],lazy[MAXN]; 9 int n,m,tree_cnt,ls[MAXN],rs[MAXN],a[MAXN],root; 10 inline int read() 11 { 12 int x=0;int f=1;char ch=getchar(); 13 while(!isdigit(ch)){if(ch==‘-‘) f=-1;ch=getchar();} 14 while(isdigit(ch)){x=x*10+ch-‘0‘;ch=getchar();} 15 return x*f; 16 } 17 18 inline void pushup(int cur){ 19 val[cur]=val[ls[cur]]+val[rs[cur]]; 20 } 21 22 inline void pushdown(int cur,int l,int r){ 23 if(!lazy[cur]) return; 24 lazy[ls[cur]]+=lazy[cur]; 25 lazy[rs[cur]]+=lazy[cur]; 26 int mid=(l+r)>>1; 27 val[ls[cur]]+=lazy[cur]*(mid-l+1); 28 val[rs[cur]]+=lazy[cur]*(r-mid); 29 lazy[cur]=0; 30 } 31 32 void build(int l,int r,int &cur){ 33 cur=++tree_cnt; 34 if(l==r){ 35 val[cur]=a[l]; 36 return; 37 } 38 int mid=(l+r)>>1; 39 build(l,mid,ls[cur]); 40 build(mid+1,r,rs[cur]); 41 pushup(cur); 42 } 43 44 void modify(int l,int r,int ql,int qr,int cur,long long add){ 45 if(ql<=l&&qr>=r){ 46 val[cur]+=add*(r-l+1); 47 lazy[cur]+=add; 48 return; 49 } 50 pushdown(cur,l,r); 51 int mid=(l+r)>>1; 52 if(ql<=mid) modify(l,mid,ql,qr,ls[cur],add); 53 if(qr>mid) modify(mid+1,r,ql,qr,rs[cur],add); 54 pushup(cur); 55 } 56 57 long long query(int l,int r,int ql,int qr,int cur){ 58 if(ql<=l&&qr>=r) return val[cur]; 59 pushdown(cur,l,r); 60 long long ans=0; 61 int mid=(l+r)>>1; 62 if(ql<=mid) ans+=query(l,mid,ql,qr,ls[cur]); 63 if(qr>mid) ans+=query(mid+1,r,ql,qr,rs[cur]); 64 return ans; 65 } 66 67 int main(){ 68 n=read(),m=read(); 69 for(re i=1;i<=n;i++) 70 a[i]=read(); 71 int root=1; 72 build(1,n,root); 73 for(re i=1;i<=m;i++){ 74 int t=read(),x=read(),y=read(),z; 75 if(t==1){ 76 z=read(); 77 modify(1,n,x,y,1,z); 78 } 79 else{ 80 printf("%lld\n",query(1,n,x,y,1)); 81 } 82 } 83 return 0; 84 }
https://www.luogu.com.cn/record/31184232
思路 : dfs序 线段树维护(num二进制,fg染得颜色)
二进制表示染色情况
可以开 longlong状压
然后就成了单点进行答案统计
标签:math clu https dfs序 splay turn show modify ott
原文地址:https://www.cnblogs.com/liukx/p/12381093.html