首页 > 代码库 > Wikioi 1082线段树成段更新成段查询

Wikioi 1082线段树成段更新成段查询

这题从昨晚搞到现在哭敲了又改好久,刚开始是update中错了,然后找到了。但是还错,然后因为题目没有数据的范围提示,所以弄了好久都不知道哪错了,最后看评论才知道是超int了,改了之后还有错,然后才发现虽然改成long long了,但是输出的时候没改,哈哈……

#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <deque>#include <vector>#include <queue>#include <string>#include <cstring>#include <map>#include <stack>#include <set>#define PI acos(-1.0)#define mem(a,b) memset(a,b,sizeof(a))#define sca(a) scanf("%d",&a)#define sc(a,b) scanf("%d%d",&a,&b)#define pri(a) printf("%d\n",a)#define lson i<<1,l,mid#define rson i<<1|1,mid+1,r#define MM 2000004#define MN 1008#define INF 2000000000#define eps 1e-8using namespace std;typedef long long ll;typedef unsigned long long ULL;ll sum[MM],val[MM];  //1082题void pushUp(int i){    sum[i]=sum[i<<1]+sum[i<<1|1];}void pushDown(int i,int l,int r) //处理lazy标记{    if(val[i])    {        int mid=(l+r)>>1;        val[i<<1]+=val[i],val[i<<1|1]+=val[i];        sum[i<<1]+=(mid-l+1)*val[i];        sum[i<<1|1]+=(r-mid)*val[i];        val[i]=0;    }}void update(int i,int l,int r,int L,int R,int v){    if(L<=l&&r<=R)    {        val[i]+=v;        sum[i]+=(r-l+1)*v;        return ;    }    int mid=(l+r)>>1;    pushDown(i,l,r);    if(L>mid) update(rson,L,R,v);    else if(R<=mid) update(lson,L,R,v);    else    {        update(lson,L,mid,v);        update(rson,mid+1,R,v);    }    pushUp(i);}ll query(int i,int l,int r,int L,int R){    if(L<=l&&r<=R) return sum[i];    ll ans=0,mid=(l+r)>>1;    pushDown(i,l,r);    if(L>mid) return query(rson,L,R);    else if(R<=mid) return query(lson,L,R);    else    {        ans+=query(lson,L,mid);        ans+=query(rson,mid+1,R);    }    return ans;}int main(){    int n,q,mm,i,a,b,s;    sca(n);    for(i=1;i<=n;i++)    {        sca(a);        update(1,1,n,i,i,a);    }    sca(q);    while(q--)    {        sca(mm);        if(mm==1)        {            scanf("%d%d%d",&a,&b,&s);            update(1,1,n,a,b,s);        }        else        {            sc(a,b);            printf("%lld\n",query(1,1,n,a,b));        }    }    return 0;}/*51 2 3 4 552 2 52 4 52 3 31 2 5 22 3 5*/