标签:color 最大 include 查询 print src std play ret
1.洛谷1531
点修改,查询区间最大值
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #define ls (cur<<1) 5 #define rs (cur<<1|1) 6 #define mid ((a[cur].l+a[cur].r)>>1) 7 #define len(x) (a[x].r-a[x].l+1) 8 using namespace std; 9 int n,m,k,x,y; 10 char c; 11 struct tree{int l,r,max;}a[800010]; 12 void read(int &k){ 13 k=0; int f=1; char c=getchar(); 14 while(c<‘0‘||c>‘9‘)c==‘-‘&&(f=-1),c=getchar(); 15 while(‘0‘<=c&&c<=‘9‘)k=k*10+c-‘0‘,c=getchar(); 16 k*=f; 17 } 18 void pushup(int cur){a[cur].max=max(a[ls].max,a[rs].max);} 19 void build(int cur,int l,int r){ 20 a[cur].l=l; a[cur].r=r; 21 if(l<r) build(ls,l,mid),build(rs,mid+1,r),pushup(cur); 22 else read(a[cur].max); 23 } 24 void update(int cur,int pos,int x){ 25 if(len(cur)==1&&a[cur].l==pos) a[cur].max=max(a[cur].max,x); 26 else update(pos<=mid?ls:rs,pos,x),pushup(cur); 27 } 28 int query(int cur,int l,int r){ 29 if(l<=a[cur].l&&a[cur].r<=r) return a[cur].max; 30 int ret=-0X7f7f7f; 31 if(l<=mid) ret=query(ls,l,r); 32 if(r>mid) ret=max(ret,query(rs,l,r)); 33 return ret; 34 } 35 int main(){ 36 read(n); read(m); build(1,1,n); 37 for (int i=1;i<=m;i++){ 38 while (c!=‘Q‘&&c!=‘U‘) c=getchar(); 39 read(x); read(y); 40 if(c==‘Q‘) printf("%d\n",query(1,x,y)); 41 else update(1,x,y); 42 } 43 return 0; 44 }
标签:color 最大 include 查询 print src std play ret
原文地址:http://www.cnblogs.com/DriverLao/p/7944159.html