题意:
先给出一个数组,然后有两个操作
S x y 把第x个数改成y
M x y计算x~y个数的和
思路:
普通的树状数组,但不知道为什么会莫名其妙的一直WA
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <math.h> #include <bitset> #include <algorithm> #include <climits> using namespace std; #define LS 2*i #define RS 2*i+1 #define UP(i,x,y) for(i=x;i<=y;i++) #define DOWN(i,x,y) for(i=x;i>=y;i--) #define MEM(a,x) memset(a,x,sizeof(a)) #define W(a) while(a) #define gcd(a,b) __gcd(a,b) #define LL long long #define N 5000005 #define MOD 1000000007 #define INF 0x3f3f3f3f #define EXP 1e-8 #define lowbit(x) (x&-x) LL c[N]; int a[N],n; LL sum(int x) { LL ret = 0; while(x>0) { ret+=c[x]; x-=lowbit(x); } return ret; } void add(int x,int d) { while(x<=n) { c[x]+=d; x+=lowbit(x); } } int main() { int i,j,k,x,y,cas = 1; char str[10]; scanf("%d",&n); while(n) { printf("Case %d:\n",cas++); for(i = 1; i<=n; i++) { scanf("%d",&a[i]); } MEM(c,0); for(i = 1;i<=n;i++) { add(i,a[i]); } while(1) { scanf("%s",str); if(!strcmp(str,"END")) break; if(str[0]=='M') { scanf("%d%d",&x,&y); LL ans = sum(y)-sum(x-1); printf("%lld\n",ans); } else if(str[0]=='S') { scanf("%d%d",&x,&y); add(x,y-a[x]); a[x] = y; } } scanf("%d",&n); if(n) printf("\n"); } return 0; }
时间: 2024-10-23 11:23:04