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

· · 题解

P14522 【MX-S11-T3】空之碎物

考虑对操作建树,每次让左儿子去减右儿子。记 $x$ 为从根一直向左走最后到的点,那么如果 $x$ 操作了至少 $2$ 次,那么调整成一次不劣,于是 $x$ 就是根的左儿子。再记 $y$ 为从根的右儿子一直向左走最后到的点,我们发现调整成让 $y$ 一个一个减去其他数不劣。 于是固定 $x, y$ 的情况下答案为 $x - x \text{ and } y \text{ and } \neg s$,$s$ 为集合中 $x, y$ 以外的数的或。只有当集合除去 $x$ 后每个数都有一个独特的位使得在该位下仅有该数和 $x$ 为 $1$ 的情况下,答案才取不到 $x$,所以区间长度 $> \log V$ 的答案就是区间内最大值,可以简单求出。对 $\le \log V$ 的区间暴力容易做到 $O(n \text{ poly} \log V)$。 再找点性质。发现即使区间长度 $\le \log V$,$x$ 大多还是取最大值,证明如下:当区间长度 $> 3$ 且答案取不到最大值时,最大值和次大值的 LCP 内的位,其他数一定都是 $0$,LCP 的后一位也至多只有一个数这一位上为 $1$,所以任取这三个数除外的任何一个数作为 $y$ 都可以使得上述答案 $\ge$ 次大值。 对长度 $\le 3$ 的区间暴力,长度 $\le \log V$ 的枚举区间最大值,则只需要考虑最大值左右各至多 $\log V$ 个数,且不能超出笛卡尔树上其子树区间。设两边分别有 $p, q$ 个数($p \le q$),枚举 $p$ 这边的端点,扫描另一边,精细实现可以做到 $O(p \log V)$。 总复杂度相当于笛卡尔树上每个点轻子树大小对 $\log V$ chkmin 后求和再乘上 $\log V$,是 $O(n\log V \log\log V)$ 的。 很幽默是吧,我也觉得。 代码: ```cpp //Date: 2025-11-16 11:33:18 #include <bits/stdc++.h> using namespace std; #define LL long long #define P emplace_back #define CLEAR(a, v) memset(a, (v), sizeof(a)) #define pii pair<int, int> #define fi first #define se second #define rep(i, a, b) for(int i = (a); i <= (b); i++) #define per(i, a, b) for(int i = (a); i >= (b); i--) char buf[1 << 20], *p1, *p2; #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++) inline int rd() { int s = 0, m = 0; char ch = getchar(); while(!isdigit(ch)) {if(ch == '-') m = 1; ch = getchar();} while(isdigit(ch)) s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar(); return m ? -s : s; } bool MBE; namespace SOLVER { const int N = 2e5 + 5, M = 998244353; int n, a[N], pre[N], nxt[N], st[N], top, b[N], id[25]; LL ans; int op(int x, int y) {return x & ~y;} void MAIN() { n = rd(); rep(i, 1, n) a[i] = rd(); st[0] = n + 1; per(i, n, 1) { while(top && a[st[top]] < a[i]) pre[st[top--]] = i; nxt[i] = st[top], st[++top] = i; } rep(i, 1, n) ans += (LL)(nxt[i] - i) * (i - pre[i]) * a[i]; rep(i, 1, n) { int ll = max(pre[i] + 1, i - 26), rr = min(nxt[i] - 1, i + 26); if(i - ll < rr - i) { per(l, i, ll) { int Mn = 1e9, lim = min(l + 26, rr), S = (1 << 25) - 1; CLEAR(id, 0); rep(j, l, lim) b[j] = a[j] & a[i]; rep(r, l, lim) { if(r != i) { for(int T = (b[r] &= S), c; T; T ^= c) { c = T & -T; int &x = id[__lg(c)]; if(!x) x = r; else Mn = min(Mn, b[x] ^= c), S ^= c; } Mn = min(Mn, b[r] &= S); } if(r >= i && r - l + 1 > 3) ans -= Mn; } } } else { rep(r, i, rr) { int Mn = 1e9, lim = max(r - 26, ll), S = (1 << 25) - 1; CLEAR(id, 0); rep(j, lim, r) b[j] = a[j] & a[i]; per(l, r, lim) { if(l != i) { for(int T = (b[l] &= S), c; T; T ^= c) { c = T & -T; int &x = id[__lg(c)]; if(!x) x = l; else Mn = min(Mn, b[x] ^= c), S ^= c; } Mn = min(Mn, b[l] &= S); } if(l <= i && r - l + 1 > 3) ans -= Mn; } } } } rep(i, 2, n) { int x = a[i], y = a[i - 1], z = a[i - 2]; if(x < y) swap(x, y); ans = ans - x + max(op(x, y), op(y, x)); if(i == 2) continue; if(y < z) swap(y, z); if(x < y) swap(x, y); ans = ans - x + max({op(x, op(y, z)), op(x, op(z, y)), op(y, op(z, x))}); } cout << ans % 998244353 << endl; } } bool MED; signed main() { freopen("heal.in", "r", stdin); freopen("heal.out", "w", stdout); // cin.tie(0), cout.tie(0), ios::sync_with_stdio(0); for(int tt = 1; tt; tt--) SOLVER::MAIN(); cerr << (&MBE - &MED) / 1024 << " KB, " << 1000 * clock() / CLOCKS_PER_SEC << " ms\n"; return 0; } ```