HDU 1542 - Atlantis (线段树 扫描线)

49 篇文章 0 订阅
11 篇文章 0 订阅

Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6407    Accepted Submission(s): 2823



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.
 

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
 

Recommend
linle   |   We have carefully selected several similar problems for you:   1828  1255  1698  1166  1540


题意:求矩形并的面积

扫描线
线段树维护当前X轴上可以用来求面积的长度

注意初始化 WA了几次


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define S64I1(a) scanf(iform1, &(a))
#define P64I1(a) printf(oform1, (a))
#define FOR(i, s, t) for(int (i)=(s); (i)<(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

const int maxn = 100000 + 20;
const int maxo = maxn * 4;

struct Seg {
    double l, r, h, d;
    Seg(double ll=0, double rr=0, double hh=0, int dd=-1) : l(ll), r(rr), h(hh), d(dd) {}
};

int n;
int num = 0;
int segNum = 0;
Seg segs[maxn*2];
double X[maxn*4];

int cover[maxo];
double sumv[maxo];

bool cmp(Seg a, Seg b) {
    return a.h < b.h;
}

void pushUp(int o, int L, int R) {
    if(cover[o]) sumv[o] = X[R+1] - X[L];
    else if(L == R) sumv[o] = 0;
    else sumv[o] = sumv[o<<1] + sumv[o<<1|1];
}

int ql, qr, qv;
void update(int o, int L, int R) {
    if(ql <= L && R <= qr) {
        cover[o] += qv;
        pushUp(o, L, R);
        return ;
    }
    int M = L + (R-L) / 2;
    if(ql <= M) update(o<<1, L, M);
    if(M < qr) update(o<<1|1, M+1, R);
    pushUp(o, L, R);
}

int Bin(double x) {
    int l = 0;
    int r = num-1;
    while(l <= r) {
        int m = (r+l)>>1;
        if(fabs(X[m]-x) < eps) return m;
        else if(X[m] < x) l = m+1;
        else r = m-1;
    }
    return -1;
}

int main() {
    int kase = 1;

    while(scanf("%d", &n) != EOF && n) {
        memset(cover, 0, sizeof(cover));
        memset(sumv, 0, sizeof(sumv));
        segNum = num = 0;
        for(int i=0; i<n; i++) {
            double x1, y1, x2, y2;
            scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
            segs[segNum++] = Seg(x1, x2, y1, 1);
            segs[segNum++] = Seg(x1, x2, y2, -1);
            X[num++] = x1;
            X[num++] = x2;
        }
        sort(X, X+num);
        int tnum = 1;
        for(int i=1; i<num; i++) {
            if(fabs(X[i]-X[i-1]) > eps) X[tnum++] = X[i];
        }
        num = tnum;
        double ans = 0;
        sort(segs, segs+segNum, cmp);
        for(int i=0; i<segNum-1; i++) {
            ql = Bin(segs[i].l);
            qr = Bin(segs[i].r) - 1;
            qv = segs[i].d;
            if(ql <= qr) update(1, 0, num-1);
            ans += sumv[1] * (segs[i+1].h - segs[i].h);
        }
        printf("Test case #%d\nTotal explored area: %.2lf\n\n", kase++, ans);
    }

    return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值