题目链接

LOJ:https://loj.ac/problem/3048

洛谷:https://www.luogu.org/problemnew/show/P5283

Solution

考虑每个子串都是一个前缀的后缀,我们可以用堆维护四元组\((l,r,ed,pos)\)表示当前右端点为\(ed\),左端点范围是\([l,r]\),其中\([pos+1,ed]\)这个区间的异或和最大。

这其实就是固定了前缀来找后缀。那么我们每次可以从堆顶拿一个四元组出来,然后分裂成两个:\((l,pos-1,ed,\cdots),(pos+1,r,ed,\cdots)\),顺便更新答案。其中省略号的部分可以通过可持久化\(\rm 01trie\)树算出来。

复杂度\(O(n\log n+k\log n)\)。

#include<bits/stdc++.h>
using namespace std; #define int unsigned int template <class T> void read(T &x) {
x=0;T f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
} template <class T> void print(T x) {
if(x<0) putchar('-'),x=-x;
if(!x) return ;print(x/10),putchar(x%10+48);
}
template <class T> void write(T x) {if(!x) putchar('0');else print(x);putchar('\n');} #define lf double
#define ll long long #define pii pair<int,int >
#define vec vector<int > #define pb push_back
#define mp make_pair
#define fr first
#define sc second #define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++) const int maxn = 5e5+10;
const int inf = 1e9;
const lf eps = 1e-8;
const int mod = 1e9+7;
const int maxm = 2e7+10; int a[maxn],n,k; struct trie {
int son[maxm][2],tot,cnt[maxm],id[maxm],rt[maxn]; void ins(int r,int x) {
rt[r]=++tot;int now=rt[r],pre;if(r) pre=rt[r-1];else pre=0;
for(int i=31;~i;i--) {
int t=x>>i&1;
son[now][t^1]=son[pre][t^1];
now=(son[now][t]=++tot);
pre=son[pre][t];cnt[now]=cnt[pre]+1;
}id[now]=r;
} int query(int l,int r,int x) {
int now=rt[r],pre=0;if(l) pre=rt[l-1];
for(int i=31;~i;i--) {
int t=!(x>>i&1);
if(cnt[son[now][t]]-cnt[son[pre][t]]==0) t^=1;
now=son[now][t],pre=son[pre][t];
}return id[now];
}
}T; struct data {
int l,r,ed,pos; data () {} data (int _l,int _r,int _ed) {
l=_l,r=_r,ed=_ed;
pos=T.query(l,r,a[ed]);
} bool operator < (const data &rhs) const {
return (a[ed]^a[pos])<(a[rhs.ed]^a[rhs.pos]);
}
}; priority_queue<data > s; signed main() {
read(n),read(k);T.ins(0,0);
for(int i=1;i<=n;i++) read(a[i]),a[i]^=a[i-1],T.ins(i,a[i]);
for(int i=1;i<=n;i++) s.push(data(0,i-1,i));
ll ans=0;
for(int i=1;i<=k;i++) {
if(s.empty()) break;
data x=s.top();s.pop();
ans+=a[x.ed]^a[x.pos];
if(1ll*x.pos-1>=1ll*x.l) s.push(data(x.l,x.pos-1,x.ed));
if(x.r>=x.pos+1) s.push(data(x.pos+1,x.r,x.ed));
}write(ans);
return 0;
}

