poj 3468 A Simple Problem with Integers

这道题用到了陈段更新。。。

但是速度一直在2300–2500之间徘徊。。
不知道如何更快。。
以后知道了会及时更新。。

#include<cstdio>
#include<iostream>
using namespace std;
const int maxn=100010;
long long a[maxn];
struct mytree
{
    long long lazy;
    long long me;
}tree[maxn*4];
char test[2];
void build(int node,int left,int right)
{
    tree[node].lazy=0;
    int mid=((left+right)>>1);
    if(left==right)
    {
        tree[node].me=a[mid];
    }
    else
    {
        build((node<<1),left,mid);
        build((node<<1)+1,mid+1,right);
        tree[node].me=tree[(node<<1)].me+tree[(node<<1)+1].me;
    }
}
void change(int node,int left,int right,int x,int y,long long v)
{
    if(x<=left&&right<=y)
    {
        tree[node].me+=(long long)(right-left+1)*v;
        if(left!=right)
            tree[node].lazy+=v;
        return ;
    }
    else
    {
        int mid=((left+right)>>1);
        if(x>mid)
        {
            if(y<=right)
                tree[node].me+=v*(long long)(y-x+1);
            else
                tree[node].me+=v*(long long)(right-x+1);
            change((node<<1)+1,mid+1,right,x,y,v);
        }
        else if(y<=mid)
        {
            if(left<=x)
                tree[node].me+=v*(long long)(y-x+1);
            else
                tree[node].me+=v*(long long)(y-left+1);
            change((node<<1),left,mid,x,y,v);
        }
        else
        {
            if(left<=x&&y<=right)
                tree[node].me+=(long long)(y-x+1)*v;
            else if(left<=x&&right<y)
                tree[node].me+=(long long)(right-x+1)*v;
            else if(x<left&&y<=right)
                tree[node].me+=(long long)(y-left+1)*v;
            change((node<<1),left,mid,x,y,v);
            change((node<<1)+1,mid+1,right,x,y,v);
        }
    }
}
void myson(int node,int left,int right)
{
    int mid=((right+left)>>1);
    tree[(node<<1)].me+=(long long)(mid-left+1)*tree[node].lazy;
    tree[(node<<1)+1].me+=(long long)(right-(mid+1)+1)*tree[node].lazy;
    tree[(node<<1)].lazy+=tree[node].lazy;
    tree[(node<<1)+1].lazy+=tree[node].lazy;
    tree[node].lazy=0;
}
long long query(int node,int left,int right,int x,int y)
{
    if(x<=left&&right<=y)
    {
        return tree[node].me;
    }
    if(tree[node].lazy)
    {
        myson(node,left,right);
    }
    int mid=((left+right)>>1);
    if(y<=mid)
    {
        return query((node<<1),left,mid,x,y);
    }
    else if(x>mid)
    {
        return query((node<<1)+1,mid+1,right,x,y);
    }
    else
    {
        return query((node<<1),left,mid,x,y)+query((node<<1)+1,mid+1,right,x,y);
    }
}
int  main()
{
    int  n,q;
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++)
    {
        scanf("%I64d",&a[i]);
    }
    build(1,1,n);
    while(q--)
    {
        scanf("%s",test);
        int x,y;
        long long v;
        if(test[0]=='C')
        {
            scanf("%d%d%I64d",&x,&y,&v);
            if(x>y)
                swap(x,y);
            change(1,1,n,x,y,v);
        }
        else
        {
            scanf("%d%d",&x,&y);
            if(x>y)
                swap(x,y);
            printf("%I64d\n",query(1,1,n,x,y));
        }
    }
    return 0;
}

你可能感兴趣的:(线段树,poj)