题目链接:传送门
题目大意:
给定n个整数,实现下面的两种操作
S x y(把第x个数改成y) M x y(计算第x个数到第y个数之和)。
代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
#define lowbit(x) (x&-x)
typedef long long llt;
const int N = 200010;
const int M = 50010;
const int INF = 0x3fffffff;
int C[N],A[N];
void init(int n)
{
memset(C,0,sizeof(C));
for(int i = 1; i <= n; ++i){
for(int j = i-lowbit(i)+1; j <= i; ++j){
C[i] += A[j];
}
}
}
int sum(int x)
{
int ret = 0;
while(x){
ret += C[x];
x -= lowbit(x);
}
return ret;
}
//A[x] += d;
void add(int x,int d,int n)
{
while(x <= n){
C[x] += d;
x += lowbit(x);
}
}
char st[100];
int main()
{
// freopen("D:\\06.in","r",stdin);
// freopen("D:\\data.txt","w",stdout);
int n,cnt = 0;
while(~scanf("%d",&n) && n){
for(int i = 1; i <= n; ++i) scanf("%d",&A[i]);
init(n);
char ch; int a,b;
getchar();
if(cnt) printf("\n");
printf("Case %d:\n",++cnt);
while(1){
gets(st);
if(strcmp(st,"END") == 0) break;
sscanf(st,"%c %d %d",&ch,&a,&b);
if(ch == 'S'){
add(a,b-A[a],n);
A[a] = b;
}else{
printf("%d\n",sum(b)-sum(a-1));
}
}
}
return 0;
}