time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

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.

Examples

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

【题目链接】:http://codeforces.com/contest/610/problem/D

【题解】



给你n条横线和纵线;

让你求这些线覆盖的点的面积(线上的一个点覆盖的面积为1);

最后面积不能重复;

做法:

只要把右上角的横纵坐标都加1;

就转化为扫描线求并矩形的面积问题了;

要把横坐标离散化下;

具体扫描线求并矩形面积请看这篇文章

http://blog.csdn.net/harlow_cheng/article/details/53027415



【完整代码】↓↓↓

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long using namespace std; const int MAXN = 1e5+100;
const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0);
struct abc
{
LL l,r,h;
int k;
}; int n,num = 0,cnt[MAXN<<3];
LL sum[MAXN<<3];
LL a1,b1,a2,b2;
abc bian[MAXN*2];
vector <LL> a; void read2(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t) && t!='-') t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} void read1(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)&&t!='-') t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} bool cmp(abc a,abc b)
{
return a.h<b.h;
} void push_up(int rt,int l,int r)
{
if (cnt[rt])
sum[rt]=a[r+1]-a[l];
else
if (l==r)
sum[rt] = 0;
else
sum[rt] = sum[rt<<1]+sum[rt<<1|1];
} void up_data(int L,int R,int c,int l,int r,int rt)
{
if (L<=l && r<=R)
{
cnt[rt]+=c;
push_up(rt,l,r);
return;
}
int m = (l+r)>>1;
if (L <= m)
up_data(L,R,c,lson);
if (m < R)
up_data(L,R,c,rson);
push_up(rt,l,r);
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
read1(n);
for (int i = 1;i <= n;i++)
{
read2(a1);read2(b1);read2(a2);read2(b2);
if (a1>a2)
swap(a1,a2);
if (b1>b2)
swap(b1,b2);
a2++;b2++;
a.push_back(a1);a.push_back(a2);
bian[++num].l = a1,bian[num].r = a2,bian[num].h = b1,bian[num].k = 1;
bian[++num].l = a1,bian[num].r = a2,bian[num].h = b2,bian[num].k = -1;
}
sort(a.begin(),a.end());
a.erase(unique(a.begin(),a.end()),a.end());
sort(bian+1,bian+1+num,cmp);
LL ans = 0;
for (int i = 1;i <= num-1;i++)
{
int l = lower_bound(a.begin(),a.end(),bian[i].l)-a.begin();
int r = lower_bound(a.begin(),a.end(),bian[i].r)-a.begin()-1;
up_data(l,r,bian[i].k,0,a.size()-1,1);
ans += sum[1]*(bian[i+1].h-bian[i].h);
}
cout << ans<<endl;
return 0;
}

【20.51%】【codeforces 610D】Vika and Segments的更多相关文章

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

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

  2. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  3. Codeforces Round #337 Vika and Segments

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

  4. 【51.27%】【codeforces 604A】Uncowed Forces

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. 【20.23%】【codeforces 740A】Alyona and copybooks

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 【76.83%】【codeforces 554A】Kyoya and Photobooks

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

  7. 【codeforces 515C】Drazil and Factorial

    [题目链接]:http://codeforces.com/contest/515/problem/C [题意] 定义f(n)=n这个数各个位置上的数的阶乘的乘积; 给你a; 让你另外求一个不含0和1的 ...

  8. 【codeforces 766E】Mahmoud and a xor trip

    [题目链接]:http://codeforces.com/contest/766/problem/E [题意] 定义树上任意两点之间的距离为这条简单路径上经过的点; 那些点上的权值的所有异或; 求任意 ...

  9. 【codeforces 797E】Array Queries

    [题目链接]:http://codeforces.com/problemset/problem/797/E [题意] 给你一个n个元素的数组; 每个元素都在1..n之间; 然后给你q个询问; 每个询问 ...

随机推荐

  1. linux之架构图和八台服务器

    (1) (2)

  2. iOS 警告收录及科学快速的消除方法

    http://www.cocoachina.com/ios/20150914/13287.html 作者:董铂然 授权本站转载. 前言:现在你维护的项目有多少警告?看着几百条警告觉得心里烦么?你真的觉 ...

  3. Effective C++: 03资源管理

    所谓资源,就是一旦用了它,将来必须还给系统.C++中的资源有:内存.文件描述符.互斥锁.数据库连接.网络socket等. 13:以对象管理资源 1:像下面这个函数: void f() { Invest ...

  4. VirtualBox使用随笔

    1.virtualbox配置Android手机USB热点 host:Windows10;guest:windows XP/10 右击我的电脑 - 管理 - 设备管理器 - 网卡适配器,若手机正确vb连 ...

  5. Android 高仿微信语音聊天页面高斯模糊效果

    目前的应用市场上,使用毛玻璃效果的APP随处可见,比如用过微信语音聊天的人可以发现,语音聊天页面就使用了高斯模糊效果. 先看下效果图: 仔细观察上图,我们可以发现,背景图以用户头像为模板,对其进行了高 ...

  6. UVa-10986_Sending email (向前星+Dijkstra)

    题意:给你点.边,求起点到终点的最短距离. 题解:由于题目的数据量特别大,所以需要用邻接表来存边,之后对Dijkstra算法稍微魔改一下就可以了,本来以为会超时,做好了打堆优化的准备,结果卡时间过了, ...

  7. python 检测目录

    #!/usr/bin/env python# -*- coding:utf-8 -*-import osimport win32fileimport win32con ACTIONS = { 1 : ...

  8. pytorch旧版安装

    https://pytorch.org/get-started/previous-versions 可以直接下载文件 用 pip 直接在下载目录安装就可以了

  9. BERT大火却不懂Transformer?读这一篇就够了

    https://zhuanlan.zhihu.com/p/54356280 大数据文摘与百度NLP联合出品 编译:张驰.毅航.Conrad.龙心尘 来源:https://jalammar.github ...

  10. Fish Shell使用心得

    Fish的官网宣传语是 Finally, a command line shell for the 90s. 翻译过来就是 Fish shell 是一个为90后准备的 shell. 有人说:" ...