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;
}

最新文章

  1. UVA 1349(二分图匹配)
  2. Nginx + tornado + supervisor部署
  3. Angular框架
  4. Cocos2d-x中使用OpenGL ES2.0编写shader
  5. 做个伪IE浏览器?!【来自官网】
  6. 【js类库Rapha&#235;l】基于svg中的path画40%表示的环型图
  7. iOS学习之视图加载过程中会触发的方法(loadView/viewDidLoad/didReceiveMemoryWarning)
  8. HDOJ-1016 Prime Ring Problem(DFS)
  9. [转]Libev教程
  10. centos7 最小安装无ifconfig
  11. 算法-java代码实现快速排序
  12. C语言作业(三)
  13. MQTT再学习 -- MQTT 客户端源码分析
  14. [03] mapper.xml的基本元素概述
  15. Bitmap BitmapData
  16. Win8交互UX——鼠标交互
  17. 2018C语言第二次作业
  18. CSS3 媒体查询@media 查询(响应式布局)
  19. English trip -- VC(情景课)5 B Places around town 城市设施
  20. 在控制台连接oracle

热门文章

  1. 杨柳目-杨柳科-Info-新闻:杨柳絮造成的危害相关视频资讯
  2. 2019-9-2-win10-uwp-右击浮出窗在点击位置
  3. IO流之字符流-1
  4. @atcoder - AGC035E@ Develop
  5. javascript内置函数
  6. ArcGIS中的连接和关联表
  7. oralce函数 next_day(d1[,c1])
  8. @bzoj - 2388@ 旅行规划
  9. HZOJ 矩阵游戏
  10. @noi.ac - 171@ 立方体