标签:线段树
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 38359 Accepted Submission(s): 15178
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
本来看到找最大直接用rmq的,一写发现rmq更新耗时太大了。。。。。用线段树模板稍微改一下就过了,把和改成最大就行
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #define maxn 200001 using namespace std; const int MX=200001; int n,t; int tree[MX*4]; int max1(int a,int b) { return a>b?a:b; } void up(int index) { tree[index]=max1(tree[index<<1],tree[index<<1|1]); } void create(int index,int l,int r) { if(l==r) { scanf("%d",&tree[index]);return; } int mind=(l+r)/2; create(index<<1,l,mind); create(index<<1|1,mind+1,r); up(index); } int getse(int index,int s,int e,int l,int r) { if(s<=l&&r<=e) return tree[index]; int ans=0; int mind=(r+l)/2; if(s<=mind) ans=max1(ans,getse(index<<1,s,e,l,mind)); if(e>mind) ans=max1(ans,getse(index<<1|1,s,e,mind+1,r)); return ans; } void update(int index,int p,int v,int l,int r) { if(l==r){ tree[index]=max1(tree[index],v);return; } int mind=(r+l)/2; if(p<=mind) update(index<<1,p,v,l,mind); else update(index<<1|1,p,v,mind+1,r); up(index); } int main() { while(scanf("%d%d",&n,&t)!=EOF) { create(1,1,n); int a,b; char g; while(t--) { getchar(); scanf("%c%d%d",&g,&a,&b); if(g=='Q') { printf("%d\n", getse(1,a,b,1,n)); }else{ update(1,a,b,1,n); } } } return 0; }
HDU1754I Hate It,布布扣,bubuko.com
标签:线段树
原文地址:http://blog.csdn.net/fljssj/article/details/38470821