POJ1151矩形面积并
来源:互联网 发布:淘宝好评返现举报入口 编辑:程序博客网 时间:2024/06/09 23:46
题意: 求n个矩形面积并。
import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.Scanner;import java.util.Set;import java.util.TreeSet;public class Main {public static void main(String[] args) {new POJ1151().run();}}class POJ1151 {void run() {Scanner cin = new Scanner(new BufferedInputStream(System.in));PrintWriter cout = new PrintWriter(new BufferedOutputStream(System.out));int Case = 0;int n;while ((n = cin.nextInt()) != 0) {Line[] lines = new Line[2 * n];List<Double> xs = new ArrayList<Double>();for (int i = 0; i < n; i++) {double x1 = cin.nextDouble(), y1 = cin.nextDouble();double x2 = cin.nextDouble(), y2 = cin.nextDouble();lines[i * 2] = new Line(x1, x2, y1, 1);lines[i * 2 + 1] = new Line(x1, x2, y2, -1);xs.add(x1);xs.add(x2);}h = new Hash(xs);int limit = h.length() - 1;build(1, 0, limit);double res = 0;Arrays.sort(lines);for (int i = 0; i < 2 * n - 1; i++) {int l = h.idx(lines[i].left_x);int r = h.idx(lines[i].right_x) - 1;update(l, r, lines[i].f, 1, 0, limit);res += len[1] * (lines[i + 1].y - lines[i].y);}if(Case != 0) cout.println();cout.printf("Test case #%d\n", ++Case);cout.printf("Total explored area: %.2f\n", res);}cout.flush();}void build(int t, int l, int r) {left[t] = l;right[t] = r;len[t] = 0;cnt[t] = 0;if (l == r)return;int mid = (l + r) >> 1;build(t << 1, l, mid);build(t << 1 | 1, mid + 1, r);}void up(int t) {if (cnt[t] != 0)len[t] = h.value(right[t] + 1) - h.value(left[t]);else if (left[t] == right[t])len[t] = 0;elselen[t] = len[t << 1] + len[t << 1 | 1];}void update(int al, int ar, int c, int t, int l, int r) {if (al <= l && r <= ar) {cnt[t] += c;up(t);return;}int mid = (l + r) >> 1;if (al <= mid)update(al, ar, c, t << 1, l, mid);if (ar > mid)update(al, ar, c, t << 1 | 1, mid + 1, r);up(t);}Hash h;final int N = 208;int[] left = new int[N << 2];int[] right = new int[N << 2];int[] cnt = new int[N << 2];double[] len = new double[N << 2];}class Line implements Comparable<Line> {double left_x;double right_x;double y;int f;Line(double left_x, double right_x, double y, int f) {this.left_x = left_x;this.right_x = right_x;this.y = y;this.f = f;}@Overridepublic int compareTo(Line other) {return Double.compare(y, other.y);}}class Hash {Hash(List<Double> array) {Set<Double> treeSet = new TreeSet<Double>();for (double d : array)treeSet.add(d);this.array = new double[treeSet.size()];size = 0;for (double d : treeSet)this.array[size++] = d;}int length() {return size;}int idx(double key) {return Arrays.binarySearch(array, key);}double value(int idx) {return array[idx];}int size;double[] array;}
0 0
- poj1151矩形面积并
- POJ1151矩形面积并
- poj1151 矩形面积并
- poj1151等 矩形面积并
- POJ1151 离散化求矩形面积的并
- poj1151 hdu1542 wikioi3044 Atlantis 矩形面积求并
- POJ1151(线段树+扫描线求矩形面积并)
- poj1151平面矩形面积并,双离散化
- 【poj1151】Atlantis(矩形面积并+线段树+扫描线)
- POJ1151 Atlantis扫描线求矩形面积
- hdu 1542 (poj1151) Atlantis ( 线段树求矩形面积并)
- POJ1151-(线段树扫描线+离散化)求矩形面积并
- POJ1151 (HDU 1542) Atlantis【矩形面积并,线段树+离散化+扫描线模板】
- 【codevs3044】矩形面积求并&&【POJ1151】Atlantis,第一次的扫描线
- POJ1151 Atlantis(线段树,扫描线,离散化,矩形面积并)
- poj1151 Atlantis(线段树+扫描线+离散化,求矩形面积并)
- Poj1151/HDU1542(求并矩形覆盖的面积)分别用离散化处理,矩形切割,线段树三种方法实现.
- Poj1151线段树求面积并
- 类成员函数参数和类成员变量的可见性
- PTA 基础编程题 5-37 整数分解为若干项之和 (20分)
- Java 位操作符使用---Single Number
- Python中文分词组件 jieba
- httpURLConnection 报错 java.net.ConnectException: localhost/127.0.0.1:8080 - Connection refused
- POJ1151矩形面积并
- win10下cmd切换工作目录
- 24.ELK实时日志分析平台之Filebeat介绍及安装方法
- module.exports还是exports?
- iOS开发网络篇—NSURLConnection基本使用
- MySQL通信协议
- 管理第三方CocoaPods 简洁安装
- 作为一个女孩,我们努力的意义~
- 猴子选王