题目链接

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. 错误: 找不到或无法加载主类 Welcome.java

    问题原因: 不需要带.java

  2. VS2017 Asp.Net调式闪退处理

  3. 如果判断条件过多,可以直接在computed里面去返回需要判断的数据

    bad <div class="offer-item_margin" v-show="offer.supplierName || offer.supplierSto ...

  4. 2019暑假Java学习笔记(一)

    目录 基础语法(上) HelloWorld 变量 常量 数据类型 整数 浮点数 char类型 boolean类型 String 计算字符串长度 字符串比较 字符串连接 charAt()方法 字符串常用 ...

  5. 安装python 3.7

    安装 libressl-2.9.2 (SSL) sudo apt-get install libffi-dev (_ctypes) ldconfig -v wget https://www.pytho ...

  6. Spark2.x(六十):在Structured Streaming流处理中是如何查找kafka的DataSourceProvider?

    本章节根据源代码分析Spark Structured Streaming(Spark2.4)在进行DataSourceProvider查找的流程,首先,我们看下读取流数据源kafka的代码: Spar ...

  7. NGINX Cache Management (.imh nginx)

    In this article, we will explore the various NGINX cache configuration options, and tips on tweaking ...

  8. ai segmentation

    不只是医学图像啊,自然图像分割用unet也能取得非常好的效果.搞过kaggle语义分割的比赛carvana.感觉最好用的还是unet,其次linknet和提拉米苏也好用.large kernel也不错 ...

  9. Python实现PIL将图片转成字符串

    # -*- coding: utf-8 -*- # author:baoshan from PIL import Image, ImageFilter codeLib = '''@#$%&?* ...

  10. k8s记录-helm部署(九)

    helm 组件配置在 192.168.0.1 192.168.0.2 192.168.0.3 app 用户下操作tar xvf helm-v2.14.3-linux-amd64.tar.gzsudo ...