UVALive 2191 水一发树状数组

Posted by Cww97 on 2016-03-07

版权声明:本文为博主原创文章,未经博主允许不得转载。原文所在http://blog.csdn.net/cww97 https://blog.csdn.net/cww97/article/details/50817008
题目点着

题意:单点修改,区间查询,树状数组
注意输出格式,每组数据之间有回车,最后一组没有

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
#include<cstdio>
#include<cstring>
#include<iostream>
typedef long long LL;
using namespace std;
const int N=233333;
int n,a[N],T[N];
char st[5];

void add(int k,int num){
while (k<=n){
T[k]+=num;
k+=k&(-k);
}
}
LL sum(int k){
LL s=0;
while (k){
s+=T[k];
k-=k&(-k);
}
return s;
}

int main(){
//freopen("fuck.in","r",stdin);
for (int cas=1;scanf("%d",&n)==1&&n;cas++){
memset(T,0,sizeof(T));
for (int i=1;i<=n;i++){
scanf("%d\n",&a[i]);
add(i,a[i]);
}
if (cas>1)puts("");
printf("Case %d:\n",cas);
while(scanf("%s",st)&&st[0]!='E'){
int x,y;
scanf("%d%d\n",&x,&y);
if (st[0]=='M')printf("%lld\n",sum(y)-sum(x-1));
else {add(x,y-a[x]);a[x]=y;}
}
}
return 0;
}