poj 3468 A Simple Problem with Integers

Source Code



Problem: 3468		User: shu_dayang

Memory: 4256K		Time: 1704MS

Language: C++		Result: Accepted

Source Code

#include<iostream>

#include<cstdio>

#include<cstring>

#define MID(a,b)  ((a + b) >> 1)

#define lson l,m,rt<<1

#define rson m+1,r,rt<<1|1

typedef long long LL;

using namespace std;



const int MAXN = 100005;



LL sum[MAXN << 2];

LL mark[MAXN << 2];



void PushUp(int rt)

{

    sum[rt] = sum[rt << 1] + sum[(rt << 1) + 1];

}



void Build(int l,int r,int rt)

{

    mark[rt] = 0;

    if(l == r)

    {

        scanf("%I64d",&sum[rt]);

        return;

    }

    int m = MID(l,r);

    Build(lson);

    Build(rson);

    PushUp(rt);

}



void PushDown(int x,int rt)

{

    if(mark[rt])

    {

        mark[rt<<1] += mark[rt];

        mark[rt<<1|1] += mark[rt];

        sum[rt<< 1] += (x-(x >> 1)) * mark[rt];

        sum[rt << 1 | 1] += (x >> 1) *mark[rt];

        mark[rt] = 0;

    }

}



void Update(int L,int R,int c,int l,int r,int rt)

{

    if(L<=l&&r<=R)

    {

        mark[rt]+=c;

        sum[rt]+=(LL)c*(r-l+1);

        return;

    }

    PushDown(r-l+1,rt);

    int m = MID(l,r);

    if(L<=m)

        Update(L,R,c,lson);

    if(R>m)

        Update(L,R,c,rson);

        PushUp(rt);

}



LL Query(int L,int R, int l,int r,int rt)

{

    if(L <= l && r <= R)

    {

        return sum[rt];

    }

    PushDown(r-l+1,rt);

    int m = MID(l,r);

    LL ret = 0;

    if(L<=m)

        ret+=Query(L,R,lson);

    if(R>m)

        ret+=Query(L,R,rson);

    return ret;

}



int main()

{

    int N,Q;

    int a,b,c;

    char ch[5];

    scanf("%d%d",&N,&Q);

    Build(1,N,1);

    for(int i = 0;i<Q;i++)

    {

        scanf("%s",ch);

        if(ch[0] == 'Q')

        {

            scanf("%d%d",&a,&b);

            printf("%I64d\n",Query(a,b,1,N,1));

        }

        else

        {

            scanf("%d%d%d",&a,&b,&c);

            Update(a,b,c,1,N,1);

        }

    }



    return 0;

}

 

你可能感兴趣的:(Integer)