Codeforces Round #337 (Div. 2) D. Vika and Segments

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 drewn black horizontal and vertical segments parallel to the coordinate axes. All segments have width equal to1 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 integersx1, y1, x2 andy2 ( - 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).


分析:拆点然后扫描线法。线段树维护。

#include <cstdio>
#include <unordered_map>
#include <algorithm>
#include <iostream>
#define MAXN 2147483647;
using namespace std;
struct point
{
	int x,y;
} a,b;
struct line
{
	int f,l,r;
	long long h;
} yline[200100];
struct Segtree
{
	int l,r,cnt;
	long long cover;
} tree[800400];
int num,tot,n,xdis[200100],width[200100];
long long ans;
unordered_map <int ,int > Find;
bool camp(line a,line b)
{
	return a.h < b.h;
}
void build(int i,int l,int r)
{
	tree[i].l=l;
	tree[i].r=r;
	tree[i].cnt=0;
	if(l == r) return;
	int mid=(l+r)>>1;
	build(2*i,l,mid);
	build(2*i+1,mid+1,r);
}
void deal(int i,int l,int r,int f)
{
	if(l == tree[i].l && r == tree[i].r)
	{
		tree[i].cnt+=f;
		if(tree[i].cnt == 0)
		{
		    if(l != r) tree[i].cover=tree[i*2].cover+tree[i*2+1].cover;
		    else tree[i].cover=0;
		}
		else tree[i].cover=width[tree[i].r+1]-width[tree[i].l];
		return;
	}
	int mid=(tree[i].l+tree[i].r)>>1;
	if(r <= mid)
	{
		deal(2*i,l,r,f);
	}
	else if(l <= mid)
		 {
		  	deal(2*i,l,mid,f);
			deal(2*i+1,mid+1,r,f);
     	 }
     	 else deal(2*i+1,l,r,f);
    if(tree[i].cnt == 0)
	{
		tree[i].cover=tree[i*2].cover+tree[i*2+1].cover;
	}
	else tree[i].cover=width[tree[i].r+1]-width[tree[i].l];
}
int main()
{
	cin.sync_with_stdio(false);
	cin>>n;
	for(int i=1;i <= n;i++)
	{
		cin>>a.x>>a.y>>b.x>>b.y;
		if(a.x > b.x || a.y > b.y) swap(a,b);
		b.x++,b.y++;
		xdis[++tot]=a.x,yline[tot].f=1,yline[tot].h=a.y,yline[tot].l=a.x,yline[tot].r=b.x;
		xdis[++tot]=b.x;yline[tot].f=-1,yline[tot].h=b.y,yline[tot].l=a.x,yline[tot].r=b.x;
	}
	sort(xdis+1,xdis+1+2*n);
	sort(yline+1,yline+1+2*n,camp);
	xdis[0]=-MAXN;
	for(int i=1;i <= 2*n;i++)
	 if(xdis[i] != xdis[i-1])
	 {
	 	Find[xdis[i]]=++num;
		width[num]=xdis[i];
	 }
	build(1,1,num-1);
	for(int i=1;i < 2*n;i++)
	{
		int l=Find[yline[i].l],r=Find[yline[i].r]-1;
		deal(1,l,r,yline[i].f);
		ans+=1ll*tree[1].cover*(yline[i+1].h-yline[i].h);
	}
	cout<<ans<<endl;
 }



你可能感兴趣的:(线段树,扫描线)