您的位置:首页 > 其它

HDU 1166 敌兵布阵

2012-08-15 20:51 253 查看
线段树单点更新。

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