Codeforces Round #337 (Div. 2) D (矩形面积并
来源:互联网 发布:sql server和mysql区别 编辑:程序博客网 时间:2024/04/28 13:28
链接:戳这里
D. Vika and Segments
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).
题意:
矩形面积求并
思路:
前面的文章有分析
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<list>#include<stack>#include<iomanip>#include<cmath>#include<bitset>#define mst(ss,b) memset((ss),(b),sizeof(ss))///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef long double ld;#define INF (1ll<<60)-1#define Max 1e9using namespace std;int n,m;struct Line{ ll x1,x2,y; int f; Line(ll x1=0,ll x2=0,ll y=0,int f=0):x1(x1),x2(x2),y(y),f(f){} bool operator < (const Line &a)const{ if(y==a.y) return f>a.f; return y<a.y; }}L[500100];ll X[500100];ll sum[800100];int tr[800100];void build(int root,int l,int r){ if(l==r){ tr[root]=0; sum[root]=0LL; return ; } int mid=(l+r)/2; build(root*2,l,mid); build(root*2+1,mid+1,r); sum[root]=sum[root*2]+sum[root*2+1];}void pushup(int root,int l,int r){ if(tr[root]) sum[root]=X[r+1]-X[l]; else if(l==r) sum[root]=0; else sum[root]=sum[root*2]+sum[root*2+1];}void update(int root,int l,int r,int x,int y,int v){ if(x<=l && y>=r){ tr[root]+=v; pushup(root,l,r); return ; } int mid=(l+r)/2; if(y<=mid) update(root*2,l,mid,x,y,v); else if(x>mid) update(root*2+1,mid+1,r,x,y,v); else { update(root*2,l,mid,x,mid,v); update(root*2+1,mid+1,r,mid+1,y,v); } pushup(root,l,r);}int main(){ ll x1,x2,y1,y2; scanf("%d",&n);m=0; for(int i=1;i<=n;i++){ scanf("%I64d%I64d%I64d%I64d",&x1,&y1,&x2,&y2); if(x1>x2) swap(x1,x2); if(y1>y2) swap(y1,y2); x2++;y2++; L[++m]=Line(x1,x2,y1,1); X[m]=x1; L[++m]=Line(x1,x2,y2,-1); X[m]=x2; } sort(L+1,L+m+1); sort(X+1,X+m+1); int cnt=unique(X+1,X+m+1)-(X+1); build(1,1,cnt); ll ans=0; int l=lower_bound(X+1,X+cnt+1,L[1].x1)-X; int r=lower_bound(X+1,X+cnt+1,L[1].x2)-X-1; update(1,1,cnt,l,r,L[1].f); for(int i=2;i<=m;i++){ ans+=(L[i].y-L[i-1].y)*sum[1]; l=lower_bound(X+1,X+cnt+1,L[i].x1)-X; r=lower_bound(X+1,X+cnt+1,L[i].x2)-X-1; if(l<=r) update(1,1,cnt,l,r,L[i].f); } printf("%I64d\n",ans); return 0;}
0 0
- Codeforces Round #337 (Div. 2) D (矩形面积并
- Codeforces 610D 矩形面积并
- Codeforces Round #218 (Div. 2)---D. Vessels(并查集)
- Codeforces Round #297 (Div. 2) -- D. Arthur and Walls (判断矩形)
- Codeforces Round #371 (Div. 2) D. Searching Rectangles 平面矩形二分法、交互题
- 【Codeforces Round 330 (Div 2)E】【贪心 暴力】Edo and Magnets 给定矩形最多去除m个,最小面积矩形使得覆盖所有小矩形重心
- Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 开关--并查集
- CF Round#396D (Div. 2)(Codeforces 766D) 简单并查集+map
- Codeforces Round #337 (Div. 2) D.Vika and Segments
- Codeforces Round #337 (Div. 2) D. Vika and Segments
- Codeforces Round #103 (Div. 2) D
- Codeforces Round #104 (Div. 2) D
- Codeforces Round #105 (Div. 2) D
- Codeforces Round #139 (Div. 2) D. Snake
- Codeforces Round #155 (Div. 2) D-rats
- Codeforces Round #159 (Div. 2) D sum
- Codeforces Round #184 (Div. 2) D、E
- Codeforces Round#186(Div 2) D
- 树状数组题目---HDU 1541 stars 及其变形(降维思想)
- iOS coreBluetooth编程中需要注意的问题
- 神奇的备忘录
- Python读取某个目录下的zip压缩包解压开后计算每个小文件的md5值,并将压缩包名字、里面小文件名字、以及对应的md5值写入csv文件
- Android 如何让EditText不自动获取焦点
- Codeforces Round #337 (Div. 2) D (矩形面积并
- java中String类为什么要设计成不可变的
- 机器人差速驱动方式(Differential Drive)
- 深入ArrayList和LinkedList
- 八皇后问题:DFS剪枝
- 你是否应该使用一个Javascript MVC框架?
- Wolf and Rabbit
- 线段树--区间更新区间查询--hdu4027
- 计算机社会学