跳到正文

休闲·娱乐

upan

EzSun约 4 分钟
原文
文章信息与版本

文章信息

发布
2025年8月12日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 4 分钟

版本

  1. v2当前
  2. v1
C++
#include<bits/stdc++.h>
using namespace std;
#define ls (x << 1)
#define rs (x << 1 | 1)
const int N = 10007; 
int n;
int lx[N], ly[N], lenx, leny;
int now[N];
int ans;
struct line{
	int l, r, p, d;
}a[N], b[N];
void lisan(){
	sort(lx + 1, lx + n + 1);
	lenx = unique(lx + 1, lx + n + 1) - lx - 1;
	sort(ly + 1, ly + n + 1);
	leny = unique(ly + 1, ly + n + 1) - ly - 1;
}
struct Segment_Tree{
	struct sgt{
		int l, r;
		int cnt, sum; 
	}t[N << 3];
	void build(int x, int l, int r){
		t[x].l = l, t[x].r = r;
		t[x].cnt = t[x].sum = 0;
		if(l == r) return;
		int mid = (l + r) >> 1;
		build(ls, l, mid);
		build(rs, mid + 1, r);
	}
	void push_up(int x){
		if(t[x].cnt) t[x].sum = now[t[x].r + 1] - now[t[x].l];
		else if(t[x].l == t[x].r) t[x].sum = 0;
		else t[x].sum = t[ls].sum + t[rs].sum;
	}
	void update(int x, int l, int r, int d){
		if(t[x].l >= l && t[x].r <= r){
			t[x].cnt += d;
			push_up(x);
			return;
		}
		int mid = (t[x].l + t[x].r) >> 1;
		if(l <= mid) update(ls, l, r, d);
		if(r > mid) update(rs, l, r, d);
		push_up(x);
	}
	int query(){
		return t[1].sum;
	}
}T;
bool cmp1(line a, line b){
	if(a.p != b.p) return a.p < b.p;
	else return a.d > b.d;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	for(int i = 1; i <= n; i++){
		int x, y, x_, y_;
		cin >> x >> y >> x_ >> y_;
		a[(i << 1) - 1] = {x, x_, y, 1};
		a[i << 1] = {x, x_, y_, -1};
		b[(i << 1) - 1] = {y, y_, x, 1};
		b[i << 1] = {y, y_, x_, -1};
		lx[(i << 1) - 1] = x, lx[i << 1] = x_;
		ly[(i << 1) - 1] = y, ly[i << 1] = y_;
	}
	n <<= 1;
	lisan();
	sort(a + 1, a + n + 1, cmp1);
	sort(b + 1, b + n + 1, cmp1);
	T.build(1, 1, 10000);
	memcpy(now, lx, sizeof(lx));
	for(int i = 1, lst = 0; i <= n; i++){
		int l = lower_bound(lx + 1, lx + lenx + 1, a[i].l) - lx;
		int r = lower_bound(lx + 1, lx + lenx + 1, a[i].r) - lx;
		T.update(1, l, r - 1, a[i].d);
		ans += abs(T.query() - lst);
		lst = T.query();
	}
	T.build(1, 1, 10000);
	memcpy(now, ly, sizeof(ly));
	for(int i = 1, lst = 0; i <= n; i++){
		int l = lower_bound(ly + 1, ly + leny + 1, b[i].l) - ly;
		int r = lower_bound(ly + 1, ly + leny + 1, b[i].r) - ly;
		T.update(1, l, r - 1, b[i].d);
		ans += abs(T.query() - lst);
		lst = T.query();
	}
	printf("%d\n", ans);
	return 0;
} 
原文