跳到正文

题解

题解:P14522 【MX-S11-T3】空之碎物

chAsing_meteor约 18 分钟
原文

这是 2025年11月16日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年11月16日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 18 分钟
获赞
9
题目
P14522

版本

  1. v4当前
  2. v3
  3. v2
  4. v1

提供一种接近正解的小常数 O(nlog⁡3V)O(n\log^3 V) 做法以及没有实现过的 O(nlog⁡2V)O(n\log^2V) 正解复杂度做法,注意后者只是口胡,如有错误,敬请指出。

大常数 O(nlog⁡3V)O(n\log^3 V) 做法

我们先考虑如何计算的 f(l,r)f(l,r)。

首先我们发现,x⊖y=x−(x∧y)x\ominus y=x-(x\wedge y),于是我们有结论 x⊖y≤xx\ominus y\le x,事实上,从它的定义中,也可以得到这个结论。

于是我们自然想要贪心的使得 f(l,r)=max⁡i=lraif(l,r)=\max_{i=l}^r a_i。

这个等价于去除掉最大值后,剩下的数能通过 ⊖\ominus 运算得到 00,或者最大值的这一位等于 00。

下文有些地方没有考虑最大值的这一位等于 00 的情况,可以自行推导。

性质 1

假设第一个变为 00 的数是 yy,那么一定存在 bb 满足 bb 被 a[l,r]a[l,r] 去除最大值即 yy 后的可重集合包含,且 ((((y⊖b1)⊖b2)⊖⋯ )⊖bm)=0((((y\ominus b_1)\ominus b_2)\ominus\cdots )\ominus b_m)=0。

也就是说,我们进行的计算一定都是先不断地对 yy 进行操作,而不会先对另外两个数操作后,再对 yy 操作。我们来证明这个结论。

性质 1 证明

我们假设先对 aa 和 bb 两个数进行了 a←a⊖ba\gets a\ominus b 的操作,再让 y←y⊖ay\gets y\ominus a,不难发现,这个操作不会增加新的二进制位。

所以,我们选择 y←((y⊖a)⊖b)y\gets((y\ominus a)\ominus b) 一定是不劣的。

类比可以证明,无论你进行多少次,直接让 yy 全部 ⊖\ominus 上这些数是不劣的。

然后我们对选定 yy 的每个二进制位来考虑,假设它位权位 2k2^k 的位上不为 00,如果这个位在 a[l,r]a[l,r] 中除去最大值还出现过,那么直接进行一次 ⊖\ominus 运算可以消掉,否则无法消除,但是我们。

于是我们发现,如果我们处理出来除去最大值之后,每个仅在 a[l,r]a[l,r] 的所有数中出现一次的二进制位的位权和 xx,对每个不等于最大值的 aia_i 进行 ai∧xa_i\wedge x,则当存在 i∈[l,r]i\in[l,r],(ai∧(x∧ak))=0(a_i\wedge (x\wedge a_k))=0(其中 aka_k 是区间最大值)时,答案为最大值。

性质 2

长度大于 log⁡V+2\log V+2 的序列,答案为最大值。

性质 2 证明

我们发现,当区间长度大于 log⁡V+2\log V +2 时,去除最大值后,还存在 log⁡V+1=26\log V +1=26 个数(注意这里 log⁡V\log V 是向上取整的,下文同)。

而仅在 a[l,r]a[l,r] 中出现一次的位最多覆盖 log⁡V=25\log V=25 个数,还剩下一个数不包含,所以存在 (ai∧x)=0(a_i\wedge x)=0,自然也存在 (ai∧(x∧ak))=0(a_i\wedge (x\wedge a_k))=0。

接下来我们考虑处理长度小于等于 2727 的区间。

注意此时不一定是拿最大值操作最优,因此需要枚举 aka_k,计算 xx,然后暴力枚举 aia_i,即可做到复杂度 O(nlog⁡3V)O(n\log^3 V)。

对于大于的部分,显而易见的,单调栈处理后,可以做到 O(nlog⁡V)O(n\log V) 或者 O(nlog⁡2V)O(n\log^2 V),后者更容易实现,且更容易想到,反正都能通过。

我们发现可能很难通过,是因为常数太大了。但是添加剪枝后可以通过,不过我认为可以卡掉,所以继续进行了优化。

小常数 O(nlog⁡3V)O(n\log^3 V) 做法

性质 3

去除最大值后,求出的 xx,和直接判断在 a[l,r]a[l,r] 中出现次数等于 22 求出的 xx(同样是位权和),造成贡献相同。

性质 3 证明

考虑分讨,设 zz 表示这一位出现次数。

