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 9
#include<iostream> #include<cstring> #include<cstdio> using namespace std; int a[200005]; int p[200005]; int n,m; int lowbit(int x) { return x&(-x); } void build() { int i,j; for(i=1;i<=n;i++) { p[i]=a[i]; for(j=1;j<lowbit(i);j<<=1) { if(p[i]<p[i-j]) p[i]=p[i-j]; } } } void insert(int i,int x) { a[i]=x; while(i<=n) { if(p[i]<x) p[i]=x; else break; i += lowbit(i); } } int query(int l,int r) { int maxn=a[r]; while(true) { if(maxn<a[r]) maxn=a[r]; if(l==r) break; for(r-=1;r-l>=lowbit(r);r-=lowbit(r)) { if(maxn<p[r]) maxn=p[r]; } } return maxn; } int main() { while(~scanf("%d%d",&n,&m)) { memset(p,0,sizeof p); int i; for(i=1;i<=n;i++) scanf("%d",a+i); build(); char c[3]; int x,y; for(i=1;i<=m;i++) { scanf("%s%d%d",c,&x,&y); if(c[0]=='Q') printf("%d\n",query(x,y)); else insert(x,y); } } return 0; }
HDUJ 1754 I Hate It,布布扣,bubuko.com
原文:http://blog.csdn.net/hyccfy/article/details/38420771