POJ 3468 A Simple Problem with Integers (线段树区域更新)

A Simple Problem with Integers

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 62431   Accepted: 19141
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

 
线段树的基础区域更新
#include<cstdio>
#include<iostream>
#include<cstring>
#include<stack>
#include<queue>
#include<cmath>
#include<stdlib.h>
#include<algorithm>
#define LL __int64
using namespace std;
const int MAXN=100000+5;
const int INF=0x3f3f3f3f;
struct node
{
    int l,r;
    LL num,flag;
    int mid()
    {
        return (l+r)>>1;
    }
}a[MAXN*4];
int b[MAXN],n,m;

void pushdown(int len,int step)
{
    if(a[step].flag)
    {
        a[step*2].flag+=a[step].flag;
        a[step*2+1].flag+=a[step].flag;
        a[step*2].num+=(len-len/2)*a[step].flag;
        a[step*2+1].num+=(len/2)*a[step].flag;
        a[step].flag=0;
    }
}

void build(int l,int r,int step)
{
    a[step].l=l;
    a[step].r=r;
    a[step].flag=0;
    if(l==r)
    {
        a[step].num=b[l];
        return ;
    }
    int mid=a[step].mid();
    build(l,mid,step*2);
    build(mid+1,r,step*2+1);
    a[step].num=a[step*2].num+a[step*2+1].num;
}

void update(int x,int y,LL val,int step)
{
    if(x<=a[step].l && a[step].r<=y)
    {
        a[step].flag+=val;
        a[step].num+=(a[step].r-a[step].l+1)*val;
        return ;
    }
    pushdown(a[step].r-a[step].l+1,step);
    int mid=a[step].mid();
    if(x>mid) update(x,y,val,step*2+1);
    else if(y<=mid) update(x,y,val,step*2);
    else
    {
        update(x,mid,val,step*2);
        update(mid+1,y,val,step*2+1);
    }
    a[step].num=a[step*2].num+a[step*2+1].num;
}

LL query(int x,int y,int step)
{
    if(x<=a[step].l && a[step].r<=y) return a[step].num;
    pushdown(a[step].r-a[step].l+1,step);
    int mid=a[step].mid();
    if(x>mid) return query(x,y,step*2+1);
    else if(y<=mid) return query(x,y,step*2);
    else return query(x,y,step*2)+query(x,y,step*2+1);
}

int main()
{
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++) scanf("%d",&b[i]);
        build(1,n,1);
        while(m--)
        {
            char str[10];
            int A,B;
            scanf("%s %d %d",str,&A,&B);
            if(str[0]=='Q')
                printf("%I64d
",query(A,B,1));
            if(str[0]=='C')
            {
                int cnt;
                scanf("%d",&cnt);
                update(A,B,cnt,1);
            }
        }
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/clliff/p/3934493.html