2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles
2017-09-24 20:14
531 查看
There are nn 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 AA with
the bottom left corner located at (0,
0)(0,0) and
the top right corner at (2,
2)(2,2),
and the other rectangle BB with
the bottom left corner located at (1,1)(1,1) and
the top right corner at (3,3)(3,3),
it follows that the area of the union of AA and BB should
be 77,
instead of 88.
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,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.
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 10001000.
After n, there will be n lines representing the n rectangles; each line contains four integers <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),
and the top right corner of the rectangle is located at (c,
d)(c,d).
Note that integers aa, bb, cc, dd can
be as large as 1,000,0001,000,000.
These configurations of rectangles occur repetitively in the input as the pattern described above. An integer n
= 0n=0 (zero)
signifies the end of input.
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.
2017
ACM-ICPC 亚洲区(南宁赛区)网络赛
线段树加扫描线模板题,队友给力!!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define lz 2*u,l,mid
#define rz 2*u+1,mid+1,r
const int maxn=4222;
double sum[maxn];
int flag[maxn];
double X[maxn];
struct Node
{
double lx, rx, y;
int s;
Node(){};
Node(double lx_, double rx_, double y_, int s_)
{
lx=lx_, rx=rx_, y=y_, s=s_;
}
bool operator <(const Node &S) const
{
return y<S.y;
}
}line[maxn];
int find(double tmp, int n)
{
int l=1, r=n, mid;
while(l<=r)
{
mid=(l+r)>>1;
if(X[mid]==tmp) return mid;
else if(X[mid]<tmp) l=mid+1;
else r=mid-1;
}
}
void push_up(int u, int l, int r)
{
if(flag[u]) sum[u]=X[r+1]-X[l];
else if(l==r) sum[u]=0;
else sum[u]=sum[2*u]+sum[2*u+1];
}
void Update(int u, int l, int r, int tl, int tr, int c)
{
if(tl<=l&&r<=tr)
{
flag[u]+=c;
push_up(u,l,r);
return ;
}
int mid=(l+r)>>1;
if(tr<=mid) Update(lz,tl,tr,c);
else if(tl>mid) Update(rz,tl,tr,c);
else
{
Update(lz,tl,mid,c);
Update(rz,mid+1,tr,c);
}
push_up(u,l,r);
}
int main()
{
// freopen("in.txt","r",stdin);
int n,tcase=0;
while(cin >> n,n)
{
int num=0;
memset(flag,0,sizeof(flag));
memset(sum,0,sizeof(sum));
for(int i=0; i<n; i++)
{
double x1,x2,y1,y2;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
line[++num]=Node(x1,x2,y1,1);
X[num]=x1;
line[++num]=Node(x1,x2,y2,-1);
X[num]=x2;
}
sort(X+1,X+num+1);
sort(line+1,line+num+1);
int k=1;
for(int i=2; i<=num; i++)
if(X[i]!=X[i+1]) X[++k]=X[i];
double ans=0;
for(int i=1; i<num; i++)
{
int l=find(line[i].lx,k);
int r=find(line[i].rx,k)-1;
Update(1,1,k,l,r,line[i].s);
ans+=sum[1]*(line[i+1].y-line[i].y);
}
// printf("Test case #%d\n",++tcase);
// printf("Total explored area: %.2lf\n\n",ans);
printf("%.0lf\n",ans);
}
printf("*\n");
return 0;
}
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 AA with
the bottom left corner located at (0,
0)(0,0) and
the top right corner at (2,
2)(2,2),
and the other rectangle BB with
the bottom left corner located at (1,1)(1,1) and
the top right corner at (3,3)(3,3),
it follows that the area of the union of AA and BB should
be 77,
instead of 88.
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,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 beas large as 10001000.
After n, there will be n lines representing the n rectangles; each line contains four integers <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),
and the top right corner of the rectangle is located at (c,
d)(c,d).
Note that integers aa, bb, cc, dd can
be as large as 1,000,0001,000,000.
These configurations of rectangles occur repetitively in the input as the pattern described above. An integer n
= 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 theintersecting 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 *
题目来源
2017ACM-ICPC 亚洲区(南宁赛区)网络赛
线段树加扫描线模板题,队友给力!!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define lz 2*u,l,mid
#define rz 2*u+1,mid+1,r
const int maxn=4222;
double sum[maxn];
int flag[maxn];
double X[maxn];
struct Node
{
double lx, rx, y;
int s;
Node(){};
Node(double lx_, double rx_, double y_, int s_)
{
lx=lx_, rx=rx_, y=y_, s=s_;
}
bool operator <(const Node &S) const
{
return y<S.y;
}
}line[maxn];
int find(double tmp, int n)
{
int l=1, r=n, mid;
while(l<=r)
{
mid=(l+r)>>1;
if(X[mid]==tmp) return mid;
else if(X[mid]<tmp) l=mid+1;
else r=mid-1;
}
}
void push_up(int u, int l, int r)
{
if(flag[u]) sum[u]=X[r+1]-X[l];
else if(l==r) sum[u]=0;
else sum[u]=sum[2*u]+sum[2*u+1];
}
void Update(int u, int l, int r, int tl, int tr, int c)
{
if(tl<=l&&r<=tr)
{
flag[u]+=c;
push_up(u,l,r);
return ;
}
int mid=(l+r)>>1;
if(tr<=mid) Update(lz,tl,tr,c);
else if(tl>mid) Update(rz,tl,tr,c);
else
{
Update(lz,tl,mid,c);
Update(rz,mid+1,tr,c);
}
push_up(u,l,r);
}
int main()
{
// freopen("in.txt","r",stdin);
int n,tcase=0;
while(cin >> n,n)
{
int num=0;
memset(flag,0,sizeof(flag));
memset(sum,0,sizeof(sum));
for(int i=0; i<n; i++)
{
double x1,x2,y1,y2;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
line[++num]=Node(x1,x2,y1,1);
X[num]=x1;
line[++num]=Node(x1,x2,y2,-1);
X[num]=x2;
}
sort(X+1,X+num+1);
sort(line+1,line+num+1);
int k=1;
for(int i=2; i<=num; i++)
if(X[i]!=X[i+1]) X[++k]=X[i];
double ans=0;
for(int i=1; i<num; i++)
{
int l=find(line[i].lx,k);
int r=find(line[i].rx,k)-1;
Update(1,1,k,l,r,line[i].s);
ans+=sum[1]*(line[i+1].y-line[i].y);
}
// printf("Test case #%d\n",++tcase);
// printf("Total explored area: %.2lf\n\n",ans);
printf("%.0lf\n",ans);
}
printf("*\n");
return 0;
}
相关文章推荐
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles
- L. The Heaviest Non-decreasing Subsequence Problem -最长不降子序列变形nlogn-2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛- J. Minimum Distance in a Star Graph
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 A Weather Patterns(阅读理解)
- Minimum Distance in a Star Graph BFS/康托展开 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F.Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F. Overlapping Rectangles (面积并+线段树)
- 2017 ACM-ICPC亚洲区(南宁赛区)网络赛 F.Frequent Subsets Problem
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛:Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 L.The Heaviest Non-decreasing Subsequence Problem
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles
- M. Frequent Subsets Problem - 状态压缩-2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 H. A Cache Simulator
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛-I-GSM Base Station Identification(线性变换)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛-L. The Heaviest Non-decreasing Subsequence Problem
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 L. The Heaviest Non-decreasing Subsequence Problem (LIS)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 The Heaviest Non-decreasing Subsequence Problem
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles