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.

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

30 1 2 11 4 1 20 3 2 3

Output

8

Input

4-2 -1 2 -12 1 -2 1-1 -2 -1 21 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\n",ans);
122     return 0;
123 }

AC代码

时间: 2024-10-04 22:51:26

Vika and Segments - CF610D的相关文章

Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并

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

CodeForces 610D Vika and Segments

题目链接: http://codeforces.com/problemset/problem/610/D ------------------------------------------------------------------------------------ 虽然说这题是线段并 但是如果会写矩形面积并的话就直接写矩形并不用考虑那么多了 不过这里额外说明下 写矩形面积并的线段树的时候 要注意到某一段 $ -1 $一定是在这一段$ +1 $之后才会出现的操作 因此标记就是这一段被“完

Codeforces 610D Vika and Segments 线段树+离散化+扫描线

可以转变成上一题(hdu1542)的形式,把每条线段变成宽为1的矩形,求矩形面积并 要注意的就是转化为右下角的点需要x+1,y-1,画一条线就能看出来了 #include<bits/stdc++.h> #define pi acos(-1.0) #define ll long long #define mod 1000000007 #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 #pragma comment(linker,

610D - Vika and Segments(线段树+扫描线+离散化)

扫描线:http://www.cnblogs.com/scau20110726/archive/2013/04/12/3016765.html 看图,图中的数字是横坐标离散后对应的下标,计算时左端点不变,右端点加1,所以总的更新的区间是l到r-1. 也可以理解为1代表的是(1到2这一段),2代表的是(2到3这一段),3代表的是(3到4这一段)... 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #de

Codeforces Round #337 (Div. 2)

水 A - Pasha and Stick #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e5 + 5; const int INF = 0x3f3f3f3f; int main(void) { int n; scanf ("%d", &n); int ans = n / 4; if (n % 4 == 0) { ans--; } if (n %

线段树离散化

主要思路是在离散化前把右端点++, 离散化后右端点-1. 1. CF 610D Vika and Segments 大意: 给定$n$条与坐标轴平行的线段, 求一共占了多少点 #include <iostream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #

POJ 1436 Horizontally Visible Segments(线段树)

POJ 1436 Horizontally Visible Segments 题目链接 线段树处理染色问题,把线段排序,从左往右扫描处理出每个线段能看到的右边的线段,然后利用bitset维护枚举两个线段,找出另一个两个都有的线段 代码: #include <cstdio> #include <cstring> #include <algorithm> #include <bitset> #include <vector> using namesp

ACM ——Points in Segments

Given n points (1 dimensional) and q segments, you have to find the number of points that lie in each of the segments. A point pi will lie in a segment A B if A ≤ pi ≤ B. For example if the points are 1, 4, 6, 8, 10. And the segment is 0 to 5. Then t

每天一道LeetCode--434. Number of Segments in a String

Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters. For example, Input: "Hello, my name is John" Output: 5 public int countSegments(String s) { String trimmed = s.trim(); if (