您的位置:首页 > 其它

HDU 1754 I Hate It

2016-04-30 11:09 363 查看
线段树入门

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define lson rt<<1
#define rson (rt<<1)+1
using namespace std;
int a[1000001];
char b[1000001];
void build(int l,int r,int rt)
{
if(l==r)
{
scanf("%d",&a[rt]);
return;
}
int mid=(l+r)>>1;
build(l,mid,lson);
build(mid+1,r,rson);
a[rt]=max(a[lson],a[rson]);
}
void update(int l,int r,int rt,int pos,int value)
{
if(l==r)
{
a[rt]=value;
return;
}
int mid=(l+r)/2;
if(pos<=mid)
update(l,mid,lson,pos,value);
else
update(mid+1,r,rson,pos,value);
a[rt]=max(a[lson],a[rson]);
}
int query(int l,int r,int rt,int L,int R)
{
int ma=0;
if(L<=l&&r<=R)
{
ma=max(a[lson],a[rson]);
return a[rt];
}
int mid=(l+r)>>1;
if(L<=mid)
ma=max(ma,query(l,mid,lson,L,R));
if(R>mid)
ma=max(ma,query(mid+1,r,rson,L,R));
return ma;
}

int main()
{
int i,j,k,n,m,l,x,y;
while(~scanf("%d %d",&n,&m))
{
build(1,n,1);
//printf("(%d)\n",a[2]);
for(i=0;i<m;i++)
{
scanf("%s %d %d",b,&x,&y);
if(b[0]=='Q')
{
printf("%d\n",query(1,n,1,x,y));
}
else if(b[0]=='U')
{
//  printf("(1)");
update(1,n,1,x,y);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: