Description
Input
Output
Sample Input
5 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5
Sample Output
5 6 5 9
Hint
Huge input,the C function scanf() will work better than cin
#include<iostream> #include<cstdio> #include<cstring> using namespace std; #define maxn 200005 #define inf 0x3f3f3f3f int sum[maxn<<2],ll[maxn<<2],rr[maxn<<2]; int a[maxn]; inline void pushup(int i){ sum[i]=max(sum[i<<1],sum[(i<<1)|1]); } void build(int l,int r,int i){ ll[i]=l; rr[i]=r; if(l==r){ sum[i]=a[l]; return; } int m=(ll[i]+rr[i])>>1,ls=i<<1,rs=ls|1; build(l,m,ls); build(m+1,r,rs); pushup(i); } void update(int k,int v,int i){ if(ll[i]==rr[i]){ sum[i]=v; return ; } int m=(ll[i]+rr[i])>>1,ls=i<<1,rs=ls|1; if(k<=m)update(k,v,ls); else update(k,v,rs); pushup(i); } int query(int l,int r,int i){ if(l<=ll[i]&&rr[i]<=r){ return sum[i]; } int m=(ll[i]+rr[i])>>1,ls=i<<1,rs=ls|1; int ans=0; if(l<=m)ans=max(query(l,r,ls),ans); if(m<r)ans=max(query(l,r,rs),ans); return ans; } int main() { int n,m; int u,v; //freopen("in.txt","r",stdin); while(~scanf("%d%d",&n,&m)){ for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } build(1,n,1); char q[2]; while(m--){ scanf("%s %d %d",q,&u,&v); //用c会出问题 if(q[0]=='Q'){ printf("%d\n",query(u,v,1)); } else { update(u,v,1); } } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u013497977/article/details/47012523