BZOJ 3689: 异或之
字典树可以$o(logn)查找第k大$
使用$可持久化Trie 区间查找第k大,然后首先把每个数异或之后的最小丢进小根堆中,然后一个一个取出,取出后就再丢次小,一共取k次$
总的时间复杂度为$O(klogn)$
本来的考虑是 先找出第k大,然后在$Trie上DFS把小于这个数的全丢进vector 然后发现会有很多无用状态会搜索到,T掉$
#include <bits/stdc++.h>
using namespace std; #define N 100010
int n, k, arr[N], kth;
struct node
{
int base, num, pos, l, r;
node () {}
node (int base, int num, int pos, int l, int r) : base(base), num(num), pos(pos), l(l), r(r) {}
bool operator < (const node &r) const { return num > r.num; }
};
priority_queue <node> q; namespace TRIE
{
int rt[N], cnt;
struct node
{
int son[], cnt;
}a[N * ];
void init()
{
a[].son[] = a[].son[] = a[].cnt = ;
cnt = ;
}
void insert(int &now, int pre, int x)
{
now = ++cnt;
a[now] = a[pre];
int root = now; ++a[root].cnt;
for (int i = ; i >= ; --i)
{
int id = (x >> i) & ;
a[++cnt] = a[a[root].son[id]];
++a[cnt].cnt;
a[root].son[id] = cnt;
root = cnt;
}
}
int find_kth(int x, int k, int l, int r)
{
int res = ;
int tl = rt[l], tr = rt[r];
for (int s = ; s >= ; --s)
{
int id = (x >> s) & ;
int sum = a[a[tr].son[id]].cnt - a[a[tl].son[id]].cnt;
if (sum < k)
{
k -= sum;
res |= << s;
tr = a[tr].son[id ^ ];
tl = a[tl].son[id ^ ];
}
else
{
tr = a[tr].son[id];
tl = a[tl].son[id];
}
}
return res;
}
} int main()
{
while (scanf("%d%d", &n, &k) != EOF)
{
for (int i = ; i <= n; ++i) scanf("%d", arr + i);
TRIE::init();
for (int i = ; i <= n; ++i) TRIE::insert(TRIE::rt[i], TRIE::rt[i - ], arr[i]);
for (int i = ; i < n; ++i) q.push(node(arr[i], TRIE::find_kth(arr[i], , i, n), , i, n));
for (int i = ; i <= k; ++i)
{
node top = q.top(); q.pop();
printf("%d%c", top.num, " \n"[i == k]);
q.push(node(top.base, TRIE::find_kth(top.base, top.pos, top.l, top.r), top.pos + , top.l, top.r));
}
}
return ;
}
BZOJ 3689: 异或之的更多相关文章
- bzoj 3689: 异或之 Trie+堆
题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=3689 题解: 利用一个优先队列存储当前取到的数 然后再写一颗支持查找异或的k大值的Tri ...
- BZOJ 3689 异或 Trie木+堆
标题效果:特定n的数量,这种需求n数22 XOR的值前者k少 首先,我们建立了一个二进制的所有数字Trie木,您可以使用Trie木size域检查出一些其他的数字XOR值首先k少 然后,我们要保持一个堆 ...
- BZOJ 3689 异或之 (可持久化01Trie+堆)
题目大意:给你一个序列,求出第$K$大的两两异或值 先建出来可持久化$01Trie$ 用一个$set$/堆存结构体,存某个异或对$<i,j>$的第二关键字$j$,以及$ai\;xor\;a ...
- BZOJ 3689: 异或之 可持久化trie+堆
和超级钢琴几乎是同一道题吧... code: #include <bits/stdc++.h> #define N 200006 #define ll long long #define ...
- 异或之(bzoj 3689)
Description 给定n个非负整数A[1], A[2], --, A[n].对于每对(i, j)满足1 <= i < j <= n,得到一个新的数A[i] xor A[j],这 ...
- [BZOJ 4671]异或图
Description 题库链接 给定 \(s\) 个结点数相同且为 \(n\) 的图 \(G_1\sim G_s\) ,设 \(S = \{G_1, G_2,\cdots , G_s\}\) ,问 ...
- BZOJ 4671 异或图 | 线性基 容斥 DFS
题面 Description 定义两个结点数相同的图 G1 与图 G2 的异或为一个新的图 G, 其中如果 (u, v) 在 G1 与 G2 中的出现次数之和为 1, 那么边 (u, v) 在 G 中 ...
- bzoj 2466 异或方程组
对于每个灯,我们用一个变量表示其决策,xu=0表示不选,xu=1表示选.因为每个灯最后必须都亮,所以每个等都对应一个异或方程. 解这个异或方程组,有几种情况: 1.存在唯一解(得到的上三角系数矩阵的主 ...
- bzoj 4671 异或图——容斥+斯特林反演+线性基
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4671 考虑计算不是连通图的方案,乘上容斥系数来进行容斥. 可以枚举子集划分(复杂度是O(Be ...
随机推荐
- ASP.NET中JSON对时间进行序列化和反序列化
JSON格式不直接支持日期和时间.DateTime值显示为“/Date(0+0800)/”形式的JSON字符串,其中第一个数字是GMT时区中自1970年1月1 日午夜以来按正常时间(非夏令时)经过的毫 ...
- ubuntu1204-gedit中文乱码
1 在界面上使用ALT-F2打开"执行应用程序"界面. 2 输入dconf-editor.然后点击"执行"打开"Configuration Edito ...
- [MachineLearning]KNN
# -*- coding: utf-8 -*- """ Created on Wed Jun 18 11:46:15 2014 @author: hp "&qu ...
- wpf datagrid 中按钮 动态显示
/若datagrid出现滚动条可能会出现问题需要加上下面俩句话.//因为出滚动条时,为了显示加速,datagrid会加载一部分数据.另一些数据当滚动时在加载进去 这样初始化获取不到这些数据.//在da ...
- Multiview
新建3个ViewController的类 新建main.stroyboard,并配置好相应的布局(编辑界面,连接视图与类,ViewController的Storyboard ID,连接3个Button ...
- 57、Design Support Library 介绍及环境搭建
一.Material Design几个要素 扁平化.简洁: 水波反馈: 良好体验的过渡动画: 材料空间位置的直观变化: 二.Android Studio配置 在 build.gradle 文件中加入, ...
- centos7的nfs配置
author : headsen chen date : 2018-04-12 09:40:14 一,服务端安装和配置: 环境准备: systemctl stop firewalld system ...
- 解决scipy安装(pip install scipy)失败,以及其他问题
解决scipy安装(pip install scipy)失败,以及其他问题 解决: 1.在scipy官方库中并没有适合Windows的python3.6相关版本,故需要在网址http://www.lf ...
- 巨蟒python全栈开发flask2
内容回顾: 上节回顾: Flask .response 三剑客: render_template 模板 redirect 重定向 - URL地址 "" 字符串 HTTPRespon ...
- SET NAMES 'charset_name'
设置写入db和db返回读出结果的字符集character set https://dev.mysql.com/doc/refman/5.7/en/charset-connection.html SET ...