当 z=2z=2,如果有一个出现在 aka_k 上,那么同原先 xx 一样都会造成一次贡献,否则没有变化,因为对 aka_k 进行了与运算后,这一位会消失。

当 z≠2z\ne 2,本来就不会产生影响。

于是我们省略了计算 xx 的部分,大大降低了常数,已经可以通过。提供的代码添加了剪枝,注释掉后仍然可以通过。不过添加后获得了本题目前最优解第五。

这个做法的代码
C++
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using db = double;
using ldb = long double;
using lll = __int128;
using ulll = unsigned __int128;

class fastio
{
	static const int MAX = 1 << 15; char ibuf[MAX], *p1 = ibuf, *p2 = ibuf, obuf[MAX], *p3 = obuf, sta[50]; bool file_end = false;
	char get() { return p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, MAX, stdin), p1 == p2) ? (file_end = true), char(EOF) : *p1++; }
	void put(const char x) { p3 - obuf < MAX ? *p3++ = x : (fwrite(obuf, p3 - obuf, 1, stdout), p3 = obuf, *p3++ = x); }
	explicit operator bool() { return !file_end; }
	size_t flush() { size_t f = fwrite(obuf, p3 - obuf, 1, stdout); p3 = obuf; *p3 = 0; return f; }
public :
	fastio &operator >> (char &t) { for (t = get(); !isgraph(t); t = get()); return *this; }
	fastio &operator >> (char *t) { char c; for (c = get(); !isgraph(c); c = get()); for (; isgraph(c); c = get()) * t = c, t++; *t = 0; return *this; }
	fastio &operator >> (string &t) { t.clear(); char c; for (c = get(); !isgraph(c); c = get()); for (; isgraph(c); c = get()) t += c; return *this; }
	fastio &operator << (const char t) { put(t); return *this; }
	fastio &operator << (const char *t) { for (; *t; ++t) put(*t); return *this; }
	fastio &operator << (const string &t) { for (const char it : t) put(it); return *this; }
	fastio &read(char *t) { char c; for (c = get(); !isgraph(c); c = get()); for (; isgraph(c); c = get()) * t = c, t++; *t = 0; return *this; }
	template <typename any1, typename any2> pair<any1, any2> tpval() { return pair<any1, any2>(tpval<any1>(), tpval<any2>()); }
	template <typename any> typename enable_if <is_same<any, char>::value, any>::type tpval() { char t; for (t = get(); !isgraph(t); t = get()); return t; }
	template <typename any> typename enable_if <is_same<any, string>::value, any>::type tpval() { string t; char c; for (c = get(); !isgraph(c); c = get()); for (; isgraph(c); c = get()) t += c; return t; }
	template <typename any> typename enable_if <(is_signed<any>::value && is_integral<any>::value && !is_same<any, char>::value) || is_same<any, __int128_t>::value, any >::type tpval() { any t = 0; bool y = 0; char c = get(); for (; !isdigit(c); c = get()) if (c == 45) y = true; for (; isdigit(c); c = get()) t = t * 10 + c - 48; if (y == 1) t = -t; return t; }
	template <typename any> typename enable_if <(is_signed<any>::value && std::is_integral<any>::value && !is_same<any, char>::value) || is_same<any, __int128_t>::value, fastio >::type &operator >> (any &t) { t = 0; bool y = 0; char c = get(); for (; !isdigit(c); c = get()) if (c == 45) y = true; for (; isdigit(c); c = get())t = t * 10 + c - 48; if (y == 1) t = -t; return *this; }
	template <typename any> typename enable_if <(is_unsigned<any>::value && std::is_integral<any>::value && !is_same<any, char>::value) || is_same<any, __uint128_t>::value, any >::type tpval() { any t = 0; char c = get(); for (; !isdigit(c); c = get()); for (; isdigit(c); c = get()) t = t * 10 + c - 48; return t; }
	template <typename any> typename enable_if <(is_unsigned<any>::value && is_integral<any>::value && !is_same<any, char>::value) || is_same<any, __uint128_t>::value, fastio >::type &operator >> (any &t) { t = 0; char c = get(); for (; !isdigit(c); c = get()); for (; isdigit(c); c = get()) t = t * 10 + c - 48; return *this; }
	template <typename any1, typename any2> fastio &operator >> (pair<any1, any2> &t) { return *this >> t.first >> t.second; }
	template <typename any> typename enable_if <(is_signed<any>::value && is_integral<any>::value && !is_same<any, char>::value) || is_same<any, __int128_t>::value, fastio >::type &operator << (any t) { if (!t) { put(48); return *this; } int len = 0; if (t < 0) t = -t, put(45); while (t) sta[len++] = char(t % 10 + 48), t /= 10; while (len--) put(sta[len]); return *this; }
	template <typename any> typename enable_if <(is_unsigned<any>::value && is_integral<any>::value && !is_same<any, char>::value) || is_same<any, __uint128_t>::value, fastio >::type &operator << (any t) { if (!t) { put(48); return *this; } int len = 0; while (t) sta[len++] = char(t % 10 + 48), t /= 10; while (len--) put(sta[len]); return *this; }
	template <typename any1, typename any2> fastio &operator << (const pair<any1, any2> &t) { return *this << t.first << ' ' << t.second; } template <typename any> fastio &write(const any &t) { return *this << t; } template <typename any, typename...args> fastio &write(const any &t1, const args &...t2) { return (*this << t1).write(t2...); }
	template <typename any> fastio &read(any &t) { return *this >> t; }
	template <typename any, typename...args> fastio & read(any &t1, args &...t2) { return (*this >> t1).read(t2...); }
	~fastio() { fwrite(obuf, p3 - obuf, 1, stdout); }
} fio;

