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代码

最新文章

  1. GRE与Vxlan网络详解
  2. 深入剖析Nginx一点小笔记
  3. 连接到kali linux服务器上的MySQL服务器错误
  4. 利用powerdesigner反向数据库结构,生成ER图
  5. C#设计模式(23种设计模式)
  6. JavaScript 常见陷阱
  7. 兼容Firefox和IE的onpropertychange事件oninput
  8. 安卓巴士android源码、博文精选1
  9. ffplay.c函数结构简单分析(画图)
  10. JAVA_内部类
  11. Spring学习心得--------bean-Factory
  12. 第一册:lesson ninety-seven。
  13. JavaScript中防止重复提交
  14. 如何在github上搭建网站?
  15. CentOS 7 MariaDB-MHA
  16. 你不可不知的Java引用类型之——软引用
  17. GUI概述与Frame演示
  18. Maven 系列 二 :Maven 常用命令,手动创建第一个 Maven 项目
  19. Centos7 Mysql 双机热备实现数据库高可用
  20. WP8.1学习系列(第六章)——中心控件Hub面板部分交互UX

热门文章

  1. Ceres优化
  2. Weka java.lang.reflect.InvocationTargetException
  3. nginx支持php配置
  4. logstash-input-jdbc and logstash-ouput-jdbc
  5. TPO-15 C1 The campus newspaper&#39;s reporter position
  6. flume 整合 kafka
  7. Android开发-API指南-&lt;supports-screens&gt;
  8. Sharepoint 2013与Sharepoint 2016的功能对比
  9. 【模板】DFS
  10. 机器学习(四)正则化与过拟合问题 Regularization / The Problem of Overfitting