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. mySql——case when else ....demo

    DROP PROCEDURE IF EXISTS Pro_query_change_charge_by_layer_report; CREATE PROCEDURE Pro_query_change_ ...

  2. Android7.0 应用内升级

    Android7.0应用内升级 最近线上项目在7.0机器上出现应用内升级失败,原来是由于Android7.0权限问题导致. 如果项目的 targetSdkVersion>=24 在处理应用内升级 ...

  3. spark history server

    参考:http://blog.csdn.net/lsshlsw/article/details/44786575 为什么需要historyServer? 在运行Spark Application的时候 ...

  4. 第七模块:项目实战一 第1章 项目实战:CRM客户关系管理系统开发

    01-crm介绍 02-权限系统介绍 03-第一版表结构设计 04-第二版表结构设计 05-orm中创建表结构 06-销售管理系统业务 07-销售管理系统权限信息录入 08-快速实现简单的权限控制的设 ...

  5. 韦大仙--python对文件操作

    文件操作: 对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 现有文件如下 Somehow, it seems the love I knew was alwa ...

  6. 【cover-view、cover-image】 覆盖组件说明

    cover-view.cover-image 这两类覆盖组件用于显示在一些特殊组件上方(map.video.canvas.camera.live-player.live-pusher). 这类组件一般 ...

  7. 【view】 视图组件说明

    view 是视图容器,可用于包裹其它组件或文本内容. 原型: <view hover-class="[String]" hover-stop-propagation=&quo ...

  8. Ducci序列 (Ducci Sequence,ACM/ICPC Seoul 2009,UVa1594)

    题目描述: 题目思路: 直接模拟 #include<stdio.h> #include<string.h> #define maxn 105 int less(const ch ...

  9. LeetCode 104——二叉树中的最大深度

    1. 题目 2. 解答 如果根节点为空,直接返回 0.如果根节点非空,递归得到其左右子树的深度,树的深度就为左右子树深度的最大值加 1. /** * Definition for a binary t ...

  10. 2019-1-92.4G射频芯片培训资料

    2019-1-92.4G射频芯片培训资料 培训 RF 小书匠  欢迎走进zozo的学习之旅. 2.4G芯片选型 2.4G芯片开发 Q&A 2.4G芯片选型 芯片类型 soc 防盗标签2.4G无 ...