为什么后8个点都Too short on line 1.
查看原帖
为什么后8个点都Too short on line 1.
397282
_NTT_楼主2021/11/28 17:36

记录

#include <bits/stdc++.h>
using namespace std;

inline int read() {
    int x = 0; char c = getchar(); bool f = 1;
    while(c < '0' || c > '9') { if(c == '-') f = 0; c = getchar(); }
    while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
    return (f ? x : -x);
}

const int N = 1e5 + 5;
long long res;
int n, x[N << 1]; namespace in { int x1, y1, x2, y2; }
struct scanline {
    int y, l, r, f;
    inline bool operator < (const scanline &x) { return y < x.y; }
} line[N << 1];

struct segment_tree {
#define ls(k) k << 1
#define rs(k) k << 1 | 1
    int l, r, cnt = 0;
    long long len = 0;
} tree[N << 3];

void build(int k, int l, int r) {
    tree[k].l = l; tree[k].r = r;
    if(l == r) return ;

    int mid = l + r >> 1;
    build(ls(k), l, mid);
    build(rs(k), mid + 1, r);
}

void updata(int k, int &l, int &r, int &c){
    if(x[tree[k].l] >= l && x[tree[k].r + 1] <= r) {
        tree[k].cnt += c;
        if(tree[k].cnt) tree[k].len = x[tree[k].r + 1] - x[tree[k].l];
        else tree[k].len = tree[ls(k)].len + tree[rs(k)].len;
        return ;
    }

    if(x[tree[k].l] >= r || x[tree[k].r + 1] <= l) return ;

    updata(ls(k), l, r, c);
    updata(rs(k), l, r, c);

    if(tree[k].cnt) tree[k].len = x[tree[k].r + 1] - x[tree[k].l];
    else tree[k].len = tree[ls(k)].len + tree[rs(k)].len;
}

int main() {
    n = read();

    for(int i = 1 ;i <= n ;++i) {
        in::x1 = read(); in::y1 = read(); in::x2 = read(); in::y2 = read();
        x[(i << 1) - 1] = in::x1; x[i << 1] = in::x2;
        line[(i << 1) - 1] = {in::y1, in::x1, in::x2, 1};
        line[i << 1] = {in::y2, in::x1, in::x2, -1};
    }

    n <<= 1;
    sort(line + 1, line + 1 + n);
    sort(x + 1, x + 1 + n);

    int tot = unique(x + 1, x + 1 + n) - x - 1;
    build(1, 1, tot - 1);

    for(int i = 1 ;i < n ;++i) {
        updata(1, line[i].l, line[i].r, line[i].f);
        res += tree[1].len * (line[i + 1].y - line[i].y);
    }

    cout << res << endl;

    return 0;
}
2021/11/28 17:36
加载中...