cdq 分治
  • 板块CF12D Ball
  • 楼主MarchKid_Joe
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/2/10 15:05
  • 上次更新2023/10/24 01:15:19
查看原帖
cdq 分治
239163
MarchKid_Joe楼主2023/2/10 15:05

测试 1616 比标准结果大 11。有没有同错误的人分享一下如何改正的?

Who can help me?

#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
int n, m;
struct type1
{
	int x;
	int y;
	int z;
	friend bool operator<(const type1 &A, const type1 &B)
	{
		if (A.x != B.x)
			return A.x > B.x;
		if (A.y != B.y)
			return A.y > B.y;
		return A.z > B.z;
	}
	friend bool operator!=(const type1 &A, const type1 &B)
	{
		return A.x != B.x || A.y != B.y || A.z != B.z;
	}
};
struct type2
{
	int x;
	int y;
	int z;
	int cnt;
	int ans;
	friend bool operator<(const type2 &A, const type2 &B)
	{
		return A.y != B.y ? A.y > B.y : A.z > B.z;
	}
};
type1 a[N];
type2 s[N];
namespace BIT
{
	int tr[N * 3];
	int limit;
	#define lowbit(x) ((+x) & (-x))
	void add(int i, int x)
	{
		while (i <= limit)
		{
			tr[i] += x;
			i += lowbit(i);
		}
	}
	int query(int i)
	{
		int ans = 0;
		while (i > 0)
		{
			ans += tr[i];
			i -= lowbit(i);
		}
		return ans;
	}
}
using namespace BIT;
void CDQ(int l, int r)
{
	if (l == r) return;
	int mid = (l + r) >> 1;
	CDQ(l, mid), CDQ(mid + 1, r);
	int i = l, j = mid;
	vector <int> rubbish;
	sort(s + l, s + mid + 1);
	sort(s + mid + 1, s + r + 1);
	while (++j <= r)
	{
		while (i <= mid && s[j].y < s[i].y)
		{
			if (s[j].x < s[i].x) add(s[i].z, +s[i].cnt), rubbish.emplace_back(i);
			++i;
		}
		s[j].ans += query(limit) - query(s[j].z);
	}
	for (auto i : rubbish) add(s[i].z, -s[i].cnt);
}
namespace LSH /*离散化*/
{
	int S[N * 3];
	inline void insert(type1 s, int i)
	{
		S[i * 3 - 0] = s.x;
		S[i * 3 - 1] = s.y;
		S[i * 3 - 2] = s.z;
	}
	inline void initial()
	{
		sort(S + 1, S + n * 3 + 1);
		limit = unique(S + 1, S + n * 3 + 1) - S - 1;
		for (int i = 1; i <= n; i++)
		{
			a[i].x = lower_bound(S + 1, S + limit + 1, a[i].x) - S;
			a[i].y = lower_bound(S + 1, S + limit + 1, a[i].y) - S;
			a[i].z = lower_bound(S + 1, S + limit + 1, a[i].z) - S;
		}
	}
}
using namespace LSH;
signed main()
{
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> (a[i].x);
	for (int i = 1; i <= n; i++) cin >> (a[i].y);
	for (int i = 1; i <= n; i++) cin >> (a[i].z);
	for (int i = 1; i <= n; i++) insert(a[i], i);
	initial();
	sort(a + 1, a + n + 1);
	for (int i = 1; i <= n; i++) /*一样的合并为 1 个*/
	{
		if (a[i] != a[i - 1]) s[++m] = {a[i].x, a[i].y, a[i].z};
		++s[m].cnt;
	}
	CDQ(1, m);
	int ans(0);
	for (int i = 1; i <= m; i++)
		if (s[i].ans)
			ans += s[i].cnt;
	cout << ans;
	return 0;
}
2023/2/10 15:05
加载中...