您的位置:首页 > 其它

hdu 1754 也是裸的线段树

2013-01-09 15:05 218 查看
我知道了 while(~scanf("%d%d",&n,&m)) 等价于 while(scanf("%d%d",&n,&m)!=EOF)

是等价的。

而且以后用c语言输入时候的scanf("%c“,&c),这种要注意到回车空格神马的。

#include <stdio.h>
#include <algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define maxn 880000
using namespace std;
int sum[maxn];
void PushUp(int rt)
{
sum[rt]=max(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 c,int l,int r,int rt)
{
if(l==r)
{
sum[rt]=c;
return;
}
int m=(l+r)>>1;
if(p<=m)
update(p,c,lson);
else
update(p,c,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 res=0;
if(L<=m)
res=max(res,query(L,R,lson));
if(R>m)
res=max(res,query(L,R,rson));
return res;
}
int main()
{
int m,n,i,j;
char c[10];
int a,b;
while(~scanf("%d%d",&n,&m))
{
build(1,n,1);
while(m--)
{
scanf("%s%d%d",c,&a,&b);
if(c[0]=='Q')
printf("%d\n",query(a,b,1,n,1));
else
update(a,b,1,n,1);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: