http://poj.org/problem?id=1151

经典矩形面积并吧.....很简单我就不说了...

有个很神的地方,我脑残没想到:

将线段变成点啊QAQ这样方便计算了啊

还有个很坑的地方,为毛每一次我精确地计算过空间可就是wa....一改大就ac...我无力了..

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } const int N=1005;
#define lc x<<1
#define rc x<<1|1
#define lson l, mid, lc
#define rson mid+1, r, rc
int tot, idn;
double idy[N*2];
struct T { double sum; int num; }t[N*8+10];
void pushup(int x, int l, int r) { if(t[x].num>0) t[x].sum=idy[r+1]-idy[l]; else t[x].sum=t[lc].sum+t[rc].sum; }
void update(int L, int R, int k, int l=1, int r=tot, int x=1) {
if(L<=l && r<=R) { t[x].num+=k; pushup(x, l, r); return; }
int mid=(l+r)>>1;
if(L<=mid) update(L, R, k, lson);
if(mid<R) update(L, R, k, rson);
pushup(x, l, r);
}
int n, ln; struct dat { double x, y[2]; int flag; }line[N*2];
bool cmp(const dat &a, const dat &b) { return a.x<b.x; }
void cln() { tot=tot*4+1; rep(i, tot) t[i].sum=0, t[i].num=0; tot=0; } int main() {
int test=0;
while(read(n), n) {
ln=idn=tot=0;
for1(i, 1, n) {
static double x[2], y[2];
rep(k, 2) scanf("%lf%lf", &x[k], &y[k]), idy[++idn]=y[k];
++ln; line[ln].x=x[0]; rep(k, 2) line[ln].y[k]=y[k]; line[ln].flag=1;
++ln; line[ln].x=x[1]; rep(k, 2) line[ln].y[k]=y[k]; line[ln].flag=-1;
}
sort(idy+1, idy+1+idn);
idn=unique(idy+1, idy+1+idn)-idy-1;
tot=idn-1; sort(line+1, line+1+ln, cmp);
line[0].x=line[1].x;
double ans=0;
for1(i, 1, ln) {
ans+=t[1].sum*(line[i].x-line[i-1].x);
int left=lower_bound(idy+1, idy+1+idn, line[i].y[0])-idy,
rigt=lower_bound(idy+1, idy+1+idn, line[i].y[1])-idy-1;
update(left, rigt, line[i].flag);
} printf("Test case #%d\n", ++test);
printf("Total explored area: %.2f\n", ans);
puts("");
cln();
}
return 0;
}

  


Description

There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.

Input

The input consists of several test cases. Each test case starts with a line containing a single integer n (1 <= n <= 100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0 <= x1 < x2 <= 100000;0 <= y1 < y2 <= 100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area. 
The input file is terminated by a line containing a single 0. Don't process it.

Output

For each test case, your program should output one section. The first line of each section must be "Test case #k", where k is the number of the test case (starting with 1). The second one must be "Total explored area: a", where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point. 
Output a blank line after each test case.

Sample Input

2
10 10 20 20
15 15 25 25.5
0

Sample Output

Test case #1
Total explored area: 180.00

Source

【POJ】1151 Atlantis(线段树)的更多相关文章

  1. POJ 1151 Atlantis 线段树求矩形面积并 方法详解

    第一次做线段树扫描法的题,网搜各种讲解,发现大多数都讲得太过简洁,不是太容易理解.所以自己打算写一个详细的.看完必会o(∩_∩)o 顾名思义,扫描法就是用一根想象中的线扫过所有矩形,在写代码的过程中, ...

  2. POJ 1151 - Atlantis 线段树+扫描线..

    离散化: 将所有的x轴坐标存在一个数组里..排序.当进入一条线段时..通过二分的方式确定其左右点对应的离散值... 扫描线..可以看成一根平行于x轴的直线..至y=0开始往上扫..直到扫出最后一条平行 ...

  3. hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  4. POJ 1151 Atlantis 线段树+离散化+扫描线

    这次是求矩形面积并 /* Problem: 1151 User: 96655 Memory: 716K Time: 0MS Language: G++ Result: Accepted */ #inc ...

  5. POJ 1151 扫描线 线段树

    题意:给定平面直角坐标系中的N个矩形,求它们的面积并. 题解:建立一个四元组(x,y1,y2,k).(假设y1<y2)用来储存每一条线,将每一条线按x坐标排序.记录所有的y坐标以后排序离散化.离 ...

  6. POJ 1151 Atlantis(扫描线)

    题目原链接:http://poj.org/problem?id=1151 题目中文翻译: POJ 1151 Atlantis Time Limit: 1000MS   Memory Limit: 10 ...

  7. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  8. Buy Tickets POJ - 2828 思维+线段树

    Buy Tickets POJ - 2828 思维+线段树 题意 是说有n个人买票,但是呢这n个人都会去插队,问最后的队列是什么情况.插队的输入是两个数,第一个是前面有多少人,第二个是这个人的编号,最 ...

  9. POJ 1151 Atlantis 矩形面积求交/线段树扫描线

    Atlantis 题目连接 http://poj.org/problem?id=1151 Description here are several ancient Greek texts that c ...

  10. POJ 1151:Atlantis 线段树+扫描线

    Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19374   Accepted: 7358 Descrip ...

随机推荐

  1. CSS clearfix

    The problem happens when a floated element is within a container box, that element does not automati ...

  2. QEMU 使用的镜像文件:qcow2 与 raw

    qcow2 的基本原理 qcow2 镜像格式是 QEMU 模拟器支持的一种磁盘镜像.它也是可以用一个文件的形式来表示一块固定大小的块设备磁盘.与普通的 raw 格式的镜像相比,有以下特性: 更小的空间 ...

  3. 【云计算】docker run详解

    Docker学习总结之Run命令介绍 时间 2015-01-21 17:06:00                                               博客园精华区       ...

  4. Java单链表、双端链表、有序链表实现

    单链表: insertFirst:在表头插入一个新的链接点,时间复杂度为O(1) deleteFirst:删除表头的链接点,时间复杂度为O(1) 有了这两个方法,就可以用单链表来实现一个栈了,见htt ...

  5. Linux系统Shutdown命令定时关机详解

    转自:http://www.bootf.com/490.html Linux系统下的shutdown命令用于安全的关闭/重启计算机,它不仅可以方便的实现定时关机,还可以由用户决定关机时的相关参数.在执 ...

  6. Maven发布web项目到tomcat

    在java开发中经常要引入很多第三方jar包:然而无论是java web开发还是其他java项目的开发经常会由于缺少依赖包引来一些不必要的异常.常常也是因为这样的原因导致许多简单的缺包和版本问题耗费大 ...

  7. Android clickable 和 focusable

    setClickable(),好像是控制按钮是否可以被点击和点击之后触发监听器事件.setFocusable();控制键盘是否可以获得这个按钮的焦点.(我按实体键盘上方向键,button被选中) 今天 ...

  8. Greedy:Allowance(POJ 3040)

    零用钱大作战 题目大意:农夫和牛又搞新花样了,现在农夫想给Bessie每个星期都给一点零用钱,农夫有一堆面值的钱币,并且这个钱币都能被上一个钱币整除(1,5,10,50),并且钱币有一定数量,要你求最 ...

  9. codeforces B.Maximum Absurdity 解题报告

    题目链接:http://codeforces.com/contest/332/problem/B 题意:在一个序列中,在所有长度为k的区间里找出两个不重叠的最大和,输出这两个最大和所对应的开头的位置a ...

  10. Linux底下的第一个C程序

    首先保证你的Linux底下安装了GCC,假如没有安装GCC的话请参考: http://www.cnblogs.com/aspirant/p/3544398.html 假如输入 gcc -v 不再是co ...