UVa 12086 Potentiometers 树状数组裸题 单点更新 区间查询

题意:单点更新,区间查询

树状数组裸题:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;

#define ll int
#define N 200010
int tree[N], maxn;
int lowbit(int x){return x&(-x);}
int sum(int x){
	int ans = 0;
	while(x>=1){ans+=tree[x]; x-=lowbit(x);}
	return ans;
}
void change(int x, int val){
	while(x <= maxn)
	{tree[x] += val; x+=lowbit(x);}
}
char s[3];
ll num[N];
int main(){
	ll n, u, v, Cas = 1;
	while(scanf("%d",&n),n){
		memset(tree, 0, sizeof(tree));maxn = n;
		for(int i = 1; i <= n; i++){scanf("%d",&num[i]); change(i, num[i]);}
		
		if(Cas>1)puts("");
		printf("Case %d:\n", Cas++);
		while(1){
			scanf("%s",s); if(s[0]=='E')break; scanf("%d %d",&u,&v);
			if(s[0]=='S'){
				change(u, -num[u]);
				change(u, v);
				num[u] = v;
			}
			else printf("%d\n", sum(v)-sum(u-1));
		}		
	}
	return 0;
}


你可能感兴趣的:(UVa 12086 Potentiometers 树状数组裸题 单点更新 区间查询)