RT,MaxN设成1e5会RE,但设成1e6就过了(第三行)
#include<bits/stdc++.h>
#define ll long long
#define MaxN 1114514
#define lc (p << 1)
#define rc ((p << 1) | 1)
#define mid ((l + r) >> 1)
using namespace std;
int dc[MaxN << 1]; //discreate
struct ScanLine {
ll l, r, h;
int k;
bool operator < (const ScanLine &rhs) const {
return h < rhs.h;
}
} a[MaxN << 1];
struct SegTree {
ll len, cnt;
} tr[MaxN << 2];
void build(int p, int l, int r) {
//cout << l << " " << r << endl;
if(l == r) {
tr[p].len = 0, tr[p].cnt = 0;
return;
}
build(lc, l, mid);
build(rc, mid + 1, r);
}
void pushup(int p, int l, int r) {
if(tr[p].cnt > 0) {
tr[p].len = dc[r + 1] - dc[l];
} else {
tr[p].len = tr[lc].len + tr[rc].len;
}
}
void update(int p, ll l, ll r, int s, int t, int k) {
if(l >= dc[t + 1] || r <= dc[s]) return;
if(l <= dc[s] && dc[t + 1] <= r) {
tr[p].cnt += k;
// tr[p].len = dc[t + 1] - dc[s];
pushup(p, s, t);
return;
}
if(l <= dc[s + t >> 1]) update(lc, l, r, s, s + t >> 1, k);
if(dc[(s + t >> 1) + 1] <= r) update(rc, l, r, (s + t >> 1) + 1, t, k);
pushup(p, s, t);
}
int main(){
//freopen("owo.txt", "w", stdout);
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
ll _x1, _y1, _x2, _y2;
scanf("%lld%lld%lld%lld", &_x1, &_y1, &_x2, &_y2);
int t = (i << 1) - 1;
dc[t] = _x1, dc[t + 1] = _x2;
a[t] = (ScanLine){_x1, _x2, _y1, 1};
a[t + 1] = (ScanLine){_x1, _x2, _y2, -1};
}
n <<= 1;
sort(a + 1, a + n + 1);
sort(dc + 1, dc + n + 1);
int m = unique(dc + 1, dc + n + 1) - (dc + 1);
ll ans = 0;
build(1, 1, m - 1);
for(int i = 1; i < n; i++) {
update(1, a[i].l, a[i].r, 1, m - 1, a[i].k);
ans += tr[1].len * (a[i + 1].h - a[i].h);
// cout << tr[1].len << " " << a[i + 1].h - a[i].h << endl;
}
printf("%lld", ans);
return 0;
}