您的位置:首页 > 其它

hdu 1754 I Hate It

2016-01-21 22:10 302 查看
AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

const int N = 200005;

struct node{
int l,r,maxn;
}tree[N<<2];
int a
;

void build(int m,int l,int r){
tree[m].l = l;
tree[m].r = r;
if(l == r){
tree[m].maxn = a[l];
return ;
}
int mid = (l+r)>>1;
build(m<<1,l,mid);
build((m<<1)|1,mid+1,r);
tree[m].maxn = max(tree[m<<1].maxn,tree[(m<<1)|1].maxn);
}

void update(int m,int x,int val){
if(tree[m].l == x && tree[m].r == x){
tree[m].maxn = val;
return ;
}
int mid = (tree[m].l+tree[m].r)>>1;
if(x <= mid)
update(m<<1,x,val);
else
update((m<<1)|1,x,val);
tree[m].maxn = max(tree[m<<1].maxn,tree[(m<<1)|1].maxn);
}

int query(int m,int l,int r){
if(tree[m].l == l && tree[m].r == r)
return tree[m].maxn;
int mid = (tree[m].l+tree[m].r)>>1;
if(r <= mid)
return query(m<<1,l,r);
if(l > mid)
return query((m<<1)|1,l,r);
return max(query(m<<1,l,mid),query((m<<1)|1,mid+1,r));
}

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