CF1100F Ivan and Burgers

CF1100F Ivan and Burgers

静态区间,选取任意个数使得它们的异或和最大

\(n,\ m\leq5\times10^5,\ a_i\in[0,\ 10^6]\)

lxl ST表,线性基


如果暴力维护线性基,线段树时间复杂度为 \(O(n\log^2n)-O(\log^3n)\)

由于重复元素对答案没有影响,于是可以用 ST表 维护,时间复杂度为 \(O(n\log^3n)-O(\log^2n)\)

两种做法都无法通过本题。

如果沿用这个思路,瓶颈显然在线性基合并的 \(O(\log^2n)\) 上,无法再加优化

线段树做法显然无法再加拓展(和 lxl ST表时间复杂度一样的猫树空间复杂度多一只 \(\log\) ),于是考虑拓展 ST表 做法

常见的 RMQ 有 ST表 的 \(O(n\log n)-O(1)\) 的做法,但 \(O(n)-O(1)\) 的标准RMQ很难写、常数较大,且无法解决本题,于是可以考虑在随机数据下期望 \(O(n)-O(1)\) 的 lxl ST表

分块,大小设为 \(x\)

预处理每个块两端到块内每个点的前缀 \(\max\) 和后缀 \(\max\)

预处理块间ST表

若查询 \([l,\ r]\) ,且 \(l,\ r\) 分别在块 \(a,\ b\)

比如说我查l,r,这两个分别在块a,b中

则查块 \(a,\ b\) 之间的 RMQ ,以及 \(l\)\(a\) 块的后缀 \(\max\)\(r\)\(b\) 块的前缀 \(\max\)

\(l,\ r\) 在同一块中时,暴力求解

可以取 \(x=\log n\) ,当 \(l,\ r\) 不在同一块中时,这个算法是 \(O(1)\)

摘自 P3793 由乃救爷爷

如上维护,预处理块中前缀后缀线性基 \(O(n\log n)\) ,块间ST表线性基 \(O(\frac{n}{x}\log n\log^2n)=O(n\log^2n)\)

\(l,\ r\) 在同一块中,将 \([l,\ r]\) 中的元素暴力插入线性基, \(O(x\log n)=O(\log^2n)\) ;若 \(l,\ r\) 不在同一块中,合并前缀后缀块间三个线性基 \(O(\log^2n)\)

综上所述,时间复杂度 \(O(n\log^2n)\) ,空间复杂度 \(O(n\log n)\) 可能还需要卡卡常

代码

#include <bits/stdc++.h>
using namespace std;const int maxn = 5e5 + 10, maxm = 7850, base = 63;
int n, m, tot, lg[maxm], a[maxn];#define get(x) (((x) + base) >> 6)struct linear_base {int a[20];inline void clr() {memset(a, 0, sizeof a);}inline void ins(int x, int lim = 19) {for (int i = lim; ~i; --i) {if (x >> i & 1) {if (!a[i]) { a[i] = x; return; }x ^= a[i];}}}inline int query() {int res = 0;for (int i = 19; ~i; --i) {if ((res ^ a[i]) > res) res ^= a[i];}return res;}
} null, lef[maxn], rig[maxn], val[13][maxm];inline linear_base operator + (linear_base A, const linear_base &B) {for (int i = 19; ~i; --i) {if (B.a[i]) A.ins(B.a[i], i);}return A;
}const int maxn_r = maxn * 23, maxn_w = maxn * 8;
char buf_r[maxn_r], *now_r = buf_r;
char buf_w[maxn_w], *now_w = buf_w;inline int read() {int x = 0;while (*now_r < 48) ++now_r;while (*now_r > 47) x = (x << 3) + (x << 1) + (*now_r ^ 48), ++now_r;return x;
}inline void write(int x) {static char *tp, st[7];if (!x) *now_w = 48, ++now_w;for (tp = st; x; *++tp = x % 10 | 48, x /= 10);while (tp != st) *now_w = *tp, ++now_w, --tp;*now_w = 10, ++now_w;
}inline linear_base query(const int &l, const int &r) {if (l > r) return null;const int k = lg[r - l + 1];return val[k][l] + val[k][r - (1 << k) + 1];
}int main() {fread(buf_r, 1, maxn_r, stdin);n = read(), tot = get(n);for (int i = 1; i <= n; ++i) {a[i] = read();val[0][get(i)].ins(a[i]);}for (int i = 2; i <= tot; ++i) {lg[i] = lg[i >> 1] + 1;}linear_base lst;lst.clr();for (int i = 1; i <= n; ++i) {lst.ins(a[i]), lef[i] = lst;if (!(i & base)) lst.clr();}lst.clr();for (int i = n; i; --i) {if (!(i & base)) lst.clr();lst.ins(a[i]), rig[i] = lst;}for (int i = 1; i <= lg[tot]; ++i) {for (int j = 1; j + (1 << i) - 1 <= tot; ++j) {val[i][j] = val[i - 1][j] + val[i - 1][j + (1 << (i - 1))];}}m = read();register linear_base ans;for (int q = 1; q <= m; ++q) {const int l = read(), r = read();const int L = get(l), R = get(r);ans.clr();if (L == R) {for (register int i = l; i <= r; ++i) {ans.ins(a[i]);}} else {ans = rig[l] + lef[r] + query(L + 1, R - 1);}write(ans.query());}fwrite(buf_w, 1, now_w - buf_w, stdout);return 0;
}

转载于:https://www.cnblogs.com/Juanzhang/p/10877782.html

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

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

相关文章

深入浅出Nintex——更新PeopleandGroup类型的Field

转载于:https://www.cnblogs.com/mingle/archive/2011/11/25/2263199.html

从 vue-cli 源码中,我发现了27行读取 json 文件有趣的 npm 包

1. 前言大家好&#xff0c;我是若川。最近组织了源码共读活动&#xff0c;感兴趣的可以加我微信 ruochuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。已进行四个月了&#xff0c;很多小伙伴表示收获颇丰。想学源码&#xff0c;极力推荐订阅我写…

自定义view示例_自定义404页的10个示例(从最佳到最差)

自定义view示例自定义404页面 (Custom 404 pages) To customize or not to customize your 404 page? I hope by now you know the answer is that, yes, under essentially all circumstances you should customize your 404 page. 404 errors occur when someone attempts t…

BTF:实践指南

本文地址&#xff1a;BTF&#xff1a;实践指南 | 深入浅出 eBPF 1. BPF 的常见限制 1.1 调试限制1.2 可移植性2. BTF 是什么&#xff1f;3. BTF 快速入门 3.1 BPF 快速入门3.1 BTF 和 CO-RE4. 结论 BPF 是 Linux 内核中基于寄存器的虚拟机&#xff0c;可安全、高效和事件驱动…

python 混入类MixIn

写在前面 能把一件事情说的那么清楚明白&#xff0c;感谢廖雪峰的官方网站。 1.为什么要用混入类&#xff1f;&#xff08;小白入门&#xff09; 继承是面向对象编程的一个重要的方式&#xff0c;因为通过继承&#xff0c;子类就可以扩展父类的功能。 step1: 回忆一下Animal类层…

关于字符串流的学习(c++)

/* 字符串流 在字符数组中可以存放字符,也可以存放整数、浮点数以及其他类型的数据。在向字符数组存入数据之前,要先将数据从二进制形式转换为ASCII代码,然后存放在缓冲区,再从缓冲区送到字符数组。从字符数组读数据时,先将字符数组中的数据送到缓冲区,在赋给变量前要先将ASCII…

估计很多前端都没学过单元测试~

大家好&#xff0c;我是若川。最近组织了源码共读活动&#xff0c;感兴趣的可以加我微信 ruochuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。已进行四个月了&#xff0c;很多小伙伴表示收获颇丰。想学源码&#xff0c;极力推荐订阅我写的《学习…

xd可以用ui动效效果吗_通过动画使UI设计栩栩如生:Adobe XD和After Effects

xd可以用ui动效效果吗Note — If you don’t fancy splashing out on an Adobe license, you can trial their products for 14 days each. That should give you more than enough time to play, check it out.注意—如果您不愿意花钱购买Adobe许可证&#xff0c;则可以分别试…

BookMarklet:瑞士军刀你用了吗?

Bookmarklet 是一段隐藏在链接后面的js代码&#xff0c;可以收藏在收藏夹。通过这段代码&#xff0c;我们可以跨浏览器&#xff08;当然&#xff0c;也跨平台&#xff09;实现一些工具。比起浏览器插件来说&#xff0c;使用更加方便。典型的&#xff0c;dict.cn 网站的工具和有…

第十二周编程总结

这个作业属于那个课程C语言程序设计II这个作业要求在哪里https://pintia.cn/problem-sets/1127748174659035136/problems/1127749414029729792我在这个课程的目标是更好的学习函数这个作业在那个具体方面帮助我实现目锻炼了我的编程能力参考文献c语言程序设计26-1 计算最长的字…

可能是全网首个前端源码共读活动,诚邀加入学习

大家好&#xff0c;我是若川。从8月份到现在11月结束了。每周一期&#xff0c;一起读200行左右的源码&#xff0c;撰写辅助文章&#xff0c;截止到现在整整4个月了。由写有《学习源码整体架构系列》20余篇的若川【若川视野公众号号主】倾力组织&#xff0c;召集了各大厂对于源码…

现代游戏中的UX趋势

ux设计中的各种地图游戏UX (GAMES UX) Even though websites and games have matured side-by-side over the past few decades, games have a long and detailed history of user experience. Sure, it was scrappy and fairly rudimentary initially, but the only way you c…

SQL Server 2008 安装过程中遇到“性能计数器注册表”..

Windows 2008 系统 SQL Server 2008 性能计数器注册表作者&#xff1a; 来源&#xff1a; 时间&#xff1a;2010-6-13 完美集成、增强 KindEditor HTML 编辑器今天跟随部门老大去现场学习&#xff0c;安装 Windows208 下 SQL Server2008&#xff0c…

你提交代码前没有校验?巧用gitHooks解决

大家好&#xff0c;我是若川。最近组织了源码共读活动&#xff0c;感兴趣的可以加我微信 ruochuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。已进行四个月了&#xff0c;很多小伙伴表示收获颇丰。想学源码&#xff0c;极力推荐订阅我写的《学习…

Linux下自动化测试环境的搭建

1.安装Linux虚拟机&#xff0c;详情参考 https://blog.csdn.net/qq_22770715/article/details/78558374 https://www.cnblogs.com/Q277227/p/8176564.html 1.1 需要确定IP &#xff0c;使用 ifconfig 1.2 linux的用户名跟密码&#xff1b; 1.3 确定可以远程ssh登录&…

code craft_以Craft.io为先—关于我们行业的实践职业道路的系列

code craft重点 (Top highlight)For the past two decades, digital product design / UX has been shifting to become a more strategic discipline within organizations. Partially because business leaders have started to pay attention to how design-driven companie…

Nginx+httpd反代实现动静分离

什么是动静分离为了提高网站的响应速度&#xff0c;减轻程序服务器&#xff08;apachephp&#xff0c;nginxphp等&#xff09;的负载&#xff0c;对于静态资源比如图片&#xff0c;js&#xff0c;css&#xff0c;html等静态文件&#xff0c;我们可以在反向代理服务器中设置&…

(建议收藏)前端面试必问的十六条HTTP网络知识体系

大家好&#xff0c;我是若川。最近组织了源码共读活动&#xff0c;感兴趣的可以加我微信 ruochuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。已进行四个月了&#xff0c;很多小伙伴表示收获颇丰。想学源码&#xff0c;极力推荐订阅我写的《学习…

了解 DB2 Version 9.5 中的全局变量(转)

转自&#xff1a;http://www.ibm.com/developerworks/cn/data/library/techarticles/dm-0711zubiri/ 简介 在关系数据库系统内部&#xff0c;应用程序和实际数据库之间的主要交互都是以会话或连接的 SQL 语句形式来实现的。过去&#xff0c;为了在相同会话中实现不同 SQL 语句之…

jQuery新版本加载json注意事项。

jQuery在1.4版本后&#xff0c;采用了更为严格的json解析方式&#xff0c;所以所有内容都必须要有双引号。比如以前{key:”28CATEGORY”,status:”0″}是没问题的。但升级成1.4后&#xff0c;都必须加上双引号&#xff1a;{“key” : “28CATEGORY”,“status” : “0″}如果你…