POJ 3468 A Simple Problem with Integers(线段树区间操作)

来源:互联网 发布:齐博新建文章数据库 编辑:程序博客网 时间:2024/05/21 10:27

题目的意思很简单就是一个线段树的区间的增加数字,与区间的查询。

说一下,区间操作的题目第一次做啊,我瞎搞了一下,超时了啊。于是求助于啸爷,啸爷又是“苦心教导”啊。。感激不尽啊。。

一个区间当有更新的时候,先把区间上的总和更新一下,然后标记一下更新的多少,然后如果以后还会找到这个区间的时候,要把他所标记的那个数字传到他的左右子树中去。因为,这样的话,只更新了这个区间。他的子区间能没有发生过改变。所以要把他的标记给下面的然后抹去自己的标记。(说明它自己已经完成它对自己子树的更新)。

A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072KTotal Submissions: 53175 Accepted: 15899Case 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 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4

Sample Output

455915
#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.1415926535898const 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;}


0 0