HDU 1166 敌兵布阵
2012-08-15 20:51
253 查看
线段树单点更新。
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1166
模板题。我写的。
View Code
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1166
模板题。我写的。
View Code
#include <iostream> #include <cstring> #include <cstdio> #define maxn 50007 using namespace std; int a[maxn*4]; int n; void pushup(int rt) { a[rt] = a[rt<<1] + a[rt<<1|1]; } void build(int l,int r,int rt) { if (l == r) { scanf("%d",&a[rt]); return ; } int m = (l + r)>>1; build(l,m,rt<<1); build(m + 1,r,rt<<1|1); pushup(rt); } void update(int l,int r,int pos,int sc,int rt) { if (l == r) { a[rt] += sc; return ; } int m = (l + r)>>1; if (pos <= m) update(l,m,pos,sc,rt<<1); else update(m + 1,r,pos,sc,rt<<1|1); pushup(rt); } int query(int l,int r,int L,int R,int rt) { if (l >= L && r <= R) { return a[rt]; } int m = (l + r)>>1; int res = 0; if (L <= m) res += query(l,m,L,R,rt<<1); if (R > m) res += query(m + 1,r,L,R,rt<<1|1); return res; } int main() { //freopen("in.txt","r",stdin); int t,b,c,cas = 1; char op[10]; scanf("%d",&t); while (t--) { printf("Case %d:\n",cas++); scanf("%d",&n); build(0,n - 1,1); while (scanf("%s",op)) { if (op[0] == 'E') break; scanf("%d%d",&b,&c); if (op[0] == 'A') { update(0,n - 1,b - 1,c,1); } else if (op[0] == 'S') { update(0,n - 1,b - 1,-c,1); } else { int ans = query(0,n - 1,b - 1,c - 1,1); printf("%d\n",ans); } } } return 0; }
相关文章推荐
- 线段树入门[单点更新] 敌兵布阵 HDU 1166
- HDU-1166-敌兵布阵
- HDU 1166 敌兵布阵(线段树或数状数组)
- HDU 1166 敌兵布阵 树状数组解法
- HDU 1166 敌兵布阵【线段树,树状数组入门题,单点更新,区间求和】
- hdu 1166 敌兵布阵(我的第一道树形数组)
- HDU 1166 敌兵布阵 (线段树 插点问线)
- hdu 1166 敌兵布阵(树状数组)
- HDU 1166 敌兵布阵 (数状数组,或线段树)
- hdu 1166 敌兵布阵(树状数组)
- 【HDU】1166 敌兵布阵
- HDU 1166 敌兵布阵
- hdu 1166 敌兵布阵(树状数组 or 线段树)
- 树状数组浅谈 HDU 1166 敌兵布阵
- HDU 1166 敌兵布阵【线段树】
- HDU 1166 - 敌兵布阵(线段树)
- hdu 1166 敌兵布阵【线段树】单点更新,区间求和
- HDU - 1166 A - 敌兵布阵 线段树单点更新模板
- [ACM] hdu 1166 敌兵布阵 (线段树,单点更新)
- 敌兵布阵 - HDU 1166 单点更新线段树