HDU 1754
选最大值。线段树。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string.h> #include <queue> #include <cmath> #include <map> #include <vector> #define LL __int64 using namespace std; const int N=200010; const int root=1; struct Node{ int val,left,right; }tree[N*4+100]; int n,m; int score[N],pos[N]; void build(int rt,int l,int r){ if(l==r){ tree[rt].val=score[l]; tree[rt].left=l; tree[rt].right=r; pos[l]=rt; return; } tree[rt].left=l; tree[rt].right=r; build(rt*2,l,(r+l)/2); build(rt*2+1,(r+l)/2+1,r); tree[rt].val=max(tree[rt*2].val,tree[rt*2+1].val); } int query(int now,int l,int r){ int L=tree[now].left ,R=tree[now].right; if(l<=L&&r>=R){ return tree[now].val; } int m=(L+R)/2; if(l>=m+1){ return query(now*2+1,l,r); } else if(r<=m) return query(now*2,l,r); else{ return max(query(now*2,l,r),query(now*2+1,l,r)); } } void update(int now,int s){ tree[now].val=s; now/=2; while(now>0){ tree[now].val=max(tree[now*2].val,tree[now*2+1].val); now/=2; } } int main(){ int p,q; char act; while(scanf("%d%d",&n,&m)!=EOF){ for(int i=1;i<=n;i++) scanf("%d",&score[i]); getchar(); build(root,1,n); for(int i=1;i<=m;i++){ scanf("%c%d%d",&act,&p,&q); getchar(); // cout<<act<<p<<q<<endl; if(act=='Q'){ printf("%d\n",query(root,p,q)); } else{ p=pos[p]; update(p,q); } } } return 0; }