当前位置: 首页 > 工具软件 > Atlantis > 使用案例 >

Atlantis (扫描线)

仇征
2023-12-01

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

题意:求n个矩形覆盖的面积

//#include"bits/stdc++.h"
//#include<unordered_map>
//#include<unordered_set>
#include<iostream>
#include<sstream>
#include<iterator>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<bitset>
#include<climits>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stack>
#include<map>
#include<ctime>
#include<new>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
#define lson l, mid, node<<1
#define rson mid + 1, r, node<<1|1
const int INF  =  0x3f3f3f3f;
const int O    =  1e6;
const int mod  =  10007;
const int maxn =  2e3 + 5;
const double PI  =  acos(-1.0);
const double E   =  2.718281828459;

struct node{
    double l, r, h;
    int ok;
    bool friend operator < (node a, node b) {return a.h < b.h; }
}line[maxn<<2];

int treeNum[maxn<<2];

double treeSum[maxn<<2];

double  X[maxn<<2];

void pushup(int l, int r, int node){
    if(treeNum[node<<1] == treeNum[node<<1|1]) treeNum[node] = treeNum[node<<1];
    else treeNum[node] = -1;
    treeSum[node] = treeSum[node<<1] + treeSum[node<<1|1];
}

void pushdown(int l, int r, int node){
    int mid = (l + r) >> 1;
    if(treeNum[node] != -1) {
        treeNum[node<<1] = treeNum[node<<1|1] = treeNum[node];
        treeSum[node<<1] = treeNum[node] > 0 ? (X[mid+1] - X[l]) : 0;
        treeSum[node<<1|1] = treeNum[node] > 0 ? (X[r+1] - X[mid+1]) : 0;
    }
}

void build(int l, int r, int node){
    if(l == r) {
        treeSum[node] = 0; treeNum[node] = 0;
        return ;
    }
    int mid = (l + r) >> 1;
    build(lson); build(rson);
    pushup(l, r, node);
}

void update(int ql ,int qr, int ok, int l, int r, int node){
    if(ql <= l && qr >= r && treeNum[node] != -1) {
        treeNum[node] += ok;
        treeSum[node] = treeNum[node] > 0 ? (X[r+1] - X[l]) : 0;
        return ;
    }
    pushdown(l, r, node);
    int mid = (l + r) >> 1;
    if(ql <= mid) update(ql, qr, ok, lson);
    if(qr > mid) update(ql, qr, ok, rson);
    pushup(l, r, node);
}

int fun(double pos , int n, double X[]){
    int l = 1, r = n, ans = -1;
    while(l <= r) {
        int mid = (l + r) >> 1;
        if(X[mid] <= pos) { ans = mid; l = mid + 1; }
        else r = mid - 1;
    }
    return ans;
}

int main(){
    int Case = 0, n;
    while(~ scanf("%d", &n) && n){
        int cnt = 0;
        for(int i=0; i<n; i++) {
            double x1, x2, y1, y2;
            scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
            X[++ cnt] = x1;
            line[cnt] = node{x1, x2, y1, 1};
            X[++ cnt] = x2;
            line[cnt] = node{x1, x2, y2, -1};
        }
        sort(X + 1, X + cnt + 1);
        sort(line + 1, line + cnt + 1);
        int m = 1;
        for(int i=2; i<=cnt; i++) if(X[i] != X[i-1]) X[++m] = X[i];
        build(1, m-1, 1);
        double ans = 0;
        for(int i=1; i<cnt; i++) {
            int l = fun(line[i].l, m, X);
            int r = fun(line[i].r, m, X) - 1;
            if(l <= r) update(l ,r, line[i].ok, 1, m-1, 1);
            ans += treeSum[1] * (line[i+1].h - line[i].h);
        }
        printf("Test case #%d\nTotal explored area: %.2lf\n\n", ++ Case, ans );
    }
    return 0;
}

 

 类似资料: