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 <stdio.h> #include <string.h> #include <algorithm> using namespace std; typedef struct { int l,r,num; }Tree; Tree tree[1000005]; char str[10]; void Build(int t,int l,int r) { int mid; tree[t].l=l; tree[t].r=r; if (l==r) { scanf("%d",&tree[t].num); return; } mid=(l+r)/2; Build(2*t+1,l,mid); Build(2*t+2,mid+1,r); tree[t].num=max(tree[2*t+1].num,tree[2*t+2].num); } void Add(int t,int x,int y) { int l,r,mid; l=tree[t].l; r=tree[t].r; if (l==r) { tree[t].num=y; return; } mid=(l+r)/2; if (x<=mid) Add(2*t+1,x,y); if (x>mid) Add(2*t+2,x,y); tree[t].num=max(tree[2*t+1].num,tree[2*t+2].num); } int Find(int t,int x,int y) { int l,r,mid,ans; l=tree[t].l; r=tree[t].r; if (l==x && r==y) return tree[t].num; mid=(l+r)/2; ans=-1; if (x<=mid) ans=max(ans,Find(2*t+1,x,min(y,mid))); if (y>mid) ans=max(ans,Find(2*t+2,max(x,mid+1),y)); return ans; } int main() { int i,j,n,m,x,y; while(scanf("%d%d",&n,&m)!=EOF) { Build(0,0,n-1); while(m--) { scanf("%s",str); scanf("%d%d",&x,&y); if (str[0]=='Q') printf("%d\n",Find(0,x-1,y-1)); else Add(0,x-1,y); } } return 0; }