poj 3468 A Simple Problem with Integers 线段树加延迟标记

A Simple Problem with Integers
 

Description

You have N integers, A1A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15

Hint

The sums may exceed the range of 32-bit integers.

Source

思路:简单的区间更新跟区间求和,注意会爆int
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#define true ture
#define false flase
using namespace std;
#define ll long long
int scan()
{
    int res = 0 , ch ;
    while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
    {
        if( ch == EOF )  return 1 << 30 ;
    }
    res = ch - '0' ;
    while( ( ch = getchar() ) >= '0' && ch <= '9' )
        res = res * 10 + ( ch - '0' ) ;
    return res ;
}
struct is
{
    ll l,r;
    ll num;
    ll lazy;
}tree[200010*3];
void build_tree(ll l,ll r,ll pos)
{
    tree[pos].l=l;
    tree[pos].r=r;
    tree[pos].lazy=0;
    if(l==r)
    {
        //tree[pos].num=1;
        scanf("%lld",&tree[pos].num);
        return;
    }
    ll mid=(l+r)/2;
    build_tree(l,mid,pos*2);
    build_tree(mid+1,r,pos*2+1);
    tree[pos].num=tree[pos*2].num+tree[pos*2+1].num;
}
void update(ll l,ll r,ll change,ll pos)
{
    if(tree[pos].l==l&&tree[pos].r==r)
    {
        tree[pos].lazy+=change;
        tree[pos].num+=(tree[pos].r-tree[pos].l+1)*change;
        return;
    }
    if(tree[pos].lazy)
    {
        tree[pos*2].num+=(tree[pos*2].r+1-tree[pos*2].l)*tree[pos].lazy;
        tree[pos*2+1].num+=(tree[pos*2+1].r+1-tree[pos*2+1].l)*tree[pos].lazy;
        tree[pos*2].lazy+=tree[pos].lazy;
        tree[pos*2+1].lazy+=tree[pos].lazy;
        tree[pos].lazy=0;
    }
    ll mid=(tree[pos].l+tree[pos].r)/2;
    if(r<=mid)
    update(l,r,change,pos*2);
    else if(l>mid)
    update(l,r,change,pos*2+1);
    else
    {
        update(l,mid,change,pos*2);
        update(mid+1,r,change,pos*2+1);
    }
    tree[pos].num=tree[pos*2].num+tree[pos*2+1].num;
}
ll query(ll l,ll r,ll pos)
{
    //cout<<l<<" "<<r<<" "<<pos<<endl;
    if(tree[pos].l==l&&tree[pos].r==r)
    return tree[pos].num;
    if(tree[pos].lazy)
    {
        tree[pos*2].num+=(tree[pos*2].r+1-tree[pos*2].l)*tree[pos].lazy;
        tree[pos*2+1].num+=(tree[pos*2+1].r+1-tree[pos*2+1].l)*tree[pos].lazy;
        tree[pos*2].lazy+=tree[pos].lazy;
        tree[pos*2+1].lazy+=tree[pos].lazy;
        tree[pos].lazy=0;
    }
    ll mid=(tree[pos].l+tree[pos].r)/2;
    if(l>mid)
    return query(l,r,pos*2+1);
    else if(r<=mid)
    return query(l,r,pos*2);
    else
    return query(l,mid,pos*2)+query(mid+1,r,pos*2+1);
}
int main()
{
    ll x,q,i,t;
    while(~scanf("%lld",&x))
    {
        scanf("%lld",&q);
        build_tree(1,x,1);
        while(q--)
        {
            char a[10];
            ll l,r;
            ll change;
            scanf("%s%lld%lld",a,&l,&r);
            if(a[0]=='C')
            {
                scanf("%lld",&change);
                update(l,r,change,1);
            }
            else
            printf("%lld\n",query(l,r,1));
        }
    }
    return 0;
}
View Code

 

posted @ 2016-04-18 14:41  jhz033  阅读(213)  评论(0编辑  收藏  举报