标签:des style blog color java os strong io
Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5262 Accepted Submission(s): 1317
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <vector> 5 #include <iostream> 6 using namespace std; 7 8 struct mem{ 9 int l, r, maxh; 10 }; 11 12 struct node{ 13 mem b[4444]; 14 int l, r; 15 }a[888]; 16 17 void sub_build(int l,int r,int roota,int rootb){ 18 a[roota].b[rootb].l=l; 19 a[roota].b[rootb].r=r; 20 a[roota].b[rootb].maxh=-1; 21 if(l==r) return; 22 int mid=(l+r)/2; 23 sub_build(l,mid,roota,rootb<<1); 24 sub_build(mid+1,r,roota,rootb<<1|1); 25 } 26 27 void build(int la,int ra,int lb,int rb,int root){ 28 sub_build(lb,rb,root,1); 29 a[root].l=la; 30 a[root].r=ra; 31 if(la==ra) return; 32 int mid=(la+ra)/2; 33 build(la,mid,lb,rb,root<<1); 34 build(mid+1,ra,lb,rb,root<<1|1); 35 } 36 37 void sub_update(int p,int val,int roota,int rootb){ 38 if(a[roota].b[rootb].l==p&&a[roota].b[rootb].r==p){ 39 a[roota].b[rootb].maxh=max(a[roota].b[rootb].maxh,val);return; 40 } 41 int mid=(a[roota].b[rootb].l+a[roota].b[rootb].r)/2; 42 if(p<=mid) sub_update(p,val,roota,rootb<<1); 43 else sub_update(p,val,roota,rootb<<1|1); 44 a[roota].b[rootb].maxh=max(a[roota].b[rootb<<1].maxh,a[roota].b[rootb<<1|1].maxh); 45 } 46 47 void update(int p1,int p2,int val,int root){ 48 sub_update(p2,val,root,1); 49 if(a[root].l==p1&&a[root].r==p1){ 50 51 return; 52 } 53 int mid=(a[root].l+a[root].r)/2; 54 if(p1<=mid) update(p1,p2,val,root<<1); 55 else update(p1,p2,val,root<<1|1); 56 } 57 58 int sub_query(int l,int r,int roota,int rootb){ 59 if(a[roota].b[rootb].l==l&&a[roota].b[rootb].r==r){ 60 return a[roota].b[rootb].maxh; 61 } 62 int mid=(a[roota].b[rootb].l+a[roota].b[rootb].r)/2; 63 if(r<=mid) return sub_query(l,r,roota,rootb<<1); 64 else if(l>mid) return sub_query(l,r,roota,rootb<<1|1); 65 else return max(sub_query(l,mid,roota,rootb<<1),sub_query(mid+1,r,roota,rootb<<1|1)); 66 } 67 68 int query(int h1,int h2,int a1,int a2,int root){ 69 70 if(a[root].l==h1&&a[root].r==h2){ 71 return sub_query(a1,a2,root,1); 72 } 73 int mid=(a[root].l+a[root].r)/2; 74 if(h2<=mid) return query(h1,h2,a1,a2,root<<1); 75 else if(h1>mid) return query(h1,h2,a1,a2,root<<1|1); 76 else return max(query(h1,mid,a1,a2,root<<1),query(mid+1,h2,a1,a2,root<<1|1)); 77 } 78 79 main() 80 { 81 int n, i, j, k; 82 int h1, h2; 83 double a1, a2, l; 84 char c[10]; 85 int ans; 86 while(scanf("%d",&n)==1&&n){ 87 build(100,200,0,1000,1); 88 while(n--){ 89 scanf("%s",c); 90 if(strcmp(c,"I")==0){ 91 scanf("%d %lf %lf",&h1,&a1,&l); 92 update(h1,a1*10,l*10,1); 93 94 } 95 else{ 96 scanf("%d %d %lf %lf",&h1,&h2,&a1,&a2); 97 if(h1>h2) swap(h1,h2); 98 if(a1>a2) swap(a1,a2); 99 ans=query(h1,h2,a1*10,a2*10,1); 100 if(ans<0) printf("-1\n"); 101 else printf("%.1lf\n",ans/10.0); 102 } 103 } 104 } 105 }
HDU 1823 二维线段树(区间max),布布扣,bubuko.com
标签:des style blog color java os strong io
原文地址:http://www.cnblogs.com/qq1012662902/p/3889849.html