+ Scan line segment tree NAIPC 2019 Intersecting Rectangles

How you look at your dish, use a small little tree line, they will not write;

Meaning of the questions: If the matrix intersect, output 1, otherwise the output 0 (does not include embedded);

Ideas: The intersection of questions, the situation does not have to include built-in;

Have done is find the area of ​​a problem. This is similar with the road, but the way defined here with that question given opposite.

Here the following line to set -1, set to 1 above;

In this question, the first lateral side of the matrix by two kinds stored in a vertical structure, the value of 1 on the right, at -1;

Then discrete coordinates (floating point numbers when too much time in several discrete, is too large tree line does not fit)

Therefore discretized with x1, x2 to update the segment tree;

When the bottom side is updated when the (right -1) if this time interval in this already has a value, prove the intersection;

When the upper edge is updated when (weight of 1) If the time interval where the value does not become 0, proved intersection;

  1 #include<cstdio>
  2 #include<algorithm>
  3 #include<math.h>
  4 #include<string.h>
  5 using namespace std;
  6 const int maxn=2e5+10;
  7 int cnt1;
  8 int cnt2,c[maxn];
  9 struct node
 10 {
 11     int l,r,h,id;
 12 }b[maxn];
 13 struct Node
 14 {
 15     int l,r,sum;
 16 }tree[maxn<<2];
 17 void init()
 18 {
 19     memset(c,0,sizeof(c));
 20     cnt1=cnt2=0;
 21 }
 22 bool cmp(node x,node y)
 23 {
 24     return x.h<y.h;
 25 }
 26 void build(int l,int r,int root)
 27 {
 28     tree[root].l=l,tree[root].r=r;
 29     tree[root].sum=0;
 30     if(l==r) return;
 31     int mid=l+r>>1;
 32     build(l,mid,root<<1);
 33     build(mid+1,r,root<<1|1);
 34 }
 35 void pushup(int root)
 36 {
 37     tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum;
 38 }
 39 void update(int base,int key,int root)
 40 {
 41     int l=tree[root].l,r=tree[root].r;
 42     if(l==r){
 43         tree[root].sum+=key;
 44         return;
 45     }
 46     int mid=l+r>>1;
 47     if(mid>=base) update(base,key,root<<1);
 48     else       update(base,key,root<<1|1);
 49     pushup(root);
 50 }
 51 int query(int L,int R,int root)
 52 {
 53     int l=tree[root].l,r=tree[root].r;
 54     if(l>=L&&r<=R){
 55         return tree[root].sum;
 56     }
 57     int mid=l+r>>1;
 58     int ans=0;
 59     if(mid>=L) ans+=query(L,R,root<<1);
 60     if(mid<R)  ans+=query(L,R,root<<1|1);
 61     return ans;
 62 }
 63 int main()
 64 {
 65     int n;
 66     while(scanf("%d",&n)!=EOF){
 67         init();
 68         for(int i=1;i<=n;i++){
 69             int x1,x2,y1,y2;
 70             scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
 71             B [++ CNT1] .L = X1, B [CNT1] .r = X2, B [CNT1] .h = Y1, B [CNT1] .id = - . 1 ;   // lower; 
72              B [++ CNT1] X1 = .L, B [CNT1] .r = X2, B [CNT1] .h = Y2, B [CNT1] .id = . 1 ;    // upper; 
73 is              C [CNT2 ++] = X1;      // discretization pretreatment coordinates; 
74              C [CNT2 ++] = X2;      // discretization coordinate preprocessing; 
75          }
 76          Sort (C + . 1 , C + . 1 + CNT2);    // discretization 
77          int len = UNIQUE (C + . 1 , C + . 1 + CNT2) -C- . 1 ;
 78          for( Int I = . 1 ; I <= CNT1; I ++ ) {
 79              B [I] .L = lower_bound (C + . 1 , C + . 1 ; len +, B [I] .L) -C    // value of the discretized stored 
80              B [I] .r = lower_bound (C + . 1 , C + . 1 + len, B [I] .r) - C;
 81          }
 82          Sort (B + . 1 , B + . 1 + CNT1, CMP);    // ascending sorting operations herein do not clear, but after knowing this way
 83                                    // to step by step to judge whether or not the intersection; 
84          Build ( . 1 , CNT1, . 1 );           // contribution; 
85         int flag=0;
 86         for(int i=1;i<=cnt1;i++){
 87             if(b[i].id==-1){      //如果为下边
 88                 int tmp=query(b[i].l,b[i].r,1);
 89                 if(tmp){
 90                     flag=1;
 91                     break;
 92                 }
 93             }
 94             update(b[i].l,b[i].id,1);
 95             update(b[i].r,b[i].id,1);
 96             if(b[i].id==1){
 97                 int tmp=query(b[i].l,b[i].r,1);
 98                 if(tmp){
 99                     flag=1;
100                     break;
101                 }
102             }
103         }
104         printf("%d\n",flag);
105     }
106     return 0;
107 }

 

Guess you like

Origin www.cnblogs.com/pangbi/p/11622287.html