您的位置:首页 > 其它

hdu 1166 临兵布阵 线段树[hh]

2012-03-12 09:15 225 查看
题意:很清晰。。。不解释。

昨天学习了下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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: