标签:des style blog http color os io strong
Description
Input
Output
Sample Input
Sample Output
Hint
Huge input,the C function scanf() will work better than cin
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<string> 5 #include<stdlib.h> 6 #include<algorithm> 7 using namespace std; 8 struct node 9 { 10 int l,r; 11 int num; 12 int mid() 13 { 14 return (l+r)/2; 15 } 16 }a[1000000]; 17 int maxn; 18 19 void btree(int l,int r,int step) 20 { 21 a[step].l=l; 22 a[step].r=r; 23 if(l==r) 24 { 25 scanf("%d",&a[step].num); 26 return ; 27 } 28 int mid=a[step].mid(); 29 btree(l,mid,step*2); 30 btree(mid+1,r,step*2+1); 31 a[step].num=max(a[step*2].num,a[step*2+1].num); 32 } 33 34 void ptree(int step,int vis,int val) 35 { 36 if(a[step].l==a[step].r&&a[step].l==vis) 37 { 38 a[step].num=val; 39 return ; 40 } 41 int mid=a[step].mid(); 42 if(vis>mid) ptree(step*2+1,vis,val); 43 else ptree(step*2,vis,val); 44 a[step].num=max(a[step*2].num,a[step*2+1].num); 45 } 46 47 void maxtree(int l,int r,int step,int L,int R) 48 { 49 if(L<=l&&r<=R) 50 { 51 maxn=max(maxn,a[step].num); 52 return ; 53 } 54 int mid=a[step].mid(); 55 if(L>mid) 56 maxtree(mid+1,r,step*2+1,L,R); 57 else if(R<=mid) 58 maxtree(l,mid,step*2,L,R); 59 else 60 { 61 maxtree(l,mid,step*2,L,R); 62 maxtree(mid+1,r,step*2+1,L,R); 63 } 64 } 65 66 int main() 67 { 68 int n,ope; 69 while(scanf("%d %d",&n,&ope)!=EOF) 70 { 71 btree(1,n,1); 72 while(ope--) 73 { 74 getchar(); 75 char ch; 76 int x,y; 77 scanf("%c %d %d",&ch,&x,&y); 78 if(ch==‘Q‘) 79 { 80 maxn=-1; 81 maxtree(1,n,1,x,y); 82 printf("%d\n",maxn); 83 } 84 if(ch==‘U‘) 85 ptree(1,x,y); 86 } 87 } 88 89 return 0; 90 }
HDU 1754 I Hate It(线段树),布布扣,bubuko.com
标签:des style blog http color os io strong
原文地址:http://www.cnblogs.com/clliff/p/3893563.html