标签:
Description
Input
Output
Sample Input
Sample Output
Hint
Huge input,the C function scanf() will work better than cin
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int maxn=210000; int maxv[maxn*4]; #define mid (L+R)/2 #define lson rt*2,L,mid #define rson rt*2+1,mid+1,R void PushUP(int x){ maxv[x]=max(maxv[x*2],maxv[x*2+1]); } void build(int rt,int L,int R){ if(L==R){ scanf("%d",&maxv[rt]); }else{ build(lson); build(rson); PushUP(rt); } } void update(int rt,int L,int R,int x,int v){ if(L==R){ maxv[rt]=v; return ; } if(mid>=x) update(lson,x,v); else update(rson,x,v); PushUP(rt); } int query(int rt,int L,int R,int lp,int rp){ if(lp<=L&&R<=rp){ return maxv[rt]; } int ret=0; if(lp<=mid){ ret=max(ret,query(lson,lp,rp)); } if(rp>mid){ ret=max(ret,query(rson,lp,rp)); } return ret; } int main(){ int n,m; char tms[5]; while(scanf("%d%d",&n,&m)!=EOF){ memset(maxv,0,sizeof(maxv)); build(1,1,n); for(int i=0;i<m;i++){ int a,b,ans; scanf("%s%d%d",tms,&a,&b); if(tms[0]==‘Q‘){ ans=query(1,1,n,a,b); printf("%d\n",ans); }else{ update(1,1,n,a,b); } } } return 0; }
HDU 1754——I Hate It——————【线段树单点替换、区间求最大值】
标签:
原文地址:http://www.cnblogs.com/chengsheng/p/4391609.html