HDU 1542 Atlantis (線段樹+離散化+掃描線)
Atlantis
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 13446 Accepted Submission(s): 5588
Problem 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 file 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.
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.
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
給你n個矩形,讓你算出總面積,重疊的當然不能算。
POINT:
離散化不說了。
掃描線法,具體看了很多大神的部落格,可以去搜搜。
下面這兩段要好好理解一下。
if(cnt[x])sum[x]=hah[r+1]-hah[l];
R=lower_bound(hah+1,hah+1+m,len[i].r)-hah-1
可以想象成每個點都是一條線段,區間(1,1)代表端點1到2的長度,區間(1,2)代表端點1到3的長度。
為什麼這樣呢,走過彎路除錯一下就知道了呀!直接聽懂不是很無趣嗎?(你只是不會講而已別說了
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <map>
#include <string.h>
#include <algorithm>
#include <vector>
#include <fstream>
using namespace std;
#define lt 2*x
#define rt 2*x+1
#define LL long long
const int N = 210*10;
struct node
{
double l,r;
int flag;
double h;
}len[N/10];
int cnt[N];
double sum[N];
double hah[N/10];
bool cmd(node a,node b)
{
return a.h<b.h;
}
void init()
{
memset(hah,0,sizeof hah);
memset(len,0,sizeof len);
memset(sum,0,sizeof sum);
memset(cnt,0,sizeof cnt);
}
void pushup(int x,int l,int r)
{
if(cnt[x]) sum[x]=hah[r+1]-hah[l];
else if(l==r) sum[x]=0;
else sum[x]=sum[rt]+sum[lt];
}
void add(int x,int l,int r,int ll,int rr,int flag)
{
if(ll<=l&&rr>=r)
{
cnt[x]+=flag;
}
else
{
int mid=(l+r)>>1;
if(ll<=mid) add(lt,l,mid,ll,rr,flag);
if(mid<rr) add(rt,mid+1,r,ll,rr,flag);
}
pushup(x,l,r);
}
int main()
{
int n;
int p=0;
while(~scanf("%d",&n)&&n)
{
init();
int num=0;
for(int i=1;i<=n;i++)
{
double x1,y1,x2,y2;
scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
len[++num].h=y1,len[num].l=x1,len[num].r=x2;
hah[num]=x1;
len[num].flag=1;
len[++num].h=y2,len[num].l=x1,len[num].r=x2;
hah[num]=x2;
len[num].flag=-1;
}
sort(len+1,len+1+num,cmd);
sort(hah+1,hah+1+num);
int m=1;
for(int i=2;i<=num;i++)
if(hah[i]!=hah[i-1]) hah[++m]=hah[i];
double ans=0;
for(int i=1;i<=num;i++)
{
int L=lower_bound(hah+1,hah+1+m,len[i].l)-hah;
int R=lower_bound(hah+1,hah+1+m,len[i].r)-hah-1;
add(1,1,m,L,R,len[i].flag);
ans+=sum[1]*(len[i+1].h-len[i].h);
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n",++p,ans);
}
return 0;
}
相關文章
- HDU 1542 Atlantis(掃描線)
- HDU 1255 覆蓋的面積(線段樹+掃描線+離散化)
- HDU 5862 Counting Intersections(樹狀陣列+掃描線+離散化)陣列
- HDU 3333 Turing Tree(線段樹+離線操作)
- ECNU OJ 3353 塗黑板(線段樹離散化)
- HDH 1264 Counting Squares (線段樹+掃描線|暴力)
- 線段樹 transformation——hdu 4578ORM
- POJ 2528 Mayor's posters (線段樹 區間更新+離散化)
- hdu 1698 線段樹 一段更新染色
- HDU 1394 Minimum Inversion Number (暴力+線段樹)
- HDU 2795 Billboard(線段樹 區間最大)
- Transformation HDU - 4578線段樹綜合操作ORM
- HDU 1556 Color the ball(線段樹|樹狀陣列)陣列
- 洛谷P1712 [NOI2016]區間 尺取法+線段樹+離散化
- HDU 1166 敵兵佈陣 (線段樹 插點問線)
- HDU 3074 Multiply game(線段樹 單點更新)GAM
- HDU 1754 I Hate It (線段樹 區間最值)
- 可持久化線段樹持久化
- 淺談掃描線
- 線段樹
- 線~段~樹
- HDU 4027 Can you answer these queries? (線段樹 區間開方)
- 掃描線及其應用
- 線段樹最佳化建圖
- 01 線段樹
- 線段樹--RMQMQ
- 【模版】線段樹
- 線段樹模板
- 線段樹 hate it
- ut.cpp 最大線段並減線段交 [線段樹]
- 掃描所有無線網路
- 洛谷P4632 [APIO2018] New Home 新家(動態開節點線段樹 二分答案 掃描線 set)API
- 【主席數】可持續化線段樹
- HDU 3397 Sequence operation(線段樹區間染色加區間合併)
- 李超線段樹
- 線段樹進階
- 權值線段樹
- 線段樹筆記筆記