求卡常 /kk
查看原帖
求卡常 /kk
174009
denominator楼主2024/10/27 21:20

RT。1.2s 都卡不进!

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 140010, LOGN = 20;
int T, n, m, k, a_[N], a[N], c[N], t[N << 1], w[N << 1], dep[N << 1];
bool d[N];
ll ans[N];
char s[N];
int read () {
	int f = 1, x = 0;
	char ch = getchar ();
	while (ch < 48 || ch > 57) {
		ch = getchar ();
	}
	while (ch >= 48 && ch <= 57) {
		x = (x << 3) + (x << 1) + (ch ^ '0');
		ch = getchar ();
	}
	return x;
}
void pre_dep (int p) {
	if (p >= (1 << k)) {
		return ;
	}
	dep[p] = dep[p >> 1] - 1;
	pre_dep (p << 1), pre_dep (p << 1 | 1);
}
void dfs1 (int p) {
	if (p >= (1 << k)) {
		t[p] = p - (1 << k) + 1;
		w[p] = a[p - (1 << k) + 1];
		return ;
	}
	dfs1 (p << 1), dfs1 (p << 1 | 1);
	int D = dep[p], w1 = w[p << 1], w2 = w[p << 1 | 1];
	if (!d[p] && w1 >= D) {
		t[p] = t[p << 1];
	} else {
		t[p] = t[p << 1 | 1];
	}
	if (d[p] && w2 >= D || !d[p] && w1 < D) {
		w[p] = w2;
	} else {
		w[p] = w1;
	}
}
void dfs2 (int p, int lim, int l, int r) {
	if (p >= (1 << k)) {
		int i = p - (1 << k) + 1;
		if (l <= min (r, i - 1)) {
			ans[l] += i, ans[min (r, i - 1) + 1] -= i;
		}
		if (a[i] >= lim && max (l, i) <= r) {
			ans[max (l, i)] += i, ans[r + 1] -= i;
		}
		return ;
	}
	if (d[p]) {
		dfs2 (p << 1, lim, l, w[p << 1 | 1] >= dep[p]? min (t[p << 1 | 1] - 1, r): r);
		dfs2 (p << 1 | 1, max (lim, dep[p]), l, r);
	} else {
		dfs2 (p << 1 | 1, lim, l, w[p << 1] >= dep[p]? min (t[p << 1] - 1, r): r);
		dfs2 (p << 1, max (lim, dep[p]), l, r);
	}
}
int main () {
	freopen ("arena.in", "r", stdin);
	freopen ("arena.out", "w", stdout);
	n = read (), m = read ();
	while ((1 << k) < n) {
		k++;
	}
	for (int i = 1; i <= n; i++) {
		a_[i] = read ();
	}
	for (int i = 1; i <= m; i++) {
		c[i] = read ();
	}
	for (int i = k - 1; i >= 0; i--) {
		char ch = getchar ();
		while (ch < 48 || ch > 57) {
			ch = getchar ();
		}
		for (int j = 1; j <= (1 << i); j++) {
			d[j + (1 << i) - 1] = (ch ^ 48);
			ch = getchar ();
		}
	}
	dep[0] = k + 1;
	pre_dep (1);
	T = read ();
	while (T --> 0) {
		fill (ans + 1, ans + n + 1, 0);
		int x[4];
		for (int i = 0; i < 4; i++) {
			x[i] = read ();
		}
		for (int i = 1; i <= n; i++) {
			a[i] = a_[i] ^ x[i % 4];
		}
		dfs1 (1);
		for (int i = 0; i <= k; i++) {
			dfs2 (1 << (k - i), 0, i == 0? 1: (1 + (1 << (i - 1))), 1 << i);
		}
		for (int i = 1; i <= n; i++) {
			ans[i] += ans[i - 1];
		}
		ll rans = 0;
		for (int i = 1; i <= m; i++) {
			rans ^= 1ll * i * ans[c[i]];
		}
		printf ("%lld\n", rans);
	}
	return 0;
}
2024/10/27 21:20
加载中...