• Vika and Segments


    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.

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

    Input

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

    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.

    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.

    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).

    简单题意

    给你很多条与坐标轴平行的线段,求线段覆盖的点数是多少

    胡说题解

    首先先分成两类,平行x轴的和平行y轴的线段,然后排序,再合并线段,使得相同类型的线段没有交集,然后计算ans(这个时候还没完,因为横纵相交的点没有去掉

    然后我们要计算横纵相交的点数

    然后这是比较经典的双关键字的限制的求和了,可以用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
    ",ans);
    122     return 0;
    123 }
    AC代码
  • 相关阅读:
    【记】移动布局
    初次运行 Git 前的配置
    作为Web开发人员,我为什么喜欢Google Chrome浏览器
    使用CSS样式,制作六边形
    使元素相对于窗口或父元素水平垂直居中的几种方法
    angular: $http.get(...).success is not a function 错误解决
    Git和GitHub的区别及相关术语解析
    前端程序员必知:单页面应用的核心
    css超出一行添加省略号属性
    vue中的json数据报错
  • 原文地址:https://www.cnblogs.com/Randolph87/p/5199350.html
Copyright © 2020-2023  润新知