2017 icpc 南宁赛区 F.Overlapping Rectangles(重叠矩形的最大面积+线段树模板)
来源:互联网 发布:java与xml 第3版 pdf 编辑:程序博客网 时间:2024/06/05 08:19
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 亚洲区(南宁赛区)网络赛
【题解】给n个矩形,求出这n个矩形的围成的最大面积。
每给一个矩形坐标,就遍历前i-1个矩形,每次把当前输入的矩行与第k(0<k<=i-1)个矩形的重合面积求出来,然后两个矩形面积和减去重叠部分面积,每次都这样保存结果,维护一个ans就好。
【AC代码】
#include <algorithm>#include <cctype>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <map>#include <queue>#include <string>#include <set>#include <vector>using namespace std;#define pr(x) cout << #x << " = " << x << " "#define prln(x) cout << #x << " = " << x << endlconst int N =5000, INF = 0x3f3f3f3f, MOD = 1e9 + 7;int n;struct Seg { double l, r, h; int d; Seg() {} Seg(double l, double r, double h, int d): l(l), r(r), h(h), d(d) {} bool operator< (const Seg& rhs) const {return h < rhs.h;}} a[N];int cnt[N << 2];double sum[N << 2], all[N];#define lson l, m, rt << 1#define rson m + 1, r, rt << 1 | 1void push_up(int l, int r, int rt) { if(cnt[rt]) sum[rt] = all[r + 1] - all[l]; else if(l == r) sum[rt] = 0; else sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];}void update(int L, int R, int v, int l, int r, int rt) { if(L <= l && r <= R) { cnt[rt] += v; push_up(l, r, rt); return; } int m = l + r >> 1; if(L <= m) update(L, R, v, lson); if(R > m) update(L, R, v, rson); push_up(l, r, rt);}int main() { int kase = 0; while(scanf("%d", &n) == 1 && n) { for(int i = 1; i <= n; ++i) { double x1, y1, x2, y2; scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2); a[i] = Seg(x1, x2, y1, 1); a[i + n] = Seg(x1, x2, y2, -1); all[i] = x1; all[i + n] = x2; } n <<= 1; sort(a + 1, a + 1 + n); sort(all + 1, all + 1 + n); int m = unique(all + 1, all + 1 + n) - all - 1; memset(cnt, 0, sizeof cnt); memset(sum, 0, sizeof sum); double ans = 0; for(int i = 1; i < n; ++i) { int l = lower_bound(all + 1, all + 1 + m, a[i].l) - all; int r = lower_bound(all + 1, all + 1 + m, a[i].r) - all; if(l < r) update(l, r - 1, a[i].d, 1, m, 1); ans += sum[1] * (a[i + 1].h - a[i].h); } printf("%lld\n",(long long)ans); } printf("*\n"); return 0;}
- 2017 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 亚洲区(南宁赛区)网络赛 F.Overlapping Rectangles
- 矩形面积并集,2017ICPC网络赛(南宁)Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 B. Train Seats Reservation 和 F. Overlapping Rectangles
- Overlapping Rectangles(线段树,矩形面积并)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛:Overlapping Rectangles
- 2017南宁网络赛F.Overlapping Rectangles(扫描线法+线段树)
- UVA 11345 Rectangles(n个矩形重叠覆盖的面积)
- hdoj2056(Rectangles 求矩形重叠面积
- zookeeper的日志文件和快照的可视化
- 组合数取模
- 第四周项目3
- 集成学习之Adaboost算法原理小结
- Scrapy爬虫笔记
- 2017 icpc 南宁赛区 F.Overlapping Rectangles(重叠矩形的最大面积+线段树模板)
- java 技术资料,jar包下载,工具下载等资源网站
- HDOJ2009(预处理)
- css background-image 自适应宽高
- Hive基础(3):表分类、视图、数据加载方式、数据的导出、本地模式
- 梯度提升树(GBDT)原理
- jquery.ajax 跨域请求webapi,设置headers
- 实战演练 test16 T2
- 模拟 飞(fly)