You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.4 KiB

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 10010;
struct ScanLine {
int x1, x2, y, mark;
const bool operator<(const ScanLine &t) const {
if (y == t.y) return mark > t.mark;
return y < t.y;
}
} line[N];
struct Node {
int l, r;
int cnt, len;
int c;
bool lc, rc;
} tr[N << 2];
int b[N];
void pushup(int u) {
int l = tr[u].l, r = tr[u].r;
if (tr[u].cnt) {
tr[u].len = b[r + 1] - b[l];
tr[u].c = 2;
tr[u].lc = tr[u].rc = 1;
} else {
tr[u].len = tr[u << 1].len + tr[u << 1 | 1].len;
tr[u].c = tr[u << 1].c + tr[u << 1 | 1].c;
if (tr[u << 1 | 1].lc && tr[u << 1].rc) tr[u].c -= 2;
tr[u].lc = tr[u << 1].lc;
tr[u].rc = tr[u << 1 | 1].rc;
}
}
void modify(int u, int l, int r, int v) {
if (l <= tr[u].l && r >= tr[u].r) {
tr[u].cnt += v;
pushup(u);
return;
}
int mid = (tr[u].l + tr[u].r) >> 1;
if (l <= mid) modify(u << 1, l, r, v);
if (r > mid) modify(u << 1 | 1, l, r, v);
pushup(u);
}
void build(int u, int l, int r) {
tr[u].l = l, tr[u].r = r;
if (l == r) return;
int mid = (l + r) >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("HDU1828.in", "r", stdin);
#endif
ios::sync_with_stdio(false), cin.tie(0);
int n;
while (cin >> n) {
memset(tr, 0, sizeof tr);
int x1, y1, x2, y2;
for (int i = 1; i <= n; i++) {
cin >> x1 >> y1 >> x2 >> y2;
line[i * 2 - 1] = {x1, x2, y1, 1};
line[i * 2] = {x1, x2, y2, -1};
b[i * 2 - 1] = x1, b[i * 2] = x2;
}
n <<= 1;
sort(line + 1, line + n + 1);
sort(b + 1, b + n + 1);
int tot = unique(b + 1, b + 1 + n) - b - 1;
build(1, 1, tot - 1);
LL res = 0;
int last = 0;
for (int i = 1; i < n; i++) {
int L = lower_bound(b + 1, b + tot + 1, line[i].x1) - b;
int R = lower_bound(b + 1, b + tot + 1, line[i].x2) - b;
modify(1, L, R - 1, line[i].mark);
res += (LL)tr[1].c * (line[i + 1].y - line[i].y);
res += (LL)abs(tr[1].len - last);
last = tr[1].len;
}
res += line[n].x2 - line[n].x1;
printf("%lld\n", res);
}
return 0;
}