0%

POJ_3468

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.

输出描述:

You need to answer all Q commands in order. One answer in a line.

示例1
输入

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

输出

4
55
9
15

说明

The sums may exceed the range of 32-bit integers

poj_3468.cppview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
ll N,tree1[100001],tree2[100001];
ll a[100001];
int lowbit(int x)
{
return (x&(-x));
}
void update(ll x ,ll val)
{
ll i;
for( i = x ; i <= N;i+=lowbit(i) )
{
tree1[i]+=val;
tree2[i]+=x*val;
}
}
ll query(ll x)
{
ll i;
ll sum=0;
for( i = x ; i > 0 ;i-=lowbit(i))
{
sum+=(x+1)*tree1[i]-tree2[i];
}
return sum;
}
int main()
{
ll Q , i , A ,B , C;
string s;
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> Q;
a[0]=0;
for ( i = 1 ;i <= N ; i++)
{
cin >> a[i];
update(i ,a[i]-a[i-1]);
}
for ( i = 1 ; i <= Q ; i ++)
{
cin >> s;
if(s[0]=='Q')
{
cin >> A >> B ;
cout << query (B)- query(A-1)<<endl;
}
else
{
cin >> A >> B >> C;
update(A , C);
update(B+1 ,-C );
}
}
}