Created
March 31, 2015 12:28
-
-
Save lackofdream/91f8ccb592512b1d5487 to your computer and use it in GitHub Desktop.
HDU1166
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdio> | |
#include <cstring> | |
#include <algorithm> | |
#define sd(x) scanf("%d",&x) | |
#define sdd(x,y) scanf("%d%d",&x,&y) | |
#define sddd(x,y,z) scanf("%d%d%d",&x,&y,&z) | |
#define MAX(a,b) (a>b?a:b) | |
#define MIN(a,b) (a<b?a:b) | |
using namespace std; | |
const int N = 200000+5; | |
struct node | |
{ | |
int left,right,cover; | |
} tree[3*N]; | |
void update(int i,int id,int c) | |
{ | |
int mid; | |
if (tree[i].left- tree[i].right==-1) | |
{ | |
tree[i].cover+=c; | |
return; | |
} | |
tree[i].cover+=c; | |
mid = (tree[i].left+tree[i].right)/2; | |
if (mid<=id-1) return update(i*2+1,id,c); | |
else if (mid >=id) return update(i*2,id,c); | |
} | |
int query(int i,int l,int r) | |
{ | |
if (tree[i].left==l && tree[i].right==r) | |
return tree[i].cover; | |
int mid=(tree[i].left+tree[i].right)/2; | |
if (mid>=r) return query(i*2,l,r); | |
else if (mid<=l) return query(i*2+1,l,r); | |
else | |
{ | |
return query(i*2,l,mid)+query(i*2+1,mid,r); | |
} | |
} | |
void build(int i,int l,int r) | |
{ | |
tree[i].cover = 0; | |
tree[i].left=l; | |
tree[i].right=r; | |
if (tree[i].right-tree[i].left==1) return; | |
int mid = (l+r)/2; | |
build(2*i,l,mid); | |
build(2*i+1,mid,r); | |
} | |
int main() | |
{ | |
// freopen("in.txt","r",stdin); | |
// freopen("out.txt","w",stdout); | |
int t,n,q,tcase=0; | |
int a,b,c; | |
sd(t); | |
while(t--) | |
{ | |
printf("Case %d:\n",++tcase); | |
sd(n); | |
getchar(); | |
build(1,0,n); | |
char op[10]; | |
for (int i=1;i<=n;i++) | |
{ | |
sd(q); | |
update(1,i,q); | |
} | |
while (scanf("%s",op)==1) | |
{ | |
if (strcmp(op,"End")==0) break; | |
sdd(a,b); | |
if (strcmp(op,"Add")==0) update(1,a,b); | |
else if (strcmp(op,"Sub")==0) update(1,a,-b); | |
else printf("%d\n",query(1,a-1,b)); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment