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

news/2025/11/23 17:33:43/文章来源:https://www.cnblogs.com/P2441M/p/19261036

题意

定义 \(\ominus\) 为二进制不退位减法。对于一个可重集 \(S\),你可以进行若干次操作,每次操作可以选择 \(S\) 中的两个数 \(x,y\),合并成 \(x\ominus y\)\(y\ominus x\)。定义 \(f(S)\) 为将 \(S\) 合并至只剩一个数时,该数的最大可能值。

现在给定长度为 \(n\) 的序列 \(a\),求 \(\sum\limits_{i=1}^n\sum\limits_{j=i}^nf(a_{i\sim j})\),答案对 \(998244353\) 取模。\(1\leq n\leq 2\times 10^5\)\(0\leq a_i<2^{25}\)

题解

好难啊。

比较难注意到 \((x\ominus y)\ominus z\leq x\ominus(y\ominus z)\),放到表达式树上考虑,若其左链长度 \(>2\),则我们总可以通过做一次左旋使得结果不减,因此最终左链长度 \(\leq 2\)。而对于根节点的右子树对应的表达式,我们当然希望其值尽可能小,于是类似地,我们总可以通过右旋使得右子树的右链长度 \(\leq 2\)。这意味着,最优的表达式一定形如 \(x\ominus(y\ominus a_1\ominus\cdots\ominus a_{n-2})\),其中 \(x,y\)\(S\) 中的两个数,\(a_{1\sim n-2}\) 为剩下的数。

注意到 \(x\ominus y=x\operatorname{and}\neg y=x-(x\operatorname{and}y)\),所以 \(x\ominus(y\ominus a_1\ominus\cdots\ominus a_{n-2})=x-(x\operatorname{and}y\operatorname{and}\neg v)\),其中 \(v\)\(S\) 中剩余数的按位或和。一个观察是,当 \(|S|\geq \log{V}+2\) 时,\(f(S)\) 一定能取到 \(\max(S)\)

证明

\(x=\max(S)\),考虑怎样的 \(y\) 会使得 \(f(S)\) 取不到 \(\max(S)\),发现其实就是存在某一位,使得 \(x\) 在该位上为 \(1\),且 \(S-\{x\}\) 中只有 \(y\) 在这一位上为 \(1\)。而至多产生 \(\log{V}\) 个不能选择的 \(y\),所以当 \(|S|\geq \log{V}+2\) 时,一定能选出一个合适的 \(y\) 使得 \(f(S)=\max(S)\)\(\Box\)

有了这个结论,\(|S|\geq \log{V}+2\) 的情况就很容易处理了,用单调栈求出 \(a\) 的所有区间最大值之和,然后暴力减去 \(|S|<\log{V}+2\) 对应的区间最大值之和即可。这部分时间复杂度为 \(\mathcal{O}(n\log{V})\)

接下来考虑 \(|S|<\log{V}+2\) 的情况。暴力枚举短区间,枚举其中一个数作为 \(x\),预处理前后缀按位或和,还有以 \(x\) 为左右端点的区间按位或和,这样再枚举 \(y\) 既可以 \(\mathcal{O}(1)\) 计算出 \(x\ominus(y\ominus a_1\ominus\cdots\ominus a_{n-2})\) 的值。时间复杂度为 \(\mathcal{O}(n\log^3{V})\),无法通过。

考虑优化。一个很牛的结论是,\(x\) 必然取 \(S\) 的最大值或次大值。

证明

\(|S|\leq 2\) 时结论显然正确,下面讨论 \(|S|\geq 3\) 的情况。

反证法。设 \(mx,smx\) 分别为 \(S\) 的最大值和次大值,假设存在 \(p<smx\) 使得 \(f(S)\) 取到最大值。可以发现,若某一位上 \(1\) 的出现次数 \(\geq 3\),则这一位一定能在最终结果中取到 \(1\)。于是考察 \(mx,smx,p\) 在二进制表示下的 LCP 的后一位,则 \(mx\) 在这一位上必然为 \(1\)\(p\) 在这一位上必然为 \(0\)。而不管 \(smx\) 在该位上为 \(0\) 还是 \(1\),我们总可以取 \(x=mx,y=p\),使得最终结果在该位上取到 \(1\),从而使得 \(f(S)\) 取到更大的值,矛盾。\(\Box\)

这样复杂度降至 \(\mathcal{O}(n\log^2{V})\),可以通过。

代码
#include <bits/stdc++.h>using namespace std;#define lowbit(x) ((x) & -(x))
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
const int N = 2e5 + 5, MOD = 998244353;template<typename T> inline void chk_min(T &x, T y) { x = min(x, y); }
template<typename T> inline void chk_max(T &x, T y) { x = max(x, y); }
inline int add(int x, int y) { return x += y, x >= MOD ? x - MOD : x; }
inline int sub(int x, int y) { return x -= y, x < 0 ? x + MOD : x; }
inline void cadd(int &x, int y) { x += y, x < MOD || (x -= MOD); }
inline void csub(int &x, int y) { x -= y, x < 0 && (x += MOD); }int n, cur, ans, a[N];
int top, stk[N];
int pre1[N], suf1[N], pre2[N], suf2[N];int calc(int l, int r, int x) {pre1[l - 1] = suf1[r + 1] = pre2[x] = suf2[x] = 0;for (int i = l; i <= x; ++i) pre1[i] = pre1[i - 1] | a[i];for (int i = x + 1; i <= r; ++i) pre2[i] = pre2[i - 1] | a[i];for (int i = r; i >= x; --i) suf1[i] = suf1[i + 1] | a[i];for (int i = x - 1; i >= l; --i) suf2[i] = suf2[i + 1] | a[i];int res = 0, v = a[x];for (int i = l; i < x; ++i) chk_max(res, v - (v & a[i] & ~(pre1[i - 1] | suf2[i + 1] | suf1[x + 1])));for (int i = x + 1; i <= r; ++i) chk_max(res, v - (v & a[i] & ~(pre1[x - 1] | pre2[i - 1] | suf1[i + 1])));return res;
}int main() {ios::sync_with_stdio(0), cin.tie(0);cin >> n;for (int i = 1; i <= n; ++i) {cin >> a[i];while (top && a[stk[top]] <= a[i]) csub(cur, (ll)a[stk[top]] * (stk[top] - stk[top - 1]) % MOD), --top;stk[++top] = i, cadd(cur, (ll)a[i] * (i - stk[top - 1]) % MOD);cadd(ans, cur);}for (int l = 1; l <= n; ++l) for (int r = l, mx = 0, smx = 0; r <= min(l + 25, n); ++r) {if (!mx || a[r] >= a[mx]) smx = mx, mx = r;else if (!smx || a[r] >= a[smx]) smx = r;if (l < r) csub(ans, a[mx]), cadd(ans, max(calc(l, r, mx), calc(l, r, smx)));}cout << ans;return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/974078.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

10分钟,无需公网 IP!零门槛搭建 NapCatQQ 趣味 AI 人机,聊天互动超简单

10分钟,无需公网 IP!零门槛搭建 NapCatQQ 趣味 AI 人机,聊天互动超简单无需公网 IP 即可打造 QQ 智能人机:核心依赖 NapCat(接收 QQ 消息)与 AstrBot(提供 AI 能力)容器,通过 WebSocket 建立连接,配置硅基流…

1088. Rational Arithmetic (20)

1088. Rational Arithmetic (20)#include <iostream>using namespace std;long long getsame(long long a, long long b) {if(b != 0){return getsame(b, a % b);}else{return a;} }void simplify(long long &am…

1087. All Roads Lead to Rome (30)

1087. All Roads Lead to Rome (30)#include <iostream> #include <vector> #include <string.h>using namespace std;struct node {int next, cost; };vector<node> v[27000]; vector<int…

解码UDP

UDP 协议基础认知 UDP(User Datagram Protocol,用户数据报协议)是传输层核心协议之一,基于 IP 协议实现跨网络主机进程间的无连接数据传输。它面向事务提供简单通信服务,不保证数据交付、有序性和重复防护,也不提…

人工智能之数据分析 numpy:第六章 数组基本操作

人工智能之数据分析 numpy:第六章 数组基本操作人工智能之数据分析 numpy 第六章 数组基本操作@目录人工智能之数据分析 numpy前言一、修改数组形状(Reshaping)1. reshape()2. resize()3. ravel() 与 flatten()二、…

2025中山办公场地租赁优选:中山西区金嘉创新港,一站式创业空间,赋能企业成长新机遇

随着中山市产业升级与创新创业浪潮的蓬勃发展,优质办公空间已成为企业发展的重要基石。在2025年中山商业地产市场中,中山西区金嘉创新港凭借多元化的空间解决方案、完善的配套服务体系及卓越的区位优势,成为各类企业…

国产数据库替代MongoDB:政务电子证照新选择 - 教程

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

读书笔记《投资的未来》,估算收益率

比较IBM和新泽西标准石油两家公司,一个新兴的,受追捧的,一个传统的,但结果,是石油公司胜出。尽管两只股票的业绩都不错,但是1950~2003年,新泽西标准石油的投资者每年可以取得14.42%的年收益率,这比IBM提供的1…

使用代码查询快递信息的方法(与查询天气的方式雷同)

第一步:在标签中添加JS文件具体内容如下:第二步:写出大概的框架第三步:写JS部分(1)定义appkey和API地址(2)校验输入和显示加载状态(3)调用API和解析返回数据(4)展示拼接内容和判断内容是否正确第四步:保存并运行查…

1101. Quick Sort (25)

1101. Quick Sort (25)#include <iostream> #include <vector> #include <algorithm>using namespace std;int num[100010], low[100010], high[100010];int main() {int n;scanf("%d", &…

1100. Mars Numbers (20)

1100. Mars Numbers (20)#include <iostream> #include <string.h>using namespace std;char ch[2][13][5] = {"tret", "jan", "feb", "mar", "apr",…

解码网络编程基础

进程间通信方式 基础概念 程序是数据和指令的集合,运行时成为进程,操作系统会为其分配资源并记录参数。同一主机内进程通信可通过管道、信号、消息队列、信号量集、共享内存实现,这些方式依赖主机本地系统资源,无法…

C++的3种继承方式

C++的3种继承方式 在 C++ 中,继承方式(public、protected、private)决定了基类成员在派生类中的访问权限,以及派生类对象对基类成员的访问权限。正确选择继承方式是实现封装、复用和多态的关键。以下是三种继承方式…

1082. Read Number in Chinese (25)

1082. Read Number in Chinese (25)#include <iostream> #include <string.h>using namespace std;int first = 1;void setfirst() {if(first == 1){first = 0;}else{printf(" ");} }int main()…

1081. Rational Sum (20)

1081. Rational Sum (20)#include <iostream>using namespace std;long long getsame(long long a, long long b) {if(b != 0){return getsame(b, a % b);}else{return a;} }void simplify(long long &a, lo…

1067. Sort with Swap(0) (25)

1067. Sort with Swap(0) (25)#include <iostream>using namespace std;int index[100010], num[100010];int main() {int n;scanf("%d", &n);int i, count = 0;for(i = 0; i < n; i++){scanf(…

1066. Root of AVL Tree (25)

1066. Root of AVL Tree (25)#include <iostream> #include <stdlib.h>using namespace std;typedef struct node {int key, bf;struct node *lchild, *rchild; }*bnode;void rrotate(bnode *root) {bnode…

1070. Mooncake (25)

1070. Mooncake (25)#include <iostream> #include <algorithm>using namespace std;struct node {double amounts, prices, perprice; }mooncakes[1010];int cmp(node n1, node n2) {return n1.perprice …

1069. The Black Hole of Numbers (20)

1069. The Black Hole of Numbers (20)#include <iostream> #include <string.h> #include <algorithm>using namespace std;int cmp(char a, char b) {return a > b; }int main() {int num[2];s…

1050. String Subtraction (20)

1050. String Subtraction (20)#include <iostream> #include <string.h>using namespace std;int flag[130]; char s[2][10010];int main() {int i, len[2];for(i = 0; i <= 1; i++){gets(s[i]);len[i…