求助站外题

题目总版

Register_int @ 2022-10-01 17:42:56

POJ 1151,扫描线过样例了,但不知道哪里有问题/kk

#include <iostream>
#include <algorithm>
#include <map>

using namespace std;

typedef double db;

const int MAXN = 1e6 + 10;

struct Line {
    db l, r, h;
    int mark;
    bool operator < (const Line &p) const { return h < p.h; }
} line[MAXN << 1]; 

int tot;

db num[MAXN << 1];

struct segtree {
    int l, r, cnt;
    db len;
} t[MAXN << 2];

inline 
void pushup(int p) {
    if (t[p].cnt) t[p].len = num[t[p].r + 1] - num[t[p].l];
    else t[p].len = t[p << 1].len + t[p << 1 | 1].len;
}

void build(int l, int r, int p) {
    t[p].l = l, t[p].r = r;
    if (l == r) return ;
    int mid = l + r >> 1;
    build(l, mid, p << 1), build(mid + 1, r, p << 1 | 1);
}

void add(db l, db r, int k, int p) {
    if (num[t[p].r + 1] <= l || r <= num[t[p].l]) return ;
    if (l <= num[t[p].l] && num[t[p].r + 1] <= r) return t[p].cnt += k, pushup(p);
    add(l, r, k, p << 1), add(l, r, k, p << 1 | 1);
    pushup(p);
}

int n;

db ax, ay, bx, by, ans;

int main() {
    for (int c = 1; scanf("%d", &n), n; c++) {
        tot = 0, ans = 0;
        for (int i = 1; i <= n; i++) {
            scanf("%lf%lf%lf%lf", &ax, &ay, &bx, &by);
            num[++tot] = ax, line[tot] = (Line){ ax, bx, ay, 1 };
            num[++tot] = bx, line[tot] = (Line){ ax, bx, by, -1 };
        }
        sort(line + 1, line + tot + 1);
        sort(num + 1, num + tot + 1);
        n = tot, tot = unique(num + 1, num + tot + 1) - num - 1;
        build(1, tot - 1, 1);
        for (int i = 1; i < n; i++) {
            add(line[i].l, line[i].r, line[i].mark, 1);
            ans += t[1].len * (line[i + 1].h - line[i].h);
        }
        printf("Test case #%d\nTotal explored area: %.2lf\n\n", c, ans);
    }
}

by yizcdl2357 @ 2022-10-01 17:55:09

cjld,别卷了


|