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
5 6 5 9HintHuge input,the C function scanf() will work better than cin
#include <iostream> #include <stdio.h> #include <string> #include <cstring> #include <algorithm> #include <cmath> #define ll __int64 using namespace std; ll a[200009*4]; //大约是4倍点的空间 void buildtree(int le,int ri,int num) { if(le==ri) { scanf("%I64d",&a[num]); return; } int mid=(le+ri)/2; buildtree(le,mid,num*2); buildtree(mid+1,ri,num*2+1); a[num]=max(a[num],a[num*2]); //写法问题,注意将a数组初始化为全0 a[num]=max(a[num],a[num*2+1]); } ll query(int le,int ri,int num,int x,int y) { if(x<=le && y>=ri) { return a[num]; } int mid=(le+ri)/2; ll ans=0; if(x<=mid) ans=max(ans,query(le,mid,num*2,x,y)); if(y>mid) //不可以取等号,因为下次要去的区间是[mid+1,ri],不包括mid这个值 ans=max(ans,query(mid+1,ri,num*2+1,x,y)); return ans; } void add(int le,int ri,int num,int x,int y) { if(le==ri) { a[num]=y; return; } int mid=(le+ri)/2; if(x<=mid) add(le,mid,num*2,x,y); else add(mid+1,ri,num*2+1,x,y); a[num]=max(a[num],a[num*2]); a[num]=max(a[num],a[num*2+1]); } int main() { int n,m; while(~scanf("%d%d",&n,&m)) { memset(a,0,sizeof a); //初始化!!! buildtree(1,n,1); scanf("%d",&m); char s[10]; int x,y; while(m--) { scanf("%s %d %d",s,&x,&y); if(s[0]=='Q') { printf("%I64d\n",query(1,n,1,x,y)); } else { add(1,n,1,x,y); } } } return 0; }