2017ICPC南宁赛区网络赛 Overlapping Rectangles(重叠矩阵面积和=离散化模板)
2017-09-26 13:05
369 查看
There are nnn rectangles on the plane. The problem is to find the area of the union of these rectangles. Note that these rectangles might overlap with each other, and the overlapped areas of these rectangles shall not be counted more than once. For example, given a rectangle AAA with the bottom left corner located at (0,0)(0, 0)(0,0) and the top right corner at (2,2)(2, 2)(2,2), and the other rectangle BBB with the bottom left corner located at (1,1)(1,1)(1,1) and the top right corner at (3,3)(3,3)(3,3), it follows that the area of the union of AAA and BBB should be 777, instead of 888.Although the problem looks simple at the first glance, it might take a while to figure out how to do it correctly. Note that the shape of the union can be very complicated, and the intersected areas can be overlapped by more than two rectangles.Note:(1) The coordinates of these rectangles are given in integers. So you do not have to worry about the floating point round-off errors. However, these integers can be as large as 1,000,0001,000,0001,000,000.(2) To make the problem easier, you do not have to worry about the sum of the areas exceeding the long integer precision. That is, you can assume that the total area does not result in integer overflow.
Input Format
Several sets of rectangles configurations. The inputs are a list of integers. Within each set, the first integer (in a single line) represents the number of rectangles, n, which can be as large as 100010001000. After n, there will be n lines representing the n rectangles; each line contains four integers <a,b,c,d><a, b, c, d><a,b,c,d> , which means that the bottom left corner of the rectangle is located at (a,b)(a, b)(a,b), and the top right corner of the rectangle is located at (c,d)(c, d)(c,d). Note that integers aaa, bbb, ccc, ddd can be as large as 1,000,0001,000,0001,000,000.These configurations of rectangles occur repetitively in the input as the pattern described above. An integer n=0n = 0n=0 (zero) signifies the end of input.Output Format
For each set of the rectangles configurations appeared in the input, calculate the total area of the union of the rectangles. Again, these rectangles might overlap each other, and the intersecting areas of these rectangles can only be counted once. Output a single star '*' to signify the end of outputs.样例输入
2 0 0 2 2 1 1 3 3 3 0 0 1 1 2 2 3 3 4 4 5 5 0
样例输出
7 3 * 直接套模板 参考博客:POJ 1151 Atlantis(重叠矩阵面积和=离散化)
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int maxn=5555; struct Node//矩形 { double x1,y1,x2,y2; }nodes[maxn]; double x[maxn],y[maxn]; bool mp[maxn][maxn]; int find(double *x,double val,int n)//在数组x中找到val值的位置 { int L=0,R=n-1; while(R>=L) { int mid=L+(R-L)/2; if(x[mid]==val) return mid; else if(x[mid]>val) R=mid-1; else L=mid+1; } return -1; } int main() { int n,num1,num2; while(~scanf("%d",&n)) { if(n==0){printf("*\n");break;} num1=num2=0;//num1记录有多少个不同x值,num2记录y的 memset(mp,0,sizeof(mp)); for(int i=0;i<n;++i) { scanf("%lf%lf%lf%lf",&nodes[i].x1,&nodes[i].y1,&nodes[i].x2,&nodes[i].y2); x[num1++]=nodes[i].x1; x[num1++]=nodes[i].x2; y[num2++]=nodes[i].y1; y[num2++]=nodes[i].y2; } sort(x,x+num1); sort(y,y+num2); num1=unique(x,x+num1)-x;//去重 num2=unique(y,y+num2)-y;//去重 for(int i=0;i<n;++i) { //找出第i个原始大矩形覆盖的小矩形范围 int L_x=find(x,nodes[i].x1,num1); int R_x=find(x,nodes[i].x2,num1); int L_y=find(y,nodes[i].y1,num2); int R_y=find(y,nodes[i].y2,num2); for(int j=L_x;j<R_x;++j) for(int k=L_y;k<R_y;++k) mp[j][k]=true; } long long int ans=0; for(int i=0;i<num1;++i) for(int j=0;j<num2;++j)if(mp[i][j]) ans += (x[i+1]-x[i])*(y[j+1]-y[j]); printf("%lld\n",ans); } return 0; }
相关文章推荐
- 2017 icpc 南宁赛区 F.Overlapping Rectangles(重叠矩形的最大面积+线段树模板)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F. Overlapping Rectangles (面积并+线段树)
- POJ 1151 Atlantis(重叠矩阵面积和=离散化)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 B题 离散化+贪心
- 2017ICPC南宁赛区网络赛 Train Seats Reservation (简单思维)
- 2017ICPC南宁赛区网络赛 The Heaviest Non-decreasing Subsequence Problem (最长不下降子序列)
- 2017ICPC南宁赛区网络赛 Minimum Distance in a Star Graph (bfs)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛- J. Minimum Distance in a Star Graph
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J. Minimum Distance in a Star Graph
- 【计算几何】【圆反演】计蒜客17314 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 G. Finding the Radius for an Inserted Circle
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 H A Cache Simulator
- 用线段树求解坐标矩阵中的交并集面积思路及其模板代码
- HDU-1255 覆盖的面积(线段树扫描线模板+离散化+加点修改题)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 L.The Heaviest Non-decreasing Subsequence Problem
- HDU 4291:A Short problem_成都赛区网络赛—矩阵快速幂
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles
- 【2016-沈阳赛区网络赛-J】大素数模板(Count primes,uva 5901)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 L. The Heaviest Non-decreasing Subsequence Problem
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 The Heaviest Non-decreasing Subsequence Problem 最长不下降序列
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Minimum Distance in a Star Graph