您的位置:首页 > 其它

hdu 1166 敌兵布阵(线段树基本操作)

2012-08-22 15:20 477 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1166

(1)纯属敲模版,没有什么特别的。错了两次:一次是TLE,原因是输出了不该输出的(测试用的,忘了删掉);另一次WA,原因是没有输出该输出的(cas)。

具体代码:

View Code

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std;
const int N=300000;
int data
, Max[N<<2];
int n, m;
void pushup(int rt)
{
Max[rt]=max(Max[rt<<1], Max[rt<<1|1]);
}
void build(int l, int r, int rt)
{
if(l==r)
{
scanf("%d", &data[l]);
Max[rt]=data[l];
return ;
}
int m=l+r>>1;
build(lson);
build(rson);
pushup(rt);
}
int query(int L, int R, int l, int r, int rt)
{
if(L<=l&&r<=R)
return Max[rt];
int m=l+r>>1;
int ret=0;
if(L<=m) ret=max(ret, query(L, R, lson));
if(R>m) ret=max(ret, query(L, R, rson));
return ret;
}
void update(int p, int val, int l, int r, int rt)
{
if(l==r)
{
data[l]=val;
Max[rt]=data[l];
return ;
}
int m=l+r>>1;
if(p<=m) update(p, val, lson);
else update(p, val, rson);
pushup(rt);
}
int main()
{
int i, j;
int a, b;
char ch;
while(scanf("%d%d", &n, &m)!=EOF)
{
build(1, n, 1);
for(i=1;i<=m;i++)
{
scanf(" %c%d%d", &ch, &a, &b);
if(ch=='Q')
{
printf("%d\n", query(a, b, 1, n, 1));
}
else
{
update(a, b, 1, n, 1);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: