标签:des style blog http color os
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<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=200005; int n,m; int tree[maxn<<2]; void pushup(int rs)//向上更新,也可写在各个函数里 { tree[rs]=max(tree[rs<<1],tree[rs<<1|1]); } void build(int rs,int l,int r) { if(l==r) { scanf("%d",&tree[rs]); return ; } int mid=(l+r)>>1; build(rs<<1,l,mid); build(rs<<1|1,mid+1,r); pushup(rs); } void update(int rs,int l,int r,int x,int y) { if(l==r) { tree[rs]=y; return ; } int mid=(l+r)>>1; if(x<=mid) update(rs<<1,l,mid,x,y);//注意此处的<=,和mid的取值对应 else update(rs<<1|1,mid+1,r,x,y); pushup(rs); } int query(int rs,int l,int r,int x,int y) { if(l==x&&r==y) return tree[rs]; int mid=(l+r)>>1; int re=0; if(y<=mid) return query(rs<<1,l,mid,x,y);//注意此处的<=,和mid的取值对应 else if(x>mid) return query(rs<<1|1,mid+1,r,x,y); else { re=max(re,query(rs<<1,l,mid,x,mid)); re=max(re,query(rs<<1|1,mid+1,r,mid+1,y)); return re; } return 0; } int main() { char c[2]; int x,y; while(~scanf("%d%d",&n,&m)) { getchar(); build(1,1,n); for(int i=0;i<m;i++) { scanf("%s%d%d",c,&x,&y); if(c[0]=='Q') cout<<query(1,1,n,x,y)<<endl; else update(1,1,n,x,y); } } return 0; }没注意mid,debug了=-=
HDU 1754 I Hate It(线段树),布布扣,bubuko.com
标签:des style blog http color os
原文地址:http://blog.csdn.net/u013582254/article/details/38031821