求矩阵 并集的面积; 用线段树来解决

推荐一个 线段树求 矩阵交并集的 思路:

http://www.cnblogs.com/scau20110726/archive/2013/04/12/3016765.html

客官老爷请移步:

线段树矩阵交并集 模板代码:

http://blog.csdn.net/sizaif/article/details/78089278

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.

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 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 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
*

题目来源

2017 ACM-ICPC 亚洲区(南宁赛区)网络赛

【代码实现】

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define FIN      freopen("input.txt","r",stdin)
#define FOUT     freopen("output.txt","w",stdout)
#define S1(n)    scanf("%d",&n)
#define SL1(n)   scanf("%I64d",&n)
#define S2(n,m)  scanf("%d%d",&n,&m)
#define SL2(n,m)  scanf("%I64d%I64d",&n,&m)
#define Pr(n)     printf("%d\n",n)
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
using namespace std;
typedef long long ll;
const double PI=acos(-1);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int MAX=50005;
const int MOD=1e9+7;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};ll inv[maxn*2];
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){ x=1; y=0; d=a; }else{ ex_gcd(b,a%b,d,y,x); y-=x*(a/b);};}
ll gcd(ll a,ll b){ return b?gcd(b,a%b):a;}
ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll ans=exgcd(b,a%b,x,y);ll temp=x;x=y;y=temp-a/b*y;return ans;}
ll lcm(ll a,ll b){ return b/gcd(a,b)*a;}
ll qpow(ll x,ll n){ll res=1;for(;n;n>>=1){if(n&1)res=(res*x)%MOD;x=(x*x)%MOD;}return res;}
ll inv_exgcd(ll a,ll n){ll d,x,y;ex_gcd(a,n,d,x,y);return d==1?(x+n)%n:-1;}
ll inv1(ll b){return b==1?1:(MOD-MOD/b)*inv1(MOD%b)%MOD;}
ll inv2(ll b){return qpow(b,MOD-2);}const int MAXN=2000+5;int col[MAXN<<2],n,cnt,res;
double X[MAXN<<2],Sum[MAXN<<2],Sum2[MAXN<<2];struct  Seg{double l,r,h;int flag;Seg(){}Seg(double l,double r,double h,int flag):l(l),r(r),h(h),flag(flag){}bool operator <(const Seg & object ) const{return h<object.h;}
}S[MAXN<<2];void pushup(int rt,int l,int r)
{if(col[rt])//覆盖一次Sum[rt]=X[r+1]-X[l];else if(l==r) Sum[rt]=0;else Sum[rt]=Sum[rt<<1]+Sum[rt<<1|1];if(col[rt]>=2)// 覆盖两次以上Sum2[rt]=X[r+1]-X[l];else if(l==r) Sum2[rt]=0;else if(col[rt]==1) Sum2[rt]=Sum[rt<<1] +Sum[rt<<1|1];else if(col[rt]==0) Sum2[rt]=Sum2[rt<<1] +Sum2[rt<<1|1];
}void update(int L,int R,int c,int rt,int l,int r)//   l,r 固定长度 L,R  变化长度
{if(L<=l&&r<=R){col[rt]+=c;pushup(rt,l,r);return;}int mid=(l+r)>>1;if(L<=mid) update(L,R,c,lson);if(R>mid) update(L,R,c,rson);pushup(rt,l,r);
}int binary_find(double x)
{int lb=-1,ub=res-1;while(ub-lb>1){int mid=(lb+ub)>>1;if(X[mid]>=x)ub=mid;else lb=mid;}return ub;
}
double solve(int n)
{cnt=res=0;for(int i=0;i<n;i++){double a,b,c,d;scanf("%lf %lf %lf %lf",&a,&b,&c,&d);S[cnt]=Seg(a,c,b,1);X[cnt++]=a;S[cnt]=Seg(a,c,d,-1);X[cnt++]=c;}sort(X,X+cnt);sort(S,S+cnt);res++;for(int i=1;i<cnt;i++){// 去重if(X[i]!=X[i-1]) X[res++]=X[i];}memset(Sum,0,sizeof(Sum));memset(col,0,sizeof(col));memset(Sum2,0,sizeof(Sum2));double ans=0;for(int i=0;i<cnt-1;i++){int l=binary_find(S[i].l);//二分左端点,int r=binary_find(S[i].r)-1; // 左闭右开 二分右端点update(l,r,S[i].flag,1,0,res-1);ans+= Sum[1]*(S[i+1].h-S[i].h);// 矩阵并//ans+= Sum2[1]*(S[i+1].h-S[i].h); //矩阵 交集}return ans;
}
int main()
{while(~scanf("%d",&n)){if(n==0){printf("*\n");break;}printf("%.lf\n",solve(n));}return 0;
}

123

转载于:https://www.cnblogs.com/sizaif/p/9078454.html

2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F题 Overlapping Rectangles(线段树)相关推荐

  1. 计蒜客-2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛H题Skiing(拓扑序求DAG最长路)

    题意: 给定一个有向无环图,求该图的最长路. 思路: 由于是有向无环图,所以最长路肯定是一个入度为0到出度为0的路径,拓扑序在确定当前点之前能够考虑到所有到它的情况,所以最后取个最值即可. 代码: # ...

  2. 2017acm乌鲁木齐赛区网络赛F题tarjan缩点

    poj1236是问把一棵树变成强联通分量,于是答案就是rudu为0的和出度为0的最大值,因为假设入度为0的多一些,先每个出度为0的连接一个入度为0的,那么还剩一些入度为0的,这时候入度为0的随意连接一 ...

  3. 2016 ACM/ICPC亚洲区青岛站现场赛(部分题解)

    摘要 本文主要列举并求解了2016 ACM/ICPC亚洲区青岛站现场赛的部分真题,着重介绍了各个题目的解题思路,结合详细的AC代码,意在熟悉青岛赛区的出题策略,以备战2018青岛站现场赛. HDU 5 ...

  4. ZOJ 3810 A Volcanic Island (2014年牡丹江赛区网络赛B题)

    ZOJ 3810 A Volcanic Island (2014年牡丹江赛区网络赛B题) 1.题目描写叙述:点击打开链接 2.解题思路:本题是四色定理的模板题.只是有几种情况要提前特判一下:n==1直 ...

  5. ACM-ICPC 2018 南京赛区网络预赛 Lpl and Energy-saving Lamps 线段树

    目录 ACM-ICPC 2018 南京赛区网络预赛 Lpl and Energy-saving Lamps 线段树 题面 题意 思路 ACM-ICPC 2018 南京赛区网络预赛 Lpl and En ...

  6. 计蒜客 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B coin(求乘法逆元)

    Bob has a not even coin, every time he tosses the coin, the probability that the coin's front face u ...

  7. 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛B: Out-out-control cars

    问题 B: Out-out-control cars 题目描述 Two out-of-control cars crashed within about a half-hour Wednesday a ...

  8. Maximum Flow(2017 ACM-ICPC 亚洲区(西安赛区)网络赛 E)

    Problem Description Given a directed graph with nn nodes, labeled 0,1,⋯,n−1. For each <i, j> s ...

  9. Skiing(2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 H)

    Problem Description In this winter holiday, Bob has a plan for skiing at the mountain resort. This s ...

  10. 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 E Maximum Flow

    原题解链接:http://blog.csdn.net/kkkkahlua/article/details/78009087 他用的最小割的来求解最大流.认为只要讨论每一个点到0和n-1那个更小哪条边就 ...

最新文章

  1. postman测试传入json
  2. Windows 下查看端口占用情况 netstat / tasklist / findstr
  3. halcon相关的链接
  4. php与服务器关系,php与web服务器关系
  5. android 富文本框架_当微擎框架遇上uniapp,以一当十同时开发十个平台项目
  6. Spark精华问答 | spark的组件构成有哪些?
  7. SPI 读取不同长度 寄存器_[读书笔记]《计算机科学速成课》—6 寄存器和内存
  8. 「CF 932E」 Team Work
  9. GCD 和 NSOperationQueue 的差别
  10. Confluence 6 针对合并完全失败的内容重新运行合并
  11. 什么是原子性,什么是原子性操作?
  12. 华为鲲鹏HCIA认证 常考题
  13. 基于热传导方程的高温作业专用服装设计(二)
  14. MOOC 研究生学术与职业素养 课后答案
  15. linux挂载ipsan存储,centos系统ISCSI挂载IPSAN存储
  16. 嵌入式系统那些事-一张图秒懂系统启动流程
  17. 会声会影2022版新版新增蓝光功能
  18. 浏览记录-history
  19. angularjs 同步請求_AngularJS 应用请求设置同步问题~
  20. cstdio 错误解决

热门文章

  1. 架构师都应该知道的康威定律
  2. 华泰证券高薪诚聘 技术大牛/运维平台架构师
  3. 福利 | droidcon Beijing 2016安卓技术大会
  4. php模拟登陆,PHP模拟登陆手记
  5. mysql数据库过滤数据_MySQL数据库常规操作一些简单绕过过滤的方法
  6. RabbitMQ----源码安装
  7. 'telent' 不是内部或外部命令,也不是可运行的程序或批处理文件。
  8. python中的itertools模块
  9. 初中级前端开发工程师如何提升个人能力?
  10. apache kafkac系列lient发展-java