跳到正文

个人记录

P5816求条,样例RE

#SUSPENDED#约 4 分钟
原文

这是 2026年1月3日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年1月3日
更新
2026年1月4日
篇幅
约 4 分钟

版本

  1. v2当前
  2. v1
C++
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'

const int N = 1e5 + 7;
int pre[N], pos[N]; 
int n, ans, cnt;

struct node1 {
	int x, y;
}p[N];

struct node2 {
	int op, x, y, r;
}l[N * 8];

bool cmpx(node1 a, node1 b) {
	//return a.x == b.x ? a.y < b.y : a.x < b.x;
    if (a.x != b.x) {
        return a.x < b.x;
    } 
	else {
        return a.y < b.y;
    }
}

bool cmpy (node1 a, node1 b) {
	//return a.y == b.y ? a.x < b.x : a.y < b.y;
	if (a.y != b.y) {
		return a.y < b.y;
	}
	else {
		return a.x < b.x;
	}
}

bool cmp2 (node2 a, node2 b) {
	//return a.y == b.y ? a.op < b.op : a.y < b.y;
	if (a.y != b.y) {
		return a.y < b.y;
	}
	else {
		return a.op < b.op;
	}
}

int lowbit (int x) {
	return x & (-x);
}

void add (int x, int v) {
	for (int i = x; i <= N; i += lowbit(i)) {
		pre[i] += v;
	}
}

int ask (int x) {
	int sum = 0;
	for (int i = x; i; i -= lowbit(i)) {
		sum += pre[i];
	}
	return sum;
}

int main() {
	ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; ++i) {
    	cin >> p[i].x >> p[i].y;
    	pos[i] = p[i].x;
	}
	sort (pos + 1, pos + 1 + n);
	int tot = unique (pos + 1, pos + 1 + n) - pos - 1;
	for (int i = 1; i <= n; ++i) {
		p[i].x = lower_bound (pos + 1, pos + 1 + tot, p[i].x) - pos;
	}
	sort (p + 1, p + 1 + n, cmpx);
	for (int i = 1; i < n; ++i) {
		if (p[i].x == p[i + 1].x) {
			l[++cnt] = (node2) {1, p[i].x, 0, p[i].y};
			l[++cnt] = (node2) {-1, p[i].x, 0, p[i].y};
		}
	}
	sort (p + 1, p + 1 + n, cmpy);
	for (int i = 1; i < n; ++i) {
		if (p[i].y == p[i + 1].y) {
			l[++cnt] = (node2) {0, p[i].x, p[i + 1].x, p[i].y};
		}
	}
	sort (l + 1, l + 1 + cnt, cmp2);
	for (int i = 1; i <= cnt; ++i) {
		if (!l[i].op) ans += ask(l[i].r - 1) - ask(l[i].x);
		else add(l[i].x, l[i].op);
	}
	cout << ans + n << endl;

	return 0;
}