POJ——3468A Simple Problem with Integers(区间更新)

来源:互联网 发布:java打印高为n的金字塔 编辑:程序博客网 时间:2024/04/29 21:25
A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072KTotal Submissions: 78332 Accepted: 24139Case Time Limit: 2000MS

Description

You have N integers, A1, A2, ... ,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 A1,A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+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

Hint

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

Source

POJ Monthly--2007.11.25, Yang Yi




这题WA了许久,最后发现读入数据时也应该是%I64d,而且用%LLd还不行
/** 线段树:区间更新 *线段树每个节点存储当前区间的和 *标记数组存取当前增量,记得每次更新增量时要自加上之前的该区间增量,因为有可能之前该区间的增量并没有向下更新掉 *注意增量可能是负数 *输入输出用%I64d,%LLD会WA,应该和评测系统有关 *最好不用cin,cout,除非关掉同步 */#include<iostream>#include<stdio.h>#include<string.h>#define rson mid + 1,r,rt << 1 |1#define lson l,mid,rt << 1#define LL long longusing namespace std;const int N = 100005;LL Tree[N << 2];LL col[N << 2];void PushUp(int rt)//向上更新和{    Tree[rt] = Tree[rt << 1] + Tree[rt << 1 | 1];}void PushDown(int rt,int l)//向下更新增量{    if(col[rt])    {        col[rt << 1] += col[rt];        col[rt << 1 | 1] += col[rt];        Tree[rt << 1] += (l - (l >> 1)) * col[rt];        Tree[rt << 1 | 1] += (l >> 1) * col[rt];        col[rt] = 0;    }}void Build(int l,int r,int rt){    col[rt] = 0;    if(l == r)    {        scanf("%I64d",&Tree[rt]);        return;    }    int mid = (l+r) >> 1;    Build(lson);    Build(rson);    PushUp(rt);}void Upgrade(int ll,int rr,int v,int l,int r,int rt){    if(ll <= l && r <= rr)    {        Tree[rt] += (v * (r - l + 1));        col[rt] += v;        return;    }    PushDown(rt,r - l + 1);//    int mid = (l + r) >> 1;    if(ll <= mid) Upgrade(ll,rr,v,lson);    if(rr > mid) Upgrade(ll,rr,v,rson);    PushUp(rt);}LL Query(int ll,int rr,int l,int r,int rt){    LL sum = 0;    if(ll <= l && r <= rr)    {        sum += Tree[rt];        return sum;    }    PushDown(rt,r - l + 1);    int mid = (l + r) >> 1;    if(ll <= mid) sum += Query(ll,rr,lson);    if(rr > mid) sum += Query(ll,rr,rson);    return sum;}int main(){    int n,a,b,c,q;    char temp[2];    //freopen("FileIn.txt","r",stdin);freopen("FileOut.txt","w",stdout);    scanf("%d%d",&n,&q);    Build(1,n,1);    for(int i = 0; i < q; i++)    {        scanf("%s%d%d",&temp,&a,&b);        if(temp[0] == 'C')        {            scanf("%d",&c);            Upgrade(a,b,c,1,n,1);        }        else        {            printf("%I64d\n",Query(a,b,1,n,1));        }    }    //fclose(stdin);fclose(stdout);    return 0;}


0 0
原创粉丝点击