hdu 1166 临兵布阵 线段树[hh]
2012-03-12 09:15
225 查看
题意:很清晰。。。不解释。
昨天学习了下hh的线段树,学完了顿时石化,产生了对我的代码风格无比的鄙视。。 看了hh的线段树,不仅感觉到这个模版灰常的实用美观,而且我也刻意的改变了下我的代码书写的习惯,多加些tab和空格之类的东西,会使你的代码异常美观!!
昨天学习了下hh的线段树,学完了顿时石化,产生了对我的代码风格无比的鄙视。。 看了hh的线段树,不仅感觉到这个模版灰常的实用美观,而且我也刻意的改变了下我的代码书写的习惯,多加些tab和空格之类的东西,会使你的代码异常美观!!
#include<iostream> using namespace std; #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 len,int l,int r,int rt) { if (l == r) { sum[rt] += len; return ; } int m = (l + r) >> 1; if (m >= p) update(p , len , lson); else update(p , len ,rson); 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) >> 1; int ans=0; if (m >= L) ans += query(L , R , lson); if (m < R) ans += query(L , R , rson); return ans; } int main() { int T , N; char s[10]; scanf("%d",&T); for (int t = 1 ; t <= T ; t ++) { printf("Case %d:\n",t); scanf("%d",&N); build(1 , N , 1); int a , b; while (scanf("%s",s)) { if (s[0] == 'E') break; scanf("%d%d",&a,&b); if (s[0] == 'A') { update(a , b, 1 , N , 1); } else if (s[0] == 'Q') { printf("%d\n",query(a , b , 1 , N , 1)); } else if (s[0] == 'S') { update(a, -b , 1 , N , 1); } } } return 0; }
相关文章推荐
- HDU 1166 敌兵布阵(线段树)
- HDU 1166 敌兵布阵 树状数组||线段树
- hdu 1166 敌兵布阵(线段树基础题)
- HDU 1166.敌兵布阵【线段树】【树状数组】【4月27】
- hdu-1166 敌兵布阵(线段树的增删查改)
- HDU 1166 - 敌兵布阵(线段树)
- HDU 1166敌兵布阵-线段树入门
- 线段树单点更新 - HDU 1166 敌兵布阵 1754 I Hate it
- HDU 1166 敌兵布阵(线段树模板)
- hdu 1166 敌兵布阵 线段树 单点更新
- HDU 1166 敌兵布阵 线段树
- hdu1166 敌兵布阵——(线段树模板)
- HDU 1166 敌兵布阵(线段树 or 二叉索引树)
- HDU 1166 敌兵布阵 (树状数组和线段树解法)
- hdu 1166 敌兵布阵--线段树
- 【线段树I:母题】hdu 1166 敌兵布阵
- HDU-1166-敌兵布阵(线段树 单点更新)
- [HDU]HDOJ1166 敌兵布阵 基础线段树
- HDU 1166 敌兵布阵(线段树单点更新)
- HDU-1166- 敌兵布阵(线段树 插点问线)