POJ 3468 A Simple Problem with Integers(線段樹區間操作)

畫船聽雨發表於2014-02-25

題目的意思很簡單就是一個線段樹的區間的增加數字,與區間的查詢。

說一下,區間操作的題目第一次做啊,我瞎搞了一下,超時了啊。於是求助於嘯爺,嘯爺又是“苦心教導”啊。。感激不盡啊。。

一個區間當有更新的時候,先把區間上的總和更新一下,然後標記一下更新的多少,然後如果以後還會找到這個區間的時候,要把他所標記的那個數字傳到他的左右子樹中去。因為,這樣的話,只更新了這個區間。他的子區間能沒有發生過改變。所以要把他的標記給下面的然後抹去自己的標記。(說明它自己已經完成它對自己子樹的更新)。

A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 53175   Accepted: 15899
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
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 10001000
//#define LL __LL64
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
const LL maxn = 100100;

using namespace std;

struct node
{
    LL sum;
    LL add;
}f[4*maxn];

LL num[4*maxn];

void Bulid(LL l, LL r, LL site)
{
    if(l == r)
    {
        f[site].sum = num[l];
        f[site].add = 0;
        return;
    }
    LL mid = (l+r)>>1;
    Bulid(l, mid, site<<1);
    Bulid(mid+1, r, site<<1|1);
    f[site].add = 0;
    f[site].sum = f[site<<1].sum+f[site<<1|1].sum;
}

void Updata(LL L, LL R, LL l, LL r, LL site, LL x)
{
    if(L == l && R == r)
    {
        f[site].add += x;
        f[site].sum += (r-l+1)*x;
        return;
    }
    LL mid = (L+R)>>1;

    if(f[site].add)//這裡如果不去掉標記的話,最後的賦值又可能會變小;因為沒更新左右的值;
    {
        Updata(L, mid, L, mid, site<<1, f[site].add);
        Updata(mid+1, R, mid+1, R, site<<1|1, f[site].add);
        f[site].add = 0;
    }

    if(r <= mid)
        Updata(L, mid, l, r, site<<1, x);
    else if(l > mid)
        Updata(mid+1, R, l, r, site<<1|1, x);
    else
    {
        Updata(L, mid, l, mid, site<<1, x);
        Updata(mid+1, R, mid+1, r, site<<1|1, x);
    }
    f[site].sum = f[site<<1].sum+f[site<<1|1].sum;
}

node Qusery(LL L, LL R, LL l, LL r, LL site)
{

    if(L == l && R == r)
    {
        return f[site];
    }
    LL mid = (L + R)>>1;

    if(f[site].add)
    {
        Updata(L, mid, L, mid, site<<1, f[site].add);
        Updata(mid+1, R, mid+1, R, site<<1|1, f[site].add);
        f[site].add = 0;
    }

    if(r <= mid)
    {
        return Qusery(L, mid, l, r, site<<1);
    }
    else if(l > mid)
    {
        return Qusery(mid+1, R, l, r, site<<1|1);
    }
    node t1 = Qusery(L, mid, l, mid, site<<1);
    node t2 = Qusery(mid+1, R, mid+1, r, site<<1|1);
    t1.sum += t2.sum;
    return t1;
}


int main()
{
    LL n, m;
    while(~scanf("%lld %lld",&n, &m))
    {
        for(LL i = 1; i <= n; i++)
            scanf("%lld",&num[i]);
        char str;
        LL l, r, x;
        Bulid(1, n, 1);
        while(m--)
        {
            scanf("%*c%c",&str);
            if(str == 'Q')
            {
                scanf("%lld %lld",&l, &r);
                node temp;
                temp = Qusery(1, n, l, r, 1);
                printf("%lld\n",temp.sum);
            }
            else if(str == 'C')
            {
                scanf("%lld %lld %lld",&l, &r, &x);
                Updata(1, n, l, r, 1, x);
            }
        }
    }
    return 0;
}


相關文章