poj3468 A Simple Problem with Integers

A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 71334   Accepted: 21998
Case Time Limit: 2000MS

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

POJ Monthly--2007.11.25, Yang Yi

仍然是一道线段树区间更新的问题,上次做的那道题目只涉及到更新,查询的时候只查询根节点,而这个也是需要查询的,同样的道理,查询一个节点之前要完成其之前未完成的向下更新操作,而且,由于题目给出的操作是在每一个节点上都加上一个数,但是在向下更新的时候,下面的节点也有可能存在更早的未完成的向下更新操作,而且这两次更新的效果是累加的,所以延时标志也应该是累加的。

#include <iostream>
#include <stdio.h>
#define MAX 800000
using namespace std;

struct Tree
{
    long long sum;
    long long flag;
};
Tree tree[MAX];
void PushUp(int rt)
{
    tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}

void PushDown(int rt,int len)
{
    if(tree[rt].flag!=0)
    {
        tree[rt<<1].flag+=tree[rt].flag;
        tree[rt<<1|1].flag+=tree[rt].flag;
        tree[rt<<1].sum+=(len-(len>>1))*tree[rt].flag;
        tree[rt<<1|1].sum+=(len>>1)*tree[rt].flag;
        tree[rt].flag=0;
    }
}

void Build(int l,int r,int rt)
{
    tree[rt].flag=0;
    if(l==r)
    {
        cin>>tree[rt].sum;
        return;
    }
    int m=(l+r)>>1;
    Build(l,m,rt<<1);
    Build(m+1,r,rt<<1|1);
    PushUp(rt);
}

void Update(int L,int R,int d,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        tree[rt].flag+=d;
        tree[rt].sum+=(r-l+1)*d;
        return;
    }
    PushDown(rt,r-l+1);
    int m=(l+r)>>1;
    if(L<=m)
        Update(L,R,d,l,m,rt<<1);
    if(R>m)
        Update(L,R,d,m+1,r,rt<<1|1);
    PushUp(rt);
}

long long Query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        return tree[rt].sum;
    }
    PushDown(rt,r-l+1);
    int m=(l+r)>>1;
    long long sum=0;
    if(L<=m)
        sum+=Query(L,R,l,m,rt<<1);
    if(R>m)
        sum+=Query(L,R,m+1,r,rt<<1|1);
    return sum;
}


int main()
{
    int n,q;
    int a,b,c;
    int i;
    char op;
    scanf("%d%d",&n,&q);
    {
        Build(1,n,1);
        while(q--)
        {
            cin>>op;
            if(op=='Q')
            {
                scanf("%d%d",&a,&b);
                cout<<Query(a,b,1,n,1)<<endl;
            }
            else if(op=='C')
            {
                scanf("%d%d%d",&a,&b,&c);
                Update(a,b,c,1,n,1);
            }
        }
    }
    return 0;
}





你可能感兴趣的:(simple,problem,a,线段树区间更新,poj3468,wit)