A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。

A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 69589   Accepted: 21437
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.

Source

 
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <string>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #include <stack>
11 #include <queue>
12 #include <sstream>
13 #include <iomanip>
14 using namespace std;
15 typedef long long LL;
16 const int INF=0x4fffffff;
17 const int EXP=1e-5;
18 const int MS=100005;
19 
20 LL sum[MS];
21 LL C[2][MS];
22 int N,Q;
23 
24 int lowbit(int x)
25 {
26       return x&(-x);
27 }
28 
29 void updata(int no,int x,LL value)
30 {
31       while(x<=N)
32       {
33             C[no][x]+=value;
34             x+=lowbit(x);
35       }
36 }
37 
38 LL getsum(int no,int x)
39 {
40       LL res=0;
41       while(x>0)
42       {
43             res+=C[no][x];
44             x-=lowbit(x);
45       }
46       return res;
47 }
48 
49 void solve()
50 {
51       scanf("%d%d",&N,&Q);
52       sum[0]=0;
53       for(int i=1;i<=N;i++)
54       {
55             scanf("%lld",&sum[i]);
56             sum[i]+=sum[i-1];
57       }
58       memset(C,0,sizeof(C));
59       int l,r;
60       LL c;
61       char cmd[10];
62       for(int i=1;i<=Q;i++)
63       {
64             scanf("%s",cmd);
65             if(cmd[0]=='Q')
66             {
67                   scanf("%d%d",&l,&r);
68                   LL ans=sum[r]-sum[l-1]+(getsum(0,r)-getsum(1,r)*(N-r))-(getsum(0,l-1)-getsum(1,l-1)*(N-l+1));
69                   printf("%lld\n",ans);
70             }
71             else
72             {
73                   scanf("%d%d%lld",&l,&r,&c);
74                   updata(0,l,c*(N-l+1));
75                   updata(0,r+1,(N-r)*(-c));
76 
77                   updata(1,l,c);
78                   updata(1,r+1,-c);
79             }
80       }
81 }
82 
83 
84 int main()
85 {
86       solve();
87       return 0;
88 }

 

posted @ 2015-03-31 21:34  daydaycode  阅读(155)  评论(0编辑  收藏  举报