hdu 1754 I Hate It 线段树 单点更新

来源:互联网 发布:淘宝层级和排名 编辑:程序博客网 时间:2024/05/17 01:58
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define N 200001int maxn[N<<2];int x;void build(int l,int r,int rt){if(l==r){scanf("%d",&x);maxn[rt]=x;return;}int mid=(l+r)>>1;build(l,mid,rt<<1);build(mid+1,r,rt<<1|1);maxn[rt]=max(maxn[rt<<1],maxn[rt<<1|1]);}void update(int l,int r,int pos,int x,int rt){if(l==r){maxn[rt]=x;return;}int mid=(l+r)>>1;if(pos<=mid) update(l,mid,pos,x,rt<<1);else update(mid+1,r,pos,x,rt<<1|1);maxn[rt]=max(maxn[rt<<1],maxn[rt<<1|1]);}int query(int l,int r,int st,int ed,int rt){if(l>=st&&r<=ed){return maxn[rt];}int mid=(l+r)>>1;int temp=0;if(st<=mid) temp=max(temp,query(l,mid,st,ed,rt<<1));if(ed>mid) temp=max(temp,query(mid+1,r,st,ed,rt<<1|1));return temp;}int main(){int n,m;while(scanf("%d%d",&n,&m)!=EOF){build(1,n,1);char ch[3];int r,c;while(m--){scanf("%s%d%d",&ch,&r,&c);if(ch[0]=='U') update(1,n,r,c,1);else printf("%d\n",query(1,n,r,c,1));}}return 0;}
裸裸的线段树。。
0 0