关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回

poj3468

发布时间:2022-07-27 13:55:47
                                                                                                A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 47204   Accepted: 13856
Case Time Limit: 2000MS
Descriptionpoj3468

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.    这题跟http://www.cnblogs.com/Deng1185246160/p/3247466.html  有非常相似之处 !!  
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = 100010;
int a[maxn];__int64 ans ;
struct Node
{
    int left,right;
    __int64 sum,add;
}tree[maxn*5];

void build(int l,int r,int n)
{
    tree[n].left = l;
    tree[n].right = r;
    tree[n].add = 0; 
    if(l == r)
    {
        tree[n].sum = a[l];
        return ;
    }
    int mid = (l+r)/2;
    build(l,mid,2*n);
    build(mid+1,r,2*n+1);
    tree[n].sum = tree[2*n].sum + tree[2*n+1].sum;
}

void update(int l,int r,int add,int n)
{
    if(tree[n].left > r || tree[n].right < l) return ;
    if(tree[n].left >= l && tree[n].right <= r) 
    {
        tree[n].sum += (tree[n].right - tree[n].left + 1)*add;
        tree[n].add += add;
        return ;
    }
    if(tree[n].add)
    {
        tree[2*n].sum += (tree[2*n].right - tree[2*n].left + 1)* tree[n].add;
        tree[2*n].add += tree[n].add;
        tree[2*n+1].sum += (tree[2*n+1].right - tree[2*n+1].left + 1)* tree[n].add;
        tree[2*n+1].add += tree[n].add;
        tree[n].add = 0;
    }
    update(l,r,add,2*n);
    update(l,r,add,2*n+1);
    tree[n].sum = tree[2*n].sum + tree[2*n+1].sum ;
}

void query(int l,int r,int n)
{
    if(tree[n].left > r || tree[n].right < l) return ;
    if(tree[n].left >= l && tree[n].right <= r) 
    {
        ans += tree[n].sum;
         return ;
    }
    if(tree[n].add)
    {
        tree[2*n].sum += (tree[2*n].right - tree[2*n].left + 1)* tree[n].add;
        tree[2*n].add += tree[n].add;
        tree[2*n+1].sum += (tree[2*n+1].right - tree[2*n+1].left + 1)* tree[n].add;
        tree[2*n+1].add += tree[n].add;
        tree[n].add = 0;
    }
    query(l,r,2*n);
    query(l,r,2*n+1);
    tree[n].sum = tree[2*n].sum + tree[2*n+1].sum ;
}
int main()
{
    int n,m;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        int i,x,y,z;
        char c;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        build(1,n,1);
        scanf("%*c");
        for(i=1;i<=m;i++)
        {
            scanf("%c",&c);
            if(c=='Q')
            {
                scanf("%d %d%*c",&x,&y);
                ans = 0;
                query(x,y,1);
                printf("%I64d
",ans);
            }
            else 
            {
                scanf("%d %d %d%*c",&x,&y,&z);
                update(x,y,z,1);
            }
        }
    }
    return 0;
}

/template/Home/DawnNew/PC/Static

立即注册风纳云账号,免费体验多款产品

立即注册