标签:
Description
Input
Output
Sample Input
1 10 1 2 3 4 5 6 7 8 9 10 Query 1 3 Add 3 6 Query 2 7 Sub 10 2 Add 6 3 Query 3 10 End
Sample Output
Case 1: 6 33 59
#include <iostream> #include<cstdio> #include<cstring> using namespace std; int t,n; int b[50005*4]; //tle或者re全错在数组大小要N*4 void build(int node,int left,int right) { if (left==right) {scanf("%d",&b[node]); return;} build(node*2,left,(left+right)/2); build(node*2+1,(left+right)/2+1,right); b[node]=b[node*2]+b[node*2+1]; return; } void add(int x,int y,int left,int right,int node) { if(left==right) {b[node]+=y; return;} int mid=(left+right)/2; if (x<=mid) add(x,y,left,mid,node*2); else add(x,y,mid+1,right,node*2+1); //b[node]+=y; b[node]=b[node*2]+b[node*2+1]; } int query(int x,int y,int left,int right,int node) { if (x<=left && y>=right) return b[node]; int mid=(left+right)/2; int ans=0; if (x<=mid) ans+=query(x,y,left,mid,node*2); if (y>mid) ans+=query(x,y,mid+1,right,node*2+1); return ans; } int main() { scanf("%d",&t); for(int ii=1;ii<=t;ii++) { printf("Case %d:\n",ii); scanf("%d",&n); //for(int i=1;i<=n;i++) scanf("%d",&a[i]); //memset(b,-1,sizeof(b)); build(1,1,n); char ch[10]; while(scanf("%s",&ch) && ch[0]!=‘E‘) { int x,y; scanf("%d%d",&x,&y); if (ch[0]==‘Q‘) printf("%d\n",query(x,y,1,n,1)); if (ch[0]==‘A‘) add(x,y,1,n,1); if (ch[0]==‘S‘) add(x,-y,1,n,1); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/stepping/p/5877544.html