Vika and Segments - CF610D

Vika has an infinite sheet of squared paper. Initially all squares are white. She introduced a two-dimensional coordinate system on this sheet and drew n black horizontal and vertical segments parallel to the coordinate axes. All segments have width equal to 1 square, that means every segment occupy some set of neighbouring squares situated in one row or one column.ide

Your task is to calculate the number of painted cells. If a cell was painted more than once, it should be calculated exactly once.this

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of segments drawn by Vika.spa

Each of the next n lines contains four integers x1, y1, x2 and y2 ( - 109 ≤ x1, y1, x2, y2 ≤ 109) — the coordinates of the endpoints of the segments drawn by Vika. It is guaranteed that all the segments are parallel to coordinate axes. Segments may touch, overlap and even completely coincide.code

Output

Print the number of cells painted by Vika. If a cell was painted more than once, it should be calculated exactly once in the answer.blog

Sample test(s)
Input
3
0 1 2 1
1 4 1 2
0 3 2 3
Output
8
Input
4
-2 -1 2 -1
2 1 -2 1
-1 -2 -1 2
1 2 1 -2
Output
16
Note

In the first sample Vika will paint squares (0, 1), (1, 1), (2, 1), (1, 2), (1, 3), (1, 4), (0, 3) and (2, 3).排序

簡單題意ci

給你不少條與座標軸平行的線段,求線段覆蓋的點數是多少input

胡說題解it

首先先分紅兩類,平行x軸的和平行y軸的線段,而後排序,再合併線段,使得相同類型的線段沒有交集,而後計算ans(這個時候還沒完,由於橫縱相交的點沒有去掉io

而後咱們要計算橫縱相交的點數

而後這是比較經典的雙關鍵字的限制的求和了,能夠用cdq分治,或者排序按序加入而後維護區間和之類的

腦殘錯誤

一開始RE幾發,最後查出緣由是由於sort的cmp沒打好,不能判斷出來相等(a<b是true,b<a也是true)而後就鬼畜了,因此打cmp的時候正確的姿式是每一個關鍵字都要比較(AC代碼裏面並無徹底改過來,懶。。。

  1 #include<cstdio>
  2 #include<algorithm>
  3 #include<cmath>
  4 using namespace std;
  5 
  6 struct point{
  7     bool q;
  8     int h,d,l,r;
  9 };
 10 
 11 const int maxn=100100;
 12 
 13 int n,s[maxn*2],x[maxn*2],tot;
 14 point a[maxn*3];
 15 long long ans;
 16 
 17 bool compare(point a,point b){
 18     if(a.q^b.q)return a.q;
 19     if(a.q){
 20         if(a.l!=b.l)return a.l<b.l;
 21         if(a.d!=b.d)return a.d<b.d;
 22         return a.h<b.h;
 23     }
 24     else{
 25         if(a.d!=b.d)return a.d<b.d;
 26         if(a.l!=b.l)return a.l<b.l;
 27         return a.r<b.r;
 28     }
 29 }
 30 
 31 bool cmp2(point a,point b){
 32     if(a.h!=b.h)return a.h>b.h;
 33     if(a.q^b.q)return a.q>b.q;
 34     return a.l<b.l;
 35 }
 36 
 37 int find(int i){
 38     int l=1,r=tot,mid;
 39     while(l!=r){
 40         mid=(l+r)/2;
 41         if(x[mid]>=i)r=mid;
 42         else l=mid+1;
 43     }
 44     return l;
 45 }
 46 
 47 int lowbit(int x){
 48     return x&-x;
 49 }
 50 
 51 int sum(int x){
 52     int ss=0;
 53     while(x>0){
 54         ss+=s[x];
 55         x-=lowbit(x);
 56     }
 57     return ss;
 58 }
 59 
 60 void add(int x,int y){
 61     while(x<=tot){
 62         s[x]+=y;
 63         x+=lowbit(x);
 64     }
 65 }
 66 
 67 int main(){
 68     scanf("%d",&n);
 69     int i;
 70     for(i=1;i<=n;i++){
 71         scanf("%d%d%d%d",&a[i].l,&a[i].h,&a[i].r,&a[i].d);
 72         if(a[i].r<a[i].l)swap(a[i].l,a[i].r);
 73         if(a[i].h<a[i].d)swap(a[i].h,a[i].d);
 74         if(a[i].l==a[i].r)a[i].q=true;
 75     }
 76     sort(a+1,a+1+n,compare);
 77     for(i=1;i<n;i++)
 78     if(a[i].q==a[i+1].q){
 79         if(a[i].q){
 80             if(a[i].l==a[i+1].l)
 81             if(a[i+1].d<=a[i].h+1){
 82                 a[i+1].d=a[i].d;
 83                 a[i+1].h=fmax(a[i+1].h,a[i].h);
 84                 a[i].l=0;a[i].r=-1;
 85             }
 86         }
 87         else{
 88             if(a[i].h==a[i+1].h)
 89             if(a[i+1].l<=a[i].r+1){
 90                 a[i+1].l=a[i].l;
 91                 a[i+1].r=fmax(a[i+1].r,a[i].r);
 92                 a[i].l=0;a[i].r=-1;
 93             }
 94         }
 95     }
 96     for(i=1;i<=n;i++)ans+=(a[i].r-a[i].l+1)*(a[i].h-a[i].d+1);
 97     for(i=1;i<=n;i++)
 98     if(a[i].l<=a[i].r)x[++tot]=a[i].l,x[++tot]=a[i].r;
 99     sort(x+1,x+1+tot);
100     int tmp=n;
101     for(i=1;i<=tmp;i++)if(a[i].q && a[i].l<=a[i].r){
102         ++n;
103         a[n].q=true;
104         a[n].h=a[i].h;
105         a[n].d=a[i].l;
106         a[n].l=1;a[n].r=1;
107         ++n;
108         a[n].q=true;
109         a[n].h=a[i].d-1;
110         a[n].d=a[i].l;
111         a[n].l=-1;
112         a[i].l=0;a[i].r=-1;
113     }
114     sort(a+1,a+1+n,cmp2);
115     for(i=1;i<=n;i++){
116         if(a[i].l<=a[i].r){
117             if(a[i].q)add(find(a[i].d),a[i].l);
118             else ans-=sum(find(a[i].r))-sum(find(a[i].l)-1);
119         }
120     }
121     printf("%I64d\n",ans);
122     return 0;
123 }
AC代碼
相關文章
相關標籤/搜索