[LOJ3048] [十二省联考2019] 异或粽子的更多相关文章

  1. [十二省联考2019]异或粽子——可持久化trie树+堆

    题目链接: [十二省联考2019]异或粽子 求前$k$大异或区间,可以发现$k$比较小,我们考虑找出每个区间. 为了快速得到一个区间的异或和,将原序列做前缀异或和. 对于每个点作为右端点时,我们维护出 ...

  2. 【BZOJ5495】[十二省联考2019]异或粽子(主席树,贪心)

    [BZOJ5495][十二省联考2019]异或粽子(主席树,贪心) 题面 BZOJ 洛谷 题解 这不是送分题吗... 转异或前缀和,构建可持久化\(Trie\). 然后拿一个堆维护每次的最大值,每次如 ...

  3. [十二省联考2019]异或粽子 01trie

    [十二省联考2019]异或粽子 01trie 链接 luogu 思路 首先求前k大的(xo[i]^xo[j])(i<j). 考场上只想到01trie,不怎么会写可持久,就写了n个01trie,和 ...

  4. 【简】题解 P5283 [十二省联考2019]异或粽子

    传送门:P5283 [十二省联考2019]异或粽子 题目大意: 给一个长度为n的数列,找到异或和为前k大的区间,并求出这些区间的异或和的代数和. QWQ: 考试时想到了前缀异或 想到了对每个数按二进制 ...

  5. Luogu P5283 / LOJ3048 【[十二省联考2019]异或粽子】

    联考Day1T1...一个考场上蠢了只想到\(O(n^2)\)复杂度的数据结构题 题目大意: 求前\(k\)大区间异或和的和 题目思路: 真的就是个sb数据结构题,可持久化01Trie能过(开O2). ...

  6. 洛谷P5283 & LOJ3048:[十二省联考2019]异或粽子——题解

    https://www.luogu.org/problemnew/show/P5283 https://loj.ac/problem/3048 小粽是一个喜欢吃粽子的好孩子.今天她在家里自己做起了粽子 ...

  7. Luogu P5283 [十二省联考2019]异或粽子

    感觉不是很难的一题,想了0.5h左右(思路歪了,不过想了一个大常数的两只\(\log\)做法233) 然后码+调了1h,除了一个SB的数组开小外基本上也没什么坑点 先讲一个先想到的方法,我们对于这种问 ...

  8. [十二省联考2019]异或粽子(堆+可持久化Trie)

    前置芝士:可持久化Trie & 堆 类似于超级钢琴,我们用堆维护一个四元组\((st, l, r, pos)\)表示以\(st\)为起点,终点在\([l, r]\)内,里面的最大值的位置为\( ...

  9. Luogu5283 十二省联考2019异或粽子(trie/可持久化trie+堆)

    做前缀异或和,用堆维护一个五元组(x,l,r,p,v),x为区间右端点的值,l~r为区间左端点的范围,p为x在l~r中最大异或和的位置,v为该最大异或和,每次从堆中取出v最大的元素,以p为界将其切成两 ...

随机推荐

  1. element ui,input框输入时enter健进行搜索

    <el-form-item label="企业名称"> <el-input v-model="formSearch.kw" @keyup.en ...

  2. Redis 下载 安装

    Redis 官网 https://redis.io/ github 主页 https://github.com/antirez/redis 下载页面 https://redis.io/download ...

  3. slot 插槽子组件向父组件传值

    slot 插槽要实现子组件向父组件传值,则需要运用 作用域插槽 1.父组件中用 标签加上 slot-scoped 的属性,属性值随性.(旧版本是scope,vue新版本必须用slot-scope) 2 ...

  4. Android -------- BouncingJellyView 果冻视图(阻尼效果)

    分享一个不错的效果,分享给大家 BouncingJellyView 果冻视图,就像果冻一样伸缩弹跳,也叫阻尼效果.这个效果在MIUI上面到处都可以看到. 效果图: 使用 项目更目录bulid.grad ...

  5. JQuery selector form input

    var inputPhoneInFormActivity = $("form#formtab input[name='phone']"); if(inputPhoneInFormA ...

  6. LoadRunner之录制你的第一个脚本

    LoadRunner安装完成之后,肯定就迫不及待的想要上手试用了.下面就是讲一下LR脚本录制的流程和基本的设置. 1.先放一张脚本录制以及运行的流程图 2.脚本录制步骤 1)以管理员身份打开LR软件, ...

  7. SpringBoot过滤XSS脚本攻击

    XSS攻击是什么 XSS攻击全称跨站脚本攻击,是为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS,XSS是一种在web应用中的计算机安 ...

  8. RedisHelper Redis帮助类

    using StackExchange.Redis; using System; using System.Collections.Generic; using System.IO; using Sy ...

  9. 【前端】input输入框只能输入大于等于0的正数

    大于等于0的正数,允许小数 <input type="number" step="1" min="0" onkeyup="t ...

  10. [报错处理]Python Requests - No connection adapters

    出错信息很清楚:Python请求 - 没有连接适配器. 你得把网络协议加进入网址: http://192.168.1.61:8080/api/call 没有 http:// 请求不知道如何连接远程. ...