2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F. Overlapping Rectangles (面积并+线段树)
来源:互联网 发布:淘宝cf商城 编辑:程序博客网 时间:2024/06/07 03:16
There are n 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 A with the bottom left corner located at (0,0) and the top right corner at (2,2), and the other rectangle B with the bottom left corner located at (1,1) and the top right corner at (3,3), it follows that the area of the union of A and B should be 7, instead of 8.
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,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 1000. After n, there will be n lines representing the n rectangles; each line contains four integers <a,b,c,d> , which means that the bottom left corner of the rectangle is located at (a,b), and the top right corner of the rectangle is located at (c,d). Note that integers a, b, c, d can be as large as 1,000,000.
These configurations of rectangles occur repetitively in the input as the pattern described above. An integer n=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.
样例输入
20 0 2 21 1 3 330 0 1 12 2 3 34 4 5 50
样例输出
73*
题目来源
2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
面积并的模板题,赤裸裸的模板题。放一个线段树的题解。点击打开链接
代码实现:
#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<queue>#include<cstdio>#define ll long long#define lz 2*u,l,mid #define rz 2*u+1,mid+1,r#define mset(a,x) memset(a,x,sizeof(a))using namespace std;const double PI=acos(-1);const int inf=0x3f3f3f3f;const double esp=1e-6;const int maxn=400005;const int mod=1e9+7;int dir[4][2]={0,1,1,0,0,-1,-1,0};ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}ll lcm(ll a,ll b){return a/gcd(a,b)*b;}ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;} ll sum[maxn]; int visit[maxn]; ll ans[maxn]; struct Node{ ll lx, rx, y; ll s; Node(){}; Node(ll lx_,ll rx_,ll y_,ll s_) { lx=lx_, rx=rx_, y=y_, s=s_; } bool operator <(const Node &S) const { return y<S.y; }}line[maxn]; ll find(ll temp, ll n) { ll l=1,r=n,mid; while(l<=r) { mid=(l+r)>>1; if(ans[mid]==temp) return mid; else if(ans[mid]<temp) l=mid+1; else r=mid-1; } } void push_up(ll u, ll l, ll r) { if(visit[u]) sum[u]=ans[r+1]-ans[l]; else if(l==r) sum[u]=0; else sum[u]=sum[2*u]+sum[2*u+1]; } void Update(ll u, ll l, ll r, ll tl, ll tr, ll c) { if(tl<=l&&r<=tr) { visit[u]+=c; push_up(u,l,r); return ; } ll 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() { int n; while(cin>>n) { if(!n) { cout<<"*"<<endl; break;} ll num=0; mset(visit,0); mset(sum,0); for(ll i=0;i<n;i++) { ll x1,x2,y1,y2; scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2); line[++num]=Node(x1,x2,y1,1); ans[num]=x1; line[++num]=Node(x1,x2,y2,-1); ans[num]=x2; } sort(ans+1,ans+num+1); sort(line+1,line+num+1); ll k=1; for(ll i=2;i<=num;i++) if(ans[i]!=ans[i+1]) ans[++k]=ans[i]; ll ans=0; for(ll i=1; i<num; i++) { ll l=find(line[i].lx,k); ll 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); } cout<<ans<<endl; } return 0; }
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F. Overlapping Rectangles (面积并+线段树)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F. Overlapping Rectangles(面积并)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛: F. Overlapping Rectangles(线段树)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F题 Overlapping Rectangles(线段树)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F. Overlapping Rectangles(扫描线)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F. Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F. Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F.Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 B. Train Seats Reservation 和 F. Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛:Overlapping Rectangles
- 2017 icpc 南宁赛区 F.Overlapping Rectangles(重叠矩形的最大面积+线段树模板)
- 矩形面积并集,2017ICPC网络赛(南宁)Overlapping Rectangles
- 2017 ACM-ICPC亚洲区(南宁赛区)网络赛 F.Frequent Subsets Problem
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F题
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
- Spring aop
- Android DrawerLayout和NavigationView 的使用方法
- Oracle排序中NULL值处理的五种常用方法
- 周中训练笔记8
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛- J. Minimum Distance in a Star Graph
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F. Overlapping Rectangles (面积并+线段树)
- 第三周项目4
- “米录”讨论总结(一)
- LeetCode Find Bottom Left Tree Value [Medium]
- redis java客户端connection refused问题
- 输入一个十进制正整数,将该数转换成n进制数;
- spring架构流程
- eclipse环境下如何配置tomcat,并且把项目部署到Tomcat服务器上
- 2017 ACM-ICPC南宁网络赛: I. GSM Base Station Identification(线性变换)