Codevs 1082

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
using namespace std;
struct data
{
    int left,right;
    long long sum;
    int l,r;
};
    data te[400005];
int n,q,a,b,w,cnt,p;
long long ans;
int change[400005]//here,c[200005];
void update(int x)
{
    te[x].sum=te[te[x].left].sum+te[te[x].right].sum;
}
int build(int s,int t)
{
    cnt++;
    int k=cnt;
    te[k].l=s;te[k].r=t;
    if (s==t)
    {
        te[k].sum=c[s];
        return k;
    }
    int mid=(s+t)>>1;
    te[k].left=build(s,mid);
    te[k].right=build(mid+1,t);
    update(k);
    return k;
}
void paint(int x,int k){
    te[x].sum+=((LL)te[x].r-te[x].l+1)*k;
    change[x]+=k;
}
void pushdown(int x){
    paint(te[x].left,change[x]);
    paint(te[x].right,change[x]);
    change[x]=0;
}
void insert(int x)
{
    if (x!=0)
    {
        if (change[x]!=0)
            pushdown(x);
        if (te[x].l>=a&&te[x].r<=b)
        {
            te[x].sum+=((LL)te[x].r-te[x].l+1)*w;
            change[x]=w;
            return ;
        }
        int mid=(te[x].l+te[x].r)>>1;
        if (a<=mid)  insert(te[x].left);
        if (b>mid)  insert(te[x].right);
        update(x);
        return ;
    }
}
long long findsum(int x)
{
    if (x!=0)
    {
        if (change[x]!=0)
            pushdown(x);
        if (te[x].l>b||te[x].r=a&&te[x].r<=b)  return te[x].sum;
        int mid=(te[x].l+te[x].r)>>1;
        ans=findsum(te[x].left)+findsum(te[x].right);
        return ans;
    }
}
int main ()
{
    freopen ("1082.in","r",stdin);
    //freopen ("1082.out","w",stdout);
    scanf ("%d",&n);
    for (int i=1;i<=n;i++) scanf ("%d",&c[i]);
    build(1,n);
    scanf ("%d",&q);
    for (int i=1;i<=q;i++)
    {
        scanf ("%d",&p);
        if (p==1)
        {
            scanf ("%d%d%d",&a,&b,&w);
            insert(1);
        }
        if (p==2)
        {
            scanf ("%d%d",&a,&b);
            printf ("%lld\n",findsum(1));
        }
    }
    return 0;
}

你可能感兴趣的:(Codevs 1082)