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代码里面并没有完全改过来,懒。。。

 #include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std; struct point{
bool q;
int h,d,l,r;
}; const int maxn=; int n,s[maxn*],x[maxn*],tot;
point a[maxn*];
long long ans; bool compare(point a,point b){
if(a.q^b.q)return a.q;
if(a.q){
if(a.l!=b.l)return a.l<b.l;
if(a.d!=b.d)return a.d<b.d;
return a.h<b.h;
}
else{
if(a.d!=b.d)return a.d<b.d;
if(a.l!=b.l)return a.l<b.l;
return a.r<b.r;
}
} bool cmp2(point a,point b){
if(a.h!=b.h)return a.h>b.h;
if(a.q^b.q)return a.q>b.q;
return a.l<b.l;
} int find(int i){
int l=,r=tot,mid;
while(l!=r){
mid=(l+r)/;
if(x[mid]>=i)r=mid;
else l=mid+;
}
return l;
} int lowbit(int x){
return x&-x;
} int sum(int x){
int ss=;
while(x>){
ss+=s[x];
x-=lowbit(x);
}
return ss;
} void add(int x,int y){
while(x<=tot){
s[x]+=y;
x+=lowbit(x);
}
} int main(){
scanf("%d",&n);
int i;
for(i=;i<=n;i++){
scanf("%d%d%d%d",&a[i].l,&a[i].h,&a[i].r,&a[i].d);
if(a[i].r<a[i].l)swap(a[i].l,a[i].r);
if(a[i].h<a[i].d)swap(a[i].h,a[i].d);
if(a[i].l==a[i].r)a[i].q=true;
}
sort(a+,a++n,compare);
for(i=;i<n;i++)
if(a[i].q==a[i+].q){
if(a[i].q){
if(a[i].l==a[i+].l)
if(a[i+].d<=a[i].h+){
a[i+].d=a[i].d;
a[i+].h=fmax(a[i+].h,a[i].h);
a[i].l=;a[i].r=-;
}
}
else{
if(a[i].h==a[i+].h)
if(a[i+].l<=a[i].r+){
a[i+].l=a[i].l;
a[i+].r=fmax(a[i+].r,a[i].r);
a[i].l=;a[i].r=-;
}
}
}
for(i=;i<=n;i++)ans+=(a[i].r-a[i].l+)*(a[i].h-a[i].d+);
for(i=;i<=n;i++)
if(a[i].l<=a[i].r)x[++tot]=a[i].l,x[++tot]=a[i].r;
sort(x+,x++tot);
int tmp=n;
for(i=;i<=tmp;i++)if(a[i].q && a[i].l<=a[i].r){
++n;
a[n].q=true;
a[n].h=a[i].h;
a[n].d=a[i].l;
a[n].l=;a[n].r=;
++n;
a[n].q=true;
a[n].h=a[i].d-;
a[n].d=a[i].l;
a[n].l=-;
a[i].l=;a[i].r=-;
}
sort(a+,a++n,cmp2);
for(i=;i<=n;i++){
if(a[i].l<=a[i].r){
if(a[i].q)add(find(a[i].d),a[i].l);
else ans-=sum(find(a[i].r))-sum(find(a[i].l)-);
}
}
printf("%I64d\n",ans);
return ;
}

AC代码

Vika and Segments - CF610D的更多相关文章

  1. 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 whit ...

  2. Codeforces Round #337 Vika and Segments

    D. Vika and Segments time limit per test:  2 seconds     memory limit per test:  256 megabytes input ...

  3. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线

    D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...

  4. codeforces 610D D. Vika and Segments(离散化+线段树+扫描线算法)

    题目链接: D. Vika and Segments time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  5. 【20.51%】【codeforces 610D】Vika and Segments

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)

    题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...

  7. CodeForces 610D Vika and Segments

    模板题,矩形面积并 #include <iostream> #include <cstring> #include <cstdio> #include <al ...

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

    扫描线:http://www.cnblogs.com/scau20110726/archive/2013/04/12/3016765.html 看图,图中的数字是横坐标离散后对应的下标,计算时左端点不 ...

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

    可以转变成上一题(hdu1542)的形式,把每条线段变成宽为1的矩形,求矩形面积并 要注意的就是转化为右下角的点需要x+1,y-1,画一条线就能看出来了 #include<bits/stdc++ ...

随机推荐

  1. svn 撤销 已提交的修改

    1.保证我们拿到的是最新代码:  svn update  假设最新版本号是28.  2.然后找出要回滚的确切版本号:  svn log [something]  假设根据svn log日志查出要回滚的 ...

  2. hackhttp模板的介绍

    hackhttp模板:造福人类 发起get/post/ 发起http原始数据包 漏洞利用:更为快捷放放不安 #hackhttp使用方法hh=hackhttp.hackhttp() code,head, ...

  3. WEB网站测试心得整理

    一.输入框: 1.正常的字母/文字/数字(正常流程的测试): 2.重复提交(输入内容后,重复点击提交按钮): 3.纯异常字符/正常输入夹杂异常字符(!@#¥%……&**等等): 4.长度限制( ...

  4. MyBatis 注解配置及动态SQL

      一.注解配置 目前MyBatis支持注解配置,用注解方式来替代映射文件,但是注解配置还是有点不完善,在开发中使用比较少,大部分的企业还是在用映射文件来进行配置.不完善的地方体现在于当数据表中的字段 ...

  5. CodeForces 908C. New Year and Curling 解题报告 Java

    1. 思路 这题实际上是个几何问题——两个外相切的圆,由勾股定理,他们的纵坐标有以下的规律: 则有$$y_{n+1} = y_{n} + \sqrt{(2r)^2 - (x_{n} - x_{n+1} ...

  6. [转载]Java集合框架的常见面试题

    http://www.jfox.info/40-ge-java-ji-he-lei-mian-shi-ti-he-da-an 整理自上面链接: Java集合框架为Java编程语言的基础,也是Java面 ...

  7. nodejs笔记--express篇(五)

    创建一个express + ejs的项目 express -e testEjsWebApp cd testEjsWebApp npm install http://localhost:3000 Usa ...

  8. Thunder团队第二周 - Scrum会议5

    Scrum会议5 小组名称:Thunder 项目名称:爱阅app Scrum Master:苗威 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传康 ...

  9. 11.24Daily Scrum(2)

    人员 任务分配完成情况 明天任务分配 王皓南 实现网页上视频浏览的功能.研究相关的代码和功能.996 数据库测试 申开亮 实现网页上视频浏览的功能.研究相关的代码和功能.997 实现视频浏览的功能 王 ...

  10. 20172330 2017-2018-1 《Java程序设计》第八周学习总结

    学号 2017-2018-1 <程序设计与数据结构>第八周学习总结 教材学习内容总结 这一章主要是对多态性的学习: 由继承实现多态性 多态性引用能够随时间变化指向不同类型的对象. 对于多态 ...