您的位置:首页 > 其它

HDU 1754 I hate it

2015-08-24 22:33 351 查看
//线段树,单点更新+区间求和

AC代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int MAX=200005;
int sum[MAX<<2];
char s[8];
void push(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);
push(rt);
}
void updata(int p,int add,int l,int r,int rt)
{
if(l==r)
{
sum[rt]=add;
return;
}
int m=(l+r)>>1;
if(p<=m)
updata(p,add,lson);
else
updata(p,add,rson);
push(rt);
}
int querty(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
return sum[rt];
}
int m=(l+r)>>1;
int t=-1<<29;
if(L<=m)
t=max(t,querty(L,R,lson));
if(R>m)
t=max(t,querty(L,R,rson));
return t;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
build(1,n,1);
int i;
int x,y;
for(i=0;i<m;i++)
{
scanf("%s%d%d",s,&x,&y);
if(s[0]=='Q')
{
printf("%d\n",querty(x,y,1,n,1));
}
if(s[0]=='U')
{
updata(x,y,1,n,1);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: