日常求找错

P3372 【模板】线段树 1

```cpp #include<bits/stdc++.h> #include<queue> #define lc p<<1 #define rc p<<1|1 using namespace std; inline void read(int &x) { x=0;int k=1; char c=getchar(); while(c<'0'||c>'9'){if(c=='-')k=-1;c=getchar();} while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^48),c=getchar(); x*=k; }//快读 struct tree{ int sum,tag; }a[1000001]; int data[1000001]; void push_up(int p){ a[p].sum=a[lc].sum+a[rc].sum; } void push_down(int p,int l,int r){ if(!a[p].tag)return; int mid=(l+r)>>1; a[lc].tag+=a[p].tag; a[rc].tag+=a[p].tag; a[lc].sum+=(mid-l+1)*a[p].tag; a[rc].sum+=(r-mid)*a[p].tag; a[p].tag=0; } void build(int p,int l,int r){ if(l==r){a[p].sum=data[l];return;} int mid=(l+r)>>1; build(lc,l,mid); build(rc,mid+1,r); push_up(p); } void add(int p,int l,int r,int L,int R,int k){ if(l>=L&&r<=R){a[p].sum+=(r-l+1)*k;a[p].tag+=k;return;} push_down(p,l,r); int mid=(l+r)>>1; if(L<=mid)add(lc,l,mid,L,R,k); if(R>mid)add(rc,mid+1,r,L,R,k); push_up(p); } int query(int p,int l,int r,int L,int R){ if(l>=L&&r<=R)return a[p].sum; push_down(p,l,r); int mid=(l+r)>>1,ans=0; if(L<=mid)ans+=query(lc,l,mid,L,R); if(R>mid)ans+=query(rc,mid+1,r,L,R); return ans; } int main(){ int n,m; int q; read(n);read(m); for(long long i=1;i<=n;i++) read(data[i]); build(1,1,n); while(m--){ read(q); if(q==1) { int x,y,k; read(x);read(y);read(k); add(1,1,n,x,y,k); } else{ int x,y; read(x);read(y); cout<<query(1,1,n,x,y)<<endl; } } return 0; } ```
by _zby_ @ 2019-08-14 09:23:08


@[_zby_](/space/show?uid=64977) Orz
by hewo @ 2019-08-14 09:23:59


@[_zyx_](/space/show?uid=64976) 你不是要写作业吗qwq
by _zby_ @ 2019-08-14 09:25:18


~~树状数组~~
by 塔罗兰 @ 2019-08-14 09:25:33


@[塔罗兰](/space/show?uid=111741) ~~巧了,我用树状数组没炸过了~~
by hewo @ 2019-08-14 09:26:54


@几个人 @[__QL__](/space/show?uid=108402) @[Xecades](/space/show?uid=64434) @[_hyc_](/space/show?uid=144821)
by hewo @ 2019-08-14 09:29:12


@[_zyx_](/space/show?uid=64976) Orz
by __QL__ @ 2019-08-14 09:30:12


我不会线段树stO
by __QL__ @ 2019-08-14 09:30:50


@[_zyx_](/space/show?uid=64976) pushdown时lazytag没清0
by __QL__ @ 2019-08-14 09:32:11


@[__QL__](/space/show?uid=108402) 一@就出来 6666 你整日整夜地玩(雾)洛谷吗
by hewo @ 2019-08-14 09:32:20


| 下一页