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
线段树,感觉比树状数组复杂多了,不过总算能轻松点AC模板题了,1200MS很无奈,希望以后会优化吧
#include<iostream> #include<cstring> #include<cstdio> #define M 500010 using namespace std; int n,m; struct H { int mi,ma,sum,l,r; }trees[M]; int num[M]; void build_trees(int jd,int l,int r) { trees[jd].l=l;trees[jd].r=r; if(l==r) {trees[jd].ma=num[l];return ;} int mid=(l+r)/2; build_trees(jd*2,l,mid); build_trees(jd*2+1,mid+1,r); trees[jd].ma=max(trees[jd*2].ma,trees[jd*2+1].ma); } int query(int jd,int l,int r) { int ans=0; if(l<=trees[jd].l&&r>=trees[jd].r) return trees[jd].ma; int mid=(trees[jd].l+trees[jd].r)/2; if(l<=mid) ans=max(ans,query(jd*2,l,r)); if(r>mid) ans=max(ans,query(jd*2+1,l,r)); return ans; } void update(int jd,int l,int a) { if(trees[jd].l==trees[jd].r) {trees[jd].ma=a;return;} int mid = (trees[jd].l+trees[jd].r)/2; if(l<=mid) update(jd*2,l,a); else update(jd*2+1,l,a); trees[jd].ma=max(trees[jd*2].ma,trees[jd*2+1].ma); } int main() { int i,j; int a,b; char c[1]; while(~scanf("%d%d",&n,&m)) { memset(num,0,sizeof num); for(i=1;i<=n;i++) scanf("%d",&num[i]); build_trees(1,1,n); for(i=1;i<=m;i++) { scanf("%s%d%d",c,&a,&b); if(c[0]=='Q') { printf("%d\n",query(1,a,b)); } if(c[0]=='U') { update(1,a,b); } } } return 0; }
HDU 1754 I Hate It,布布扣,bubuko.com
原文:http://blog.csdn.net/hanhai768/article/details/37885337