hdu 1166 敌兵布阵
2014-01-03 19:38
393 查看
线段树单点更新。
单点增减 区间求和。
(刚刚开始学线段树T。T....)
单点增减 区间求和。
(刚刚开始学线段树T。T....)
#include<cstdio> #include<iostream> #define lson l , m , rt<<1 #define rson m+1 , r , rt<<1|1 const int maxn = 55555; 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)>>1; build(lson); build(rson); PushUP(rt); } void Update(int p,int add ,int l,int r ,int rt ) { if(l == r) { sum[rt]+=add; return; } int m=(l+r)>>1; if(p<=m) Update(p,add,lson); else Update(p,add,rson); PushUP(rt); } int Queue(int L,int R,int l,int r,int rt) { if(L<=l && r<=R) return sum[rt]; int res=0; int m=(l+r)>>1; if(L <= m) res += Queue(L, R, lson); if(R > m) res += Queue(L, R, rson); return res; } int main() { int T,n; scanf("%d",&T); for(int cas = 1;cas <= T; cas++) { printf("Case %d:\n",cas); scanf("%d",&n); build(1,n,1); char ask[20]; while(scanf("%s",ask)) { if(ask[0]=='E') break; int a,b; scanf("%d%d",&a,&b); if(ask[0]=='Q') printf("%d\n",Queue(a,b,1,n,1)); else if(ask[0]=='A') Update(a,b,1,n,1); else Update(a,-b,1,n,1); } } return 0; }
相关文章推荐
- HDU1166敌兵布阵
- HDU 1166 敌兵布阵 (线段树基础&树状数组基础)
- HDU 1166 敌兵布阵
- hdu 1166 敌兵布阵(线段树模板题)
- HDU 1166 敌兵布阵 【树状数组】
- HDU 1166 敌兵布阵
- 线段树系列-hdu-1166-敌兵布阵-单点修改区间求和
- hdu 1166 敌兵布阵
- hdu 1166 敌兵布阵 单点更新模板
- hdu 1166 敌兵布阵
- hdu 1166 敌兵布阵
- HDU1166-线段树模版-敌兵布阵
- HDU 1166 敌兵布阵
- hdu 1166 敌兵布阵 单点更新+区间查询 segment tree or BIT
- (hdu 1166)敌兵布阵(线段树入门,单点更新)
- HDU 1166 敌兵布阵
- HDU-1166 敌兵布阵 (裸线段树算法)
- HDU 1166 敌兵布阵(线段树)
- hdu 1166 敌兵布阵(线段树入门题)
- 敌兵布阵 HDU - 1166 (线段树单点更新)