自己造了好多样例和答案代码比都是对的,但就是全wrong
查看原帖
自己造了好多样例和答案代码比都是对的,但就是全wrong
1127749
Longerdoc楼主2024/9/18 20:20
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
const int N = 6e5 + 100;
const int M = N * 31;
typedef pair<int, int> ppi;
int root[N], tr[M][2], max_id[N];
int s[N];
struct ed {
	int val;
	int l, r;
	int p;
	int R;
	bool operator< (const struct ed& t) const { return this->val < t.val; }

};
int idx;
priority_queue<ed> heap;

void insert(int i, int k, int p, int q) {
	if (k < 0) {
		max_id[q] = i;
		return;
	}
	int v = s[i] >> k & 1;
	if (p) tr[q][v ^ 1] = tr[p][v ^ 1];
	tr[q][v] = ++idx;
	insert(i, k - 1, tr[p][v], tr[q][v]);
	max_id[q] = max(max_id[tr[q][1]], max_id[tr[q][0]]);

}
ed qurey(int L, int R, int C, int r) {
	int p = root[R];
	for (int i = 31; i >= 0; i--) {
		int v = C >> i & 1;
		if (max_id[tr[p][v ^ 1]] >= L) p = tr[p][v ^ 1];
		else p = tr[p][v];
	}
	return { s[max_id[p]] ^ C,L,R,max_id[p],r };

}
int main() {
	int n, m;
	long long int ans = 0;
	cin >> n >> m;
	root[0] = ++idx;
	max_id[0] = -1;
	insert(0, 31, 0, root[0]);
	for (int i = 1; i <= n; i++) {
		int x;
		cin >> x;
		s[i] = s[i - 1] ^ x;
		root[i] = ++idx;
		insert(i, 31, root[i - 1], root[i]);
	}

	for (int i = n; i >= 1; i--) {
		auto t = qurey(0, i - 1, s[i], i);
		//cout << t.val << " " << t.p << endl;;
		heap.push(t);
	}
	while (m--) {
		auto t = heap.top();
		heap.pop();
		ans += t.val;
		//cout << t.l << " " << t.r << ' ' << t.p << endl;
		if (t.p > t.l) {
			auto tt = qurey(t.l, t.p - 1, s[t.R], t.R); heap.push(tt);
		};
		if (t.p < t.r) {
			auto tt = qurey(t.p + 1, t.r, s[t.R], t.R); heap.push(tt);
		};
	}
	printf("%lld",ans);
	return 0;
}```
2024/9/18 20:20
加载中...