线段树板子poj3468

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define ls (p<<1)
#define rs ((p<<1)|1)
#define l(x) tree[x].l
#define r(x) tree[x].r
#define sum(x) tree[x].sum
#define lazy(x) tree[x].lazy
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
int n,m,a[100010],x,y,d;
struct one
{
    int l,r;
    long long sum,lazy;
};
one tree[400010];
void Down(int p)
{
    lazy(ls)+=lazy(p);
    lazy(rs)+=lazy(p);
    sum(ls)+=(long long)(r(ls)-l(ls)+1)*lazy(p);
    sum(rs)+=(long long)(r(rs)-l(rs)+1)*lazy(p);
    lazy(p)=0;
    return ;
}
void maketree(int p,int l,int r)
{
    l(p)=l;r(p)=r;lazy(p)=0;
    if(l==r){sum(p)=a[l];lazy(p)=0;return;}
    int mid=(l+r)>>1;
    maketree(ls,l,mid);
    maketree(rs,mid+1,r);
    sum(p)=sum(ls)+sum(rs);
    lazy(p)=0;
}
bool pd()
{
    char ch=getchar();
    while(ch!='C'&&ch!='Q')ch=getchar();
    if(ch=='C')return true;
    else return false;
}
void chag(int p,int l,int r,int v)
{
    if(l(p)>=l&&r(p)<=r)
    {
        sum(p)+=(long long)(r(p)-l(p)+1)*v;
        lazy(p)+=v;
        return ;
    }
    if(l(p)>r||r(p)<l)return;
    Down(p);
    lazy(p)=0;
    int mid=(l+r)>>1;
    chag(ls,l,r,v);
    chag(rs,l,r,v);
    sum(p)=sum(ls)+sum(rs);
    return;
}
long long find(int p,int l,int r)
{
    if(l(p)>=l&&r(p)<=r)
    return sum(p);
    if(l(p)>r||r(p)<l)
    return 0;
    Down(p);
    int mid=(l+r)>>1;
    return find(ls,l,r)+find(rs,l,r);
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)a[i]=read();
    maketree(1,1,n);
    int l,r,d;
    for(int i=1;i<=m;i++)
    {
        if(pd())
        {
            l=read();r=read();d=read();
            chag(1,l,r,d);
        }
        else 
        {
            l=read();r=read();
            printf("%lld\n",find(1,l,r));
        }
    }
    return 0;
}

 

posted @ 2020-09-10 20:37  mybing  阅读(97)  评论(0编辑  收藏  举报