const int N = 2e5 + 5, M = 25, B = 27, mod = 998244353, inf = 1 << 25;
int n, a[N], s[N][M], l[N], r[N];
ll res;

void add(int x, int cnt[]) {
	int pos = 0;
	while (x) {
		cnt[pos] += x & 1;
		x >>= 1, ++pos;
	}
}

void sub(int x, int cnt[]) {
	int pos = 0;
	while (x) {
		cnt[pos] -= x & 1;
		x >>= 1, ++pos;
	}
}

int main() {
	fio >> n;
	for (int i = 1; i <= n; ++i) {
		fio >> a[i];
		for (int j = 0; j < M; ++j)
			s[i][j] = s[i - 1][j];
		add(a[i], s[i]);
	}
	for (int l = 1; l <= n; ++l)
		for (int r = l; r - l + 1 <= B && r <= n; ++r) {
			if (l == r) {
				res += a[l];
				continue;
			}
			int y = 0;
			for (int i = 0; i < M; ++i)
				if (s[r][i] - s[l - 1][i] <= 2)
					y |= (1 << i);
			int mx = 0;
			for (int k = l; k <= r; ++k) {
				if (a[k] <= mx) continue;
				int mn, x = y & a[k];
				mn = x;
				for (int i = l; i <= r; ++i)
					if (i != k) {
						mn = min(mn, a[i] & x);
						if (!mn) break;
					}
				mx = max(mx, a[k] - mn);
			}
			res += mx;
		}
	res %= mod;
	stack<int> stk;
	a[0] = inf;
	stk.push(0);
	for (int i = 1; i <= n; ++i) {
		while (!stk.empty() && a[stk.top()] < a[i]) stk.pop();
		l[i] = stk.top() + 1;
		stk.push(i);
	}
	while (!stk.empty()) stk.pop();
	a[n + 1] = inf;
	stk.push(n + 1);
	for (int i = n; i; --i) {
		while (!stk.empty() && a[stk.top()] <= a[i]) stk.pop();
		r[i] = stk.top() - 1;
		stk.push(i);
	}
	for (int i = 1; i <= n; ++i) {
		ll cnt = (ll)(i - l[i] + 1) * (r[i] - i + 1);
		for (int j = 0; j <= B && j <= i - l[i]; ++j)
			for (int k = 0; k <= B - 1 - j && k <= r[i] - i; ++k)
				--cnt;
		res = (res + cnt * a[i]) % mod;
	}
	fio << res;
	return 0;
}

O(nlog⁡2V)O(n\log^2 V) 做法

我们发现,复杂度瓶颈仍然在计算长度小于等于 2727 的区间。

首先我们有性质 3,所以有点连边的感觉了,于是考虑图论建模,这里用到了一个重要的思想,当两个元素互相有影响的时候,我们考虑转化为一条边,视情况取无向或者有向边。

我们考虑对与一个二进制位,如过它仅在 aia_i 和 a−ja-j 中出现过,那么连边 (i,j)(i,j),这里先不去重。

于是我们枚举 aka_k,枚举相连的所有边,看看是否覆盖了所有点,如果没有,答案就是 aka_k,否则对这些点进行上述运算即可。这个的复杂度是 ∑k=lrdegk≤2log⁡V\sum_{k=l}^r deg_k\le2\log V,于是复杂度为 O(nlog⁡2V)O(n\log^2 V)。

这个做法带有较大的常数,所以并没有比小常数 O(nlog⁡3V)O(n\log^3 V) 快多少。