Poj 3468 A Simple Problem with Integers

来源:互联网 发布:战争雷霆 淘宝 码 编辑:程序博客网 时间:2024/06/01 10:41

A Simple Problem with Integers

Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 92239 Accepted: 28699
Case 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 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.

手撸一发线段树成段更新,用store存储当前所要加的和,当访问时在对当前节点更新,同时转移存储到左右子树的store中。
(平时养成用scanf , printf输入输出的习惯,数据大的时候很可能会出现TLE)
AC代码

#include<iostream>#include<vector>#include<map>#include<cmath>#include<cstdio>#include<cstring>#include<algorithm>#include<ctype.h>using namespace std;struct node{    int left;    int right;    long long mid;    long long sum;    long long store;}num[500000];void Treebuild(int l , int r ,int i) // 建立一个含n个叶节点的树{    num[i].left = l;    num[i].right = r;    num[i].store = 0;    if(l == r)return ;    num[i].sum = 0;    num[i].mid = (l + r) / 2;    Treebuild(l , num[i].mid , 2 * i);    Treebuild(num[i].mid + 1 , r , 2 * i + 1);}void Tinsert(int i , int n , long long  nuc) //将每个元素插入树的叶中{    if(num[n].left == num[n].right)    {        num[n].sum = nuc;        return;    }    num[n].sum += nuc;    if(i <= num[n].mid)        Tinsert(i , 2 * n , nuc);    else Tinsert(i , 2 * n + 1 , nuc);}void Replace(int l , int r , long long nuc , int i) // 每个节点的更新及成段更新。{    if(num[i].left == num[i].right)    {        num[i].sum += nuc;        return;    }    if(l == num[i].left && r == num[i].right)    {        num[i].store += nuc;        return;    }    num[i].sum += nuc * (r - l + 1);    if(l > num[i].mid)    {        Replace(l , r , nuc , 2 * i + 1);    }    else if(r <= num[i].mid)    {        Replace(l , r , nuc , 2 * i);    }    else    {        Replace(l , num[i].mid , nuc , 2 * i);        Replace(num[i].mid + 1 , r , nuc , 2 * i + 1);    }}long long  Find(int l , int r , int i) // 查找某一段的和{    if(num[i].left == num[i].right)    return num[i].sum;    num[i].sum += num[i].store * (num[i].right - num[i].left + 1);    Replace(num[i].left , num[i].mid , num[i].store , 2 * i );    Replace(num[i].mid + 1 , num[i].right , num[i].store ,2 * i + 1);    num[i].store = 0;    if(l == num[i].left && r == num[i].right)    {        return num[i].sum;    }    else if(l > num[i].mid)    {        return Find(l , r , 2 * i + 1);    }    else if(r <= num[i].mid)        return Find(l , r , 2 * i);    else    {        return Find(l , num[i].mid , 2 * i) + Find(num[i].mid + 1 , r, 2 * i + 1);    }}int main(){    int n,m;    while(cin>>n>>m)    {        Treebuild(1,n + 1 ,1);        for(int i = 1 ; i <= n ; ++i)        {            long long  num;            scanf("%lld" , &num);            Tinsert(i , 1 , num);        }        for(int i = 1 ; i <= m ; ++i)        {            getchar();            char c;            scanf("%c", &c);            if(c == 'Q')            {                int a , b ;                scanf("%d%d", &a ,&b);                if(a > b)                {                    int t = a;                    a = b;                    b = t;                }                long long ans = Find(a ,b ,1);                cout<<ans<<endl;            }            else            {                int a , b ;                long long c;                scanf("%d%d%lld" , &a , &b , &c);                if(a > b)                {                    int t = a;                    a = b;                    b = t;                }                Replace(a , b , c , 1);            }        }    }    return 0;}
0 0