# [bzoj3155]Preprefix sum(树状数组)

## 3155: Preprefix sum

Time Limit: 1 Sec  Memory Limit: 512 MB
Submit: 1183  Solved: 546
[Submit][Status][Discuss]

5 3
1 2 3 4 5
Query 5
Modify 3 2
Query 5

35
32

## HINT

1<=N,M<=100000,且在任意时刻0<=Ai<=100000

## Source

Katharon+#1

S为  1*a1, 1*a1+1*a2, 1*a1+1*a2+1*a3...

SS为1*a1, 2*a1+1*a2, 3*a1+2*a2+1*a3...

0*a1, 0*a1+1*a2, 0*a1+1*a2+2*a3...

 1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4 #define LL long long
5 int n,m;
6 LL a[101000],bit1[101000],bit2[101000];
7 int lb(int x){
8     return x&(-x);
9 }
10 LL q1(int x){
11     LL ans=0;
12     while(x){
13         ans+=bit1[x];
14         x-=lb(x);
15     }
16     return ans;
17 }
18 LL q2(int x){
19     LL ans=0;
20     while(x){
21         ans+=bit2[x];
22         x-=lb(x);
23     }
24     return ans;
25 }
26 int c1(int x,LL num){
27     while(x<=n){
28         bit1[x]+=num;
29         x+=lb(x);
30     }
31     return 0;
32 }
33 int c2(int x,LL num){
34     while(x<=n){
35         bit2[x]+=num;
36         x+=lb(x);
37     }
38     return 0;
39 }
40 int main(){
41     scanf("%d %d",&n,&m);
42     for(int i=1;i<=n;i++){
43         scanf("%lld",&a[i]);
44         c1(i,a[i]);
45         c2(i,(i-1)*a[i]);
46     }
47     for(int i=1;i<=m;i++){
48         char in[10];
49         scanf("%s",in);
50         if(in[0]=='Q'){
51             int x;
52             scanf("%d",&x);
53             printf("%lld\n",x*q1(x)-q2(x));
54         }else{
55             int x;
56             LL y;
57             scanf("%d %lld",&x,&y);
58             LL tmp=y-a[x];
59             a[x]+=tmp;
60             c1(x,tmp);
61             c2(x,(x-1)*tmp);
62         }
63     }
64     return 0;
65 }
View Code

//p.s. 其实这道题提供了树状数组处理区间修改区间求和的一个方法

(完虐线段树oooooooooooooooooo)

//p.p.s.难道我讲的不清楚吗...

posted @ 2016-09-15 00:42  Pumbit-Legion  阅读(536)  评论(0编辑  收藏  举报