标签:
//hdu1754 #include<stdio.h> #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 //注意因为这里右边是+1的,所以在下面更新以及查询时要注意是否要加=号!!! #define maxn 200020 int sum[maxn<<2],a[maxn]; int max(int x,int y) { return x>y?x:y; } void PushUP(int rt) { sum[rt] = max(sum[rt<<1],sum[rt<<1|1]); } void build(int l,int r,int rt)//建树 { if(l == r) { sum[rt]=a[l]; return; } int m = (l + r)>>1; build(lson); build(rson); PushUP(rt); } void Updata(int i,int B,int l,int r,int rt)//更新 { if(l == r) { sum[rt]=B; return ; } int m = (l + r)/2; if(m < i) Updata(i,B,rson); else Updata(i,B,lson); PushUP(rt); } int Query(int L,int R,int l,int r,int rt)//查询 { if(L <= l&&R >= r) return sum[rt]; int m = (l + r)/2; int ret=-9999999; if(m < L) ret = max(ret,Query(L,R,rson)); else if(m >= R) ret = max(ret,Query(L,R,lson)); else { ret = max(ret,Query(L,R,lson)); ret = max(ret,Query(L,R,rson)); } return ret; } int main() { int n,m,i; char s[3]; while(scanf("%d%d",&n,&m)!=EOF) { for(i = 1; i <= n; i++) scanf("%d",&a[i]); build(1,n,1); int a,b; for(i = 0; i < m; i++) { scanf("%s",s); scanf("%d%d",&a,&b); if(s[0] == ‘U‘) Updata(a,b,1,n,1); else printf("%d\n",Query(a,b,1,n,1)); } } }
hdu1166
//hdu1166 #include<stdio.h> #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define maxn 50505 int sum[maxn<<2]; void PushUP(int rt) { sum[rt] = sum[rt<<1] + sum[rt<<1|1]; } void build(int l,int r,int rt) { if(l == r) { scanf("%d",&sum[rt]); return; } int m = (l + r)/2; build(lson); build(rson); PushUP(rt); } void Updata(int p,int add,int l,int r,int rt) { if(l == r) { sum[rt] += add; return ; } int m = (l + r)/2; if(p <= m) Updata(p,add,lson); else Updata(p,add,rson); PushUP(rt); } int Query(int L,int R,int l,int r,int rt) { if(L <= l&&r <= R) return sum[rt]; int ret = 0; int m = (l + r)/2; if(L > m) ret += Query(L,R,rson); else if(m >= R) ret += Query(L,R,lson); else { ret += Query(L,R,lson); ret += Query(L,R,rson); } return ret; } int main() { int T , ff = 0 , n; scanf("%d",&T); while(T--) { printf("Case %d:\n",++ff); scanf("%d",&n); build(1,n,1); char s[10]; while(scanf("%s",s)!=EOF) { if(s[0]==‘E‘) break; int a,b; scanf("%d%d",&a,&b); if(s[0]==‘A‘) Updata(a,b,1,n,1); else if(s[0]==‘S‘) Updata(a,-b,1,n,1); else if(s[0]==‘Q‘) printf("%d\n",Query(a,b,1,n,1)); } } }
标签:
原文地址:http://www.cnblogs.com/sweat123/p/4624221.html