HDU 1823 two-dimensional line segment tree (interval max)

Source: Internet
Author: User
Luck and love

Time Limit: 10000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 5262 accepted submission (s): 1317


Problem description the farthest distance in the world Is not separated by the ends of the earth
But I am in front of you
But you don't know that I love you
-Zhang XiaoYu

Some days ago, Feng bingye gave a wedding notice to wiskey, and the number of invitations reached 5 million. Oh, my God, but it's an astronomical number. I don't know how many mm flocked to it, and suddenly there was nothing to do with it, even the aunt who swept the floor came to join in. -_-|
Because of the large number of people, wiskey was too busy to handle all the statistics, and he ran home to rest. This is enough for Feng bingye. He has to handle two kinds of tasks. First, he must accept the registration of mm, the second is to help wiskey find the highest fate among qualified mm.

 

The input question contains multiple test data. The first digit is m, indicating that there are m consecutive operations. When m = 0, the processing is aborted.
Next is a operator C.
When the operator is 'I', it indicates that there is a mm registration, followed by an integer, h indicates height, two floating point numbers, a indicates activity, and l indicates fate. (100 <= H <= 200, 0.0 <= A, L <= 100.0)
When the operator is 'Q', the following four floating point numbers, H1, H2, indicate the height range, A1, A2, and so on, output the highest fate value in mm that meets the height and liveliness requirements. (100 <= H1, H2 <= 200, 0.0 <= A1, A2 <= 100.0)
All input floating point numbers have only one decimal place.

 

For each query operation, the output outputs the highest fate value in one row, keeping a decimal number.
Output-1 for queries that cannot be found.

 

Sample input8i 160 50.5 601_ I 165 30.0 80.5i 166 10.0 501_ I 170 80.5 77.5q 150 166 10.0 601_q 166 177 10.0 501_ I 166 40.0 99.9q 166 177 10.0 50.00

 

Sample output80.550000099.9 writes a two-dimensional line segment tree for the first time. The code can be used by others. In this question, H is the X axis and a is the Y axis, because each height may contain many different, therefore, a line segment tree is nested in each node of a common line segment tree. Code:
  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 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.