考虑二分结界层数,将 n 个点按 x 大小依次加入答案,一行一行的做,用树状数组维护当前这一行中[0, x - 1] 包含祭坛大于 mid 的且 [x + 1, n] 中包含的祭坛也大于 mid 的坐标,再计算出这一行有几个地方可以作为中心,简单容斥(可能还不算容斥?)一下就可以了
有一个坑就是当节点层数是 0 的时候输出两行 0
#includeusing namespace std;const int N = 1e5 + 5;struct ele { int x, y; bool operator < (const ele A) const {return x < A.x || (x == A.x && y < A.y);}}d[N];int sum[N], f[N], y[N], allsum[N];int n, len;inline int lowbit(int x) {return x & -x;}inline void add(int x, int y) {for(int i = x; i <= n; i += lowbit(i)) f[i] += y;}inline int query(int x) {int ans = 0; for(int i = x; i; i -= lowbit(i)) ans += f[i]; return ans;}int solve(int mid) { memset(sum, 0, sizeof(sum)); memset(f, 0, sizeof(f)); int now = 1, ans = 0; for(int i = 0; i <= n; i++) { len = 0; while(now <= n && d[now].x == i) { int t = d[now++].y; y[++len] = t; } if(len >= (mid << 1)) { int l = y[mid] + 1, r = y[len - mid + 1] - 1; for(int j = mid + 1; j <= len - mid; j++) ans -= (sum[y[j]] >= mid && (allsum[y[j]] - sum[y[j]] >= mid)); ans += (query(r) - query(l - 1)); } for(int j = 1; j <= len; j++) { int t = y[j]; if(sum[t] >= mid && (allsum[t] - sum[t] == mid)) add(t, -1); sum[t]++; if(sum[t] == mid && (allsum[t] - sum[t] >= mid)) add(t, 1); } } return ans;}int main() { cin >> n; for(int i = 1; i <= n; i++) scanf("%d %d", &d[i].x, &d[i].y); sort(d + 1, d + n + 1); for(int i = 1; i <= n; i++) allsum[d[i].y]++; int l = 1, r = n / 4; while(l < r) { int mid = (l + r + 1) >> 1; if(solve(mid)) l = mid; else r = mid - 1; } if(solve(l) == 0) printf("0\n0\n"); else printf("%d\n%d\n", l, solve(l)); return 0;}