跳到正文

题解

题解:CF2267F2 XOR Transformations (Hard Version)

P2441M约 6 分钟
原文

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

文章信息与版本

文章信息

发布
2026年9月26日
收录
2026年9月26日
更新
2026年11月21日
篇幅
约 6 分钟
获赞
5
题目
CF2267F2

版本

  1. v2当前
  2. v1

什么叫 F1 被卡常了一怒之下写了 F2?

典完了。

猜测变化次数不能太多。不妨设当前所有数都 <2k<2^k,其中有 xx 个数在第 k−1k-1 位上为 11,n−xn-x 个数在第 k−1k-1 位上为 00,那么只有 (x2)+(n−x2)\dbinom{x}{2}+\dbinom{n-x}{2} 个数对的异或和 <2k−1<2^{k-1}。取 x=⌊n2⌋x=\left\lfloor\dfrac{n}{2}\right\rfloor 时上式取到最小值,而对于 n≥5n\geq 5,不难证明 (⌊n/2⌋2)+(⌈n/2⌉2)≥n−1\dbinom{\lfloor n/2\rfloor}{2}+\dbinom{\lceil n/2\rceil}{2}\geq n-1。又因为 (n−12)≥n\dbinom{n-1}{2}\geq n,所以至多 22 次操作后所有数 <2k−1<2^{k-1}。因此最多操作 2⌈log⁡V⌉2\lceil\log V\rceil 次。

现在问题只剩下如何快速求出 ai⊕aja_i\oplus a_j 的前 nn 小值。把 aa 扔到 Trie 上,容易做到 O(log⁡V)\mathcal{O}(\log{V}) 求 ai⊕va_i\oplus v 的第 kk 小值 kth⁡(v,k)\operatorname{kth}(v,k)。为了排除 ai⊕aia_i\oplus a_i 的情况,这里要钦定 k≥2k\geq 2。然后直接多路归并,用一个小根堆维护所有 (kth⁡(ai,k),ai,k)(\operatorname{kth}(a_i,k),a_i,k),每次取出堆顶令 k←k+1k\gets k+1 再塞回去。注意这里一个无序对会被考虑两次,我们直接求出前 2n2n 小值,取偶数位置上的数放到 aa 里即可。

时间复杂度为 O(nlog⁡nlog⁡V+nlog⁡2V)\mathcal{O}(n\log{n}\log{V}+n\log^2{V})。

代码
C++
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using i128 = __int128;
using ui = unsigned int;
using ull = unsigned long long;
using u128 = unsigned __int128;
using ld = long double;
using pii = pair<int, int>;
const int MAXN = 1e5 + 5;

template<typename T> T lowbit(T x) { return x & -x; }
template<typename T> void chkMin(T &x, T y) { x = y < x ? y : x; }
template<typename T> void chkMax(T &x, T y) { x = x < y ? y : x; }
constexpr int lg2(ll x) { return 63 ^ __builtin_clzll(x); }
constexpr ll bitCeil(ll x) { return x == 1 ? 1ll : 1ll << lg2(x - 1) + 1; }

int tc, n, q, a[MAXN], tmp[MAXN];

struct Trie {
	static const int MAXC = 3e6 + 5;
	int tot, son[MAXC][2], cnt[MAXC];

	void init() {
		for (int i = 0; i <= tot; ++i) son[i][0] = son[i][1] = cnt[i] = 0;
		tot = 0;
	}

	void ins(int x) {
		int p = 0;
		for (int i = 29; i >= 0; --i) {
			int ch = x >> i & 1;
			if (!son[p][ch]) son[p][ch] = ++tot;
			++cnt[p = son[p][ch]];
		}
	}

	int kth(int x, int k) {
		int p = 0, res = 0;
		for (int i = 29; i >= 0; --i) {
			int ch = x >> i & 1, q = son[p][ch];
			if (k <= cnt[q]) {
				p = q;
			} else {
				k -= cnt[q];
				res |= 1 << i;
				p = son[p][ch ^ 1];
			}
		}
		return res;
	}
} T;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	cin >> tc;
	while (tc--) {
		cin >> n >> q;
		for (int i = 1; i <= n; ++i) cin >> a[i];

		vector<int> ans;
		while (true) {
			int mx = *max_element(a + 1, a + n + 1), mn = *min_element(a + 1, a + n + 1);
			if (!mx) break;
			ans.emplace_back(mx - mn);

			T.init();
			for (int i = 1; i <= n; ++i) T.ins(a[i]);

			priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<>> Q;
			for (int i = 1; i <= n; ++i) Q.emplace(T.kth(a[i], 2), i, 2);
			for (int i = 1; i <= n * 2; ++i) {
				auto [val, id, k] = Q.top();
				Q.pop();
				if (~i & 1) tmp[i >> 1] = val;
				if (k < n && i < n * 2) Q.emplace(T.kth(a[id], k + 1), id, k + 1);
			}
			copy(tmp + 1, tmp + n + 1, a + 1);
		}

		while (q--) {
			int x;
			cin >> x;
			cout << (x < ans.size() ? ans[x] : 0) << '\n';
		}
	}
	return 0;
}