POJ 3468

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 <bits/stdc++.h>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
ll tree[maxn][2];
int n;
int lowbit(int x)
{
    return x&-x;
}
void update(ll x,int i,int f)
{
    while(i<=n)
    {
        tree[i][f]+=x;
        i+=lowbit(i);
    }
}
ll query(int i,int f)
{
    ll ans=0;
    while(i>0)
    {
        ans+=tree[i][f];
        i-=lowbit(i);
    }
    return ans;
}
ll ask(int i)
{
    return 1ll*(i+1)*query(i,0)-1ll*query(i,1);
}
int main()
{
    int q;
    scanf("%d%d",&n,&q);
    int x=0,y;
    for(int i = 1;i <= n;++i)
    {
        scanf("%d",&y);
        update(1ll*y-x,i,0);
        update(1ll*(y-x)*i,i,1);
        x=y;
    }
    while(q--)
    {
        char op;
        scanf(" %c",&op);
        if(op=='C')
        {
            int a,b,x;
            scanf("%d%d%d",&a,&b,&x);
            update(x,a,0);
            update(-x,b+1,0);
            update(1ll*x*a,a,1);
            update(1ll*-x*(b+1),b+1,1);
        }
        else
        {
            int a,b;
            scanf("%d%d",&a,&b);
            ll ans=ask(b)-ask(a-1);
            printf("%lld\n",ans);
        }
    }
    return 0;
}

 

 
posted @ 2019-04-25 17:26  tryatry  阅读(218)  评论(0编辑  收藏  举报