题意

类似超级钢琴,找最优解用可持久化trie。

code:

#include<bits/stdc++.h>
using namespace std;
#define re register
typedef long long ll;
const int maxn=5*1e5+10;
int n,m,tot;
int root[maxn],last[maxn*40*2];
int trie[maxn*40][2];
ll ans;
ll a[maxn],sum[maxn];
struct node
{
int x,l,r,t;ll val;
bool operator<(const node& a)const{return val<a.val;}
};
priority_queue<node>q;
inline ll read()
{
char c=getchar();ll res=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9')res=res*10+c-'0',c=getchar();
return res*f;
}
void insert(int pre,int now,int t,ll k,int id)
{
if(t<0){last[now]=id;return;}
int c=(k>>t)&1;
if(pre)trie[now][c^1]=trie[pre][c^1];
trie[now][c]=++tot;
insert(trie[pre][c],trie[now][c],t-1,k,id);
last[now]=max(last[trie[now][0]],last[trie[now][1]]);
}
int query(int now,int t,ll k,int lim)
{
if(t<0)return last[now];
int c=(k>>t)&1;
if(last[trie[now][c^1]]>=lim)return query(trie[now][c^1],t-1,k,lim);
else return query(trie[now][c],t-1,k,lim);
}
int main()
{
//freopen("test.in","r",stdin);
//freopen("test.out","w",stdout);
n=read(),m=read();
for(re int i=1;i<=n;i++)a[i]=read(),sum[i]=sum[i-1]^a[i];
root[0]=++tot;last[0]=-1;insert(0,root[0],35,0,0);
for(re int i=1;i<=n;i++)root[i]=++tot,insert(root[i-1],root[i],35,sum[i],i);
for(re int i=1;i<=n;i++)
{
int pos=query(root[n],35,sum[i-1],i);
//cerr<<(sum[pos]^sum[i-1])<<endl;
q.push((node){i,i,n,pos,sum[pos]^sum[i-1]});
}
while(m--)
{
node now=q.top();q.pop();
ans+=now.val;
if(now.t>now.l)
{
int pos=query(root[now.t-1],35,sum[now.x-1],now.l);
q.push((node){now.x,now.l,now.t-1,pos,sum[now.x-1]^sum[pos]});
}
if(now.t<now.r)
{
int pos=query(root[now.r],35,sum[now.x-1],now.t+1);
q.push((node){now.x,now.t+1,now.r,pos,sum[now.x-1]^sum[pos]});
}
}
printf("%lld",ans);
return 0;
}

luoguP5283 [十二省联考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. Luogu P5283 [十二省联考2019]异或粽子

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

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

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

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

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

  9. 洛谷.5283.[十二省联考2019]异或粽子(可持久化Trie 堆)

    LOJ 洛谷 考场上都拍上了,8:50才发现我读错了题=-= 两天都读错题...醉惹... \(Solution1\) 先求一遍前缀异或和. 假设左端点是\(i\),那么我们要在\([i,n]\)中找 ...

随机推荐

  1. 2019 CVPR 基于GAN的ImageCaptioning论文

    1.MSCap: Multi-Style Image Captioning with Unpaired Stylized Text 生成多种风格的caption 当前的image captioning ...

  2. Python 列表生成式 & 字典生成式

    Python 列表生成式 & 字典生成式 通过生成式可以更加简洁地生成列表和字典 列表生成式 对比 直接生成数据后加入列表示例: user_list = list() for i in ran ...

  3. linux shell攻略学习笔记二

    1.Cat命令 这么多命令,常用的 Cat –n file  显示文件以及行数 Cat - echo 'Text through stdin' | cat - file.txt Text throug ...

  4. IT兄弟连 HTML5教程 HTML5的基本语法 了解Web

    HTML也是计算机编程语言,但由于功能简单易用,不涉及业务逻辑,算是编程语言中最简单的了.其实学习HTML这门语言,就是在学习一个个HTML标记的使用,标记的名称和使用不是自定义的,它的功能及用法是已 ...

  5. 【MySQL报错】ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

    ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot exec ...

  6. 【Linux命令】id,usermod用户管理命令(包括/etc/passwd、shadow、group、gshadow文件)

    一.id命令 可以用来查看用户的UID.GID和附加组信息 id会显示用户以及所属群组的实际与有效ID.若两个ID相同,则仅显示实际ID.若仅指定用户名称,则显示目前用户的ID. 1.格式 id [O ...

  7. Java队列和定时器Timer

       一: Queue详解    Queue: 基本上,一个队列就是一个先入先出(FIFO)的数据结构    Queue接口与List.Set同一级别,都是继承了Collection接口.Linked ...

  8. MySQL-8.0.x DDL 原子性

    [1.mysql-8.0.x 新特性之 DDL 原子性] 在没有 DDL 原子性之前 DBA 对 DDL 语句基本上是无能为力的,比如说 DDL 执行的过程中停电了,这下就只有天知道了.实现上最终的愿 ...

  9. oracle学习笔记(十) 查询练习(一)

    查询练习一 表创建 create table employee as select * from soctt.emp ; --记得授权 sysdba用户登录 grant select on scott ...

  10. C# convert between Image and Base64string

    static void ImageMSDemo(string picPath) { byte[] imageArray = System.IO.File.ReadAllBytes(picPath); ...