字典树可以$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: 异或之的更多相关文章

  1. bzoj 3689: 异或之 Trie+堆

    题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=3689 题解: 利用一个优先队列存储当前取到的数 然后再写一颗支持查找异或的k大值的Tri ...

  2. BZOJ 3689 异或 Trie木+堆

    标题效果:特定n的数量,这种需求n数22 XOR的值前者k少 首先,我们建立了一个二进制的所有数字Trie木,您可以使用Trie木size域检查出一些其他的数字XOR值首先k少 然后,我们要保持一个堆 ...

  3. BZOJ 3689 异或之 (可持久化01Trie+堆)

    题目大意:给你一个序列,求出第$K$大的两两异或值 先建出来可持久化$01Trie$ 用一个$set$/堆存结构体,存某个异或对$<i,j>$的第二关键字$j$,以及$ai\;xor\;a ...

  4. BZOJ 3689: 异或之 可持久化trie+堆

    和超级钢琴几乎是同一道题吧... code: #include <bits/stdc++.h> #define N 200006 #define ll long long #define ...

  5. 异或之(bzoj 3689)

    Description 给定n个非负整数A[1], A[2], --, A[n].对于每对(i, j)满足1 <= i < j <= n,得到一个新的数A[i] xor A[j],这 ...

  6. [BZOJ 4671]异或图

    Description 题库链接 给定 \(s\) 个结点数相同且为 \(n\) 的图 \(G_1\sim G_s\) ,设 \(S = \{G_1, G_2,\cdots , G_s\}\) ,问 ...

  7. BZOJ 4671 异或图 | 线性基 容斥 DFS

    题面 Description 定义两个结点数相同的图 G1 与图 G2 的异或为一个新的图 G, 其中如果 (u, v) 在 G1 与 G2 中的出现次数之和为 1, 那么边 (u, v) 在 G 中 ...

  8. bzoj 2466 异或方程组

    对于每个灯,我们用一个变量表示其决策,xu=0表示不选,xu=1表示选.因为每个灯最后必须都亮,所以每个等都对应一个异或方程. 解这个异或方程组,有几种情况: 1.存在唯一解(得到的上三角系数矩阵的主 ...

  9. bzoj 4671 异或图——容斥+斯特林反演+线性基

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4671 考虑计算不是连通图的方案,乘上容斥系数来进行容斥. 可以枚举子集划分(复杂度是O(Be ...

随机推荐

  1. 第二百零九节,jQuery EasyUI,Pagination(分页)组件

    jQuery EasyUI,Pagination(分页)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 Pagination(分页)组件的使 ...

  2. XML简单学习

    XML简单概述 1.Extensible Markup language可扩展标记语言; 2.作用:具有层次性的描述有关系的数据: 体现在:描述数据关系:软件配置,以描述程序模块之间的关系: 语法介绍 ...

  3. 第8步:安装Oracle

    安装Oracle 注意,安装Oracle时需要以oracle用户身份执行,在那之前需要以root身份执行xhost+,即命令: 代码1 [root@sgdb1~]# xhost+ [root@sgdb ...

  4. win10 更新导致 VisualSVN 报0x80041024错错误

    主要是WMI丢失,重新注册下就好. 运行cmd.exe, 运行语句:mofcomp "%VISUALSVN_SERVER%WMI\VisualSVNServer.mof" 修复即可 ...

  5. 如何正确使用Google搜索

    “” 双引号表示完全匹配,结果中必须出现与搜索文本完全相同的内容 A -B 搜索包含A但不包含B的结果(请注意A后面的空格不能省略) filetype 搜索对应类型的文件.例如:中国防火墙 filet ...

  6. Android UI开发第三十九篇——Tab界面实现汇总及比较

    Tab布局是iOS的经典布局,Android应用中也有大量应用,前面也写过Android中TAb的实现,<Android UI开发第十八篇——ActivityGroup实现tab功能>.这 ...

  7. iOS 程序切换后台

    1. -(void)animationFinished:(NSString*)animationid finished:(NSNumber*)finished context:(void*)conte ...

  8. 10013: An attempt was made to access a socket in a way forbidden by its access permissions

    nginx的error.log日志报错: 2018/01/25 11:55:22 [emerg] 3380#15488: bind() to 0.0.0.0:20003 failed (10013: ...

  9. [LintCode] 全排列

    递归实现: class Solution { public: /** * @param nums: A list of integers. * @return: A list of permutati ...

  10. [LeetCode] Reverse Lists

    Well, since the head pointer may also be modified, we create a new_head that points to it to facilit ...