HDOJ-1542 Atlantis(扫描线)

来源:互联网 发布:网络教育好找工作吗 编辑:程序博客网 时间:2024/06/08 08:25
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#define maxn 105using namespace std;struct Node{    Node(){    }    Node(double a, double b, double c, int d){        x1 = a;        x2 = b;        y = c;        flag = d;    }    double x1, x2, y;    int flag;    friend bool operator < (const Node &a, const Node &b){        return a.y < b.y;    }}node[maxn];double sum[maxn<<3], p[maxn<<1];int cnt[maxn<<3];void Pushup(int n, int l, int r){    if(cnt[n]) sum[n] = p[r+1] - p[l];    else if(l == r) sum[n] = 0;    else sum[n] = sum[n<<1] + sum[n<<1|1];}void Update(int n, int l, int r, int L, int R, int s){    if(l == L && R == r){        cnt[n] += s;        Pushup(n, l, r);        return ;    }    int mid = (l + r) >> 1;    if(R <= mid)     Update(n<<1, l, mid, L, R, s);    else if(L > mid)     Update(n<<1|1, mid+1, r, L, R, s);    else{        Update(n<<1, l, mid, L, mid, s);        Update(n<<1|1, mid+1, r, mid+1, R, s);    }    Pushup(n, l, r);}int main(){//  freopen("in.txt", "r", stdin);    int n, t = 0;    while(scanf("%d", &n) == 1 && n){        memset(sum, 0, sizeof(sum));        memset(cnt, 0, sizeof(cnt));        double x1, y1, x2, y2;        int k = 0;        for(int i= 1; i <= n; i++){            scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);            node[2*i-1] = Node(x1, x2, y1, 1);            node[2*i] = Node(x1, x2, y2, -1);            p[++k] = x1;            p[++k] = x2;        }        sort(node+1, node+1+2*n);        sort(p+1, p+1+k);        int d = unique(p+1, p+1+k) - p - 1;        double ss = 0;        for(int i = 1; i < 2*n; i++){            int l = lower_bound(p+1, p+1+d, node[i].x1) - p;            int r = lower_bound(p+1, p+1+d, node[i].x2) - p;            Update(1, 1, d, l, r - 1, node[i].flag);            ss += sum[1] * (node[i+1].y - node[i].y);         }        printf("Test case #%d\nTotal explored area: %.2lf\n\n", ++t, ss);    }    return 0;}
0 0