2 10 10 20 20 15 15 25 25.5 0
Test case #1 Total explored area: 180.00
/*=============================================================================
#
# Author: liangshu - cbam
#
# QQ : 756029571
#
# School : 哈尔滨理工大学
#
# Last modified: 2015-08-11 12:30
#
# Filename: A.cpp
#
# Description:
# The people who are crazy enough to think they can change the world, are the ones who do !
=============================================================================*/
#
#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int INF = 2024;
struct line {
double s, e, y, mark;
line(double s, double e, double y, double ma):s(s),e(e),y(y), mark(ma){}
bool friend operator < (line a, line b){
return a.y < b.y;
}
};
struct Tree{
int cover;
int lft,rht;double sum;
}tree[INF<<2];
vector<double>X;
vector<line>L;
map<double, int>h;
void create(int root, int left, int right){
tree[root].lft = left;
tree[root].rht = right;
tree[root].sum = 0;
if(left + 1 != right){
int mid = (left + right)/2;
create(root<<1, left, mid);
create(root<<1|1, mid, right);
}
return ;
}
void update(int root, int val, int left, int right){
if(tree[root].lft >= left && tree[root].rht <= right){
tree[root].cover += val;
if(tree[root].cover){
tree[root].sum = X[tree[root].rht] - X[tree[root].lft];return ;
}
else{
tree[root].sum = tree[root<<1].sum + tree[root<<1|1].sum;
return ;
}
}
if(tree[root].lft == tree[root].rht)return ;
int mid = (tree[root].lft + tree[root].rht)/2;
if(left < mid){
update(root <<1, val, left, right);
}
if(right >= mid){
update(root<<1|1,val,left, right);
}
if(!tree[root].cover)
tree[root].sum = tree[root<<1].sum + tree[root<<1|1].sum;
}
int main(){
int n,cs = 1;
while(~scanf("%d", &n)&&n){
h.clear();L.clear();X.clear();
double x1, y1, x2, y2;
for(int i = 1; i <= n; i++){
scanf("%lf%lf%lf%lf",&x1, &y1, &x2, &y2);
L.push_back(line(x1, x2, y1, 1));
L.push_back(line(x1, x2, y2, -1));
X.push_back(x1);X.push_back(x2);
}
sort(X.begin(), X.end());
sort(L.begin(), L.end());
X.erase(unique(X.begin(),X.end()), X.end());
for(int i = 0; i < X.size(); i++){
h[X[i]] = i;
}
create(1, 0, X.size() - 1);
double ans = 0;int i;
for( i = 0; i < L.size() - 1; i++){
update(1, L[i].mark, h[L[i].s], h[L[i].e]);
ans += (L[i + 1].y - L[i].y) * tree[1].sum;
}
update(1, L[i].mark, h[L[i].s], h[L[i].e]);
printf("Test case #%d\nTotal explored area: %.2lf\n\n",cs++ , ans);
}
}
/*=============================================================================
#
# Author: liangshu - cbam
#
# QQ : 756029571
#
# School : 哈尔滨理工大学
#
# Last modified: 2015-08-11 12:50
#
# Filename: A.cpp
#
# Description:
# The people who are crazy enough to think they can change the world, are the ones who do !
=============================================================================*/
#
#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int INF = 2222;
struct line
{
double s, e, y, mark;
} L[INF];
double sum[INF<<2];
int cnt[INF<<2];
vector<double>dict;
bool cmp(line a, line b)
{
return a.y < b.y;
}
void pushup(int rt, int l, int r)
{
if(cnt[rt])
{
sum [rt] = dict[r + 1] - dict[l] ;
}
else
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
int bin(double k, int len)
{
int l = 0, r = len - 1;
while (l <= r)
{
int mid = (l + r)>>1;
if(dict[mid] == k)return mid;
else if(dict[mid] > k)r = mid - 1;
else l = mid + 1;
}
return -1;
}
void update(int rt, int L, int R, int l ,int r, int val)
{
if(L <= l && r <= R)
{
cnt[rt] += val;
pushup(rt, l, r);
return ;
}
int mid = (l + r)>>1;
if(L <= mid)update(rt<<1, L, R, l , mid, val);
if(R > mid)update(rt<<1|1,L, R, mid + 1, r, val);
pushup(rt,l, r);
}
int main()
{
int n,cs = 1;
while(scanf("%d", &n) != EOF && n)
{
memset(sum, 0, sizeof(sum));
int t = 0;
for(int i = 1; i <= n; i++)
{
double x1,y1,x2,y2;
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
dict.push_back (x1);
L[t].s = x1;
L[t].e = x2;
L[t].y = y1;
L[t++].mark = 1;
dict.push_back(x2);
L[t].s = x1;
L[t].e = x2;
L[t].y = y2;
L[t++].mark = -1 ;
}
sort(L , L + t, cmp);
sort(dict .begin(), dict.end());
dict.erase (unique(dict.begin(), dict.end()), dict.end());
double ans = 0;
for(int i = 0; i < t ; i ++ )
{
int l = bin(L[i].s, dict.size());
int r = bin(L[i].e, dict.size()) - 1;
if(l <= r)
update(1, l, r, 0, dict.size() - 1, L[i].mark);
ans += sum[1] * (L[i + 1].y - L[i].y);
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n",cs++ , ans);
}
return 0 ;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
杭电 HDU ACM 1225 Atlantis (线段树 扫描线 离散化 最基本)
原文地址:http://blog.csdn.net/lsgqjh/article/details/47418431