我们需要知道一个事实,trie树上是可以要求第k大的!

我们每个节点记个size值然后像其他数据结构一样维护就可以了

然后我们再搞个priority_queue什么的就好了,注意每个值会出现两次只要记一次

 /**************************************************************
Problem: 3689
User: rausen
Language: C++
Result: Accepted
Time:4328 ms
Memory:44524 kb
****************************************************************/ #include <cstdio>
#include <queue> using namespace std;
const int N = 1e5 + ;
const int Tot_sz = N * ; struct trie_node {
trie_node *son[];
int sz;
} *null, *trie_root, mempool[Tot_sz], *cnt_trie = mempool; struct data {
int x, y, k;
data() {}
data(int _x, int _y, int _k) : x(_x), y(_y), k(_k) {} inline bool operator < (const data &a) const {
return x == a.x ? (y == a.y ? k > a.k : y > a.y) : x > a.x;
}
}; int n, m;
int a[N];
priority_queue <data> h; inline int read() {
int x = ;
char ch = getchar();
while (ch < '' || '' < ch)
ch = getchar();
while ('' <= ch && ch <= '') {
x = x * + ch - '';
ch = getchar();
}
return x;
} #define Son p -> son
#define Sz p -> sz
inline void trie_null(trie_node *&p) {
p = cnt_trie;
Son[] = Son[] = p;
Sz = ;
} inline void trie_new(trie_node *&p) {
p = ++cnt_trie, *p = *null;
} inline void trie_insert(int x) {
static trie_node *p;
static int i, t;
for (p = trie_root, ++Sz, i = ; ~i; --i) {
t = x >> i & ;
if (Son[t] == null) trie_new(Son[t]);
p = Son[t], ++Sz;
}
} inline int trie_query(int x, int rank) {
static trie_node *p;
static int i, t, res;
for (p = trie_root, i = , res = ; ~i; --i) {
t = x >> i & ;
if (rank <= Son[t] -> sz) p = Son[t];
else {
rank -= Son[t] -> sz;
res |= << i;
p = Son[!t];
}
}
return res;
}
#undef Son
#undef Sz int main() {
int i, x, y, k;
n = read(), m = read();
trie_null(null);
trie_new(trie_root);
for (i = ; i <= n; ++i) {
a[i] = read();
trie_insert(a[i]);
}
for (i = ; i <= n; ++i)
h.push(data(trie_query(a[i], ), a[i], ));
for (i = ; i < * m; ++i) {
x = h.top().x, y = h.top().y, k = h.top().k;
if (i & ) {
if (i != ) putchar(' ');
printf("%d", x);
}
h.pop();
h.push(data(trie_query(y, k + ), y, k + ));
}
return ;
}

(p.s. 注意会PE...然后改了一下输出边WA了QAQ...第三次才改对...)

BZOJ3689 异或之的更多相关文章

  1. bzoj2006 [NOI2010]超级钢琴 (及其拓展)

    bzoj2006 [NOI2010]超级钢琴 给定一个序列,求长度在 \([L,\ R]\) 之间的区间和的前 \(k\) 大之和 \(n\leq5\times10^5,\ k\leq2\times1 ...

  2. HBSX2019 游记

    Day -4 训练戳SX2019 3月训练 ZJOI2019 Day1几天前就考了 T1真考了麻将QwQ 九条可怜的毒瘤真的是业界良心 今天中午才起,要开始调整生物钟了 9012HBOIers群里讨论 ...

  3. [您有新的未分配科技点]可,可,可持久化!?------0-1Trie和可持久化Trie普及版讲解

    这一次,我们来了解普通Trie树的变种:0-1Trie以及在其基础上产生的可持久化Trie(其实,普通的Trie也可以可持久化,只是不太常见) 先简单介绍一下0-1Trie:一个0-1Trie节点只有 ...

  4. 【BZOJ3689】异或之 堆+可持久化Trie树

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

  5. 【bzoj3689】异或之 可持久化Trie树+堆

    题目描述 给定n个非负整数A[1], A[2], ……, A[n].对于每对(i, j)满足1 <= i < j <= n,得到一个新的数A[i] xor A[j],这样共有n*(n ...

  6. Android数据加密之异或加密算法

    前言: 这几天被公司临时拉到去做Android IM即时通信协议实现,大致看了下他们定的协议,由于之前没有参与,据说因服务器性能限制,只达成非明文传递,具体原因我不太清楚,不过这里用的加密方式是采用异 ...

  7. Oracle数据库异机升级

    环境: A机:RHEL5.5 + Oracle 10.2.0.4 B机:RHEL5.5 需求: A机10.2.0.4数据库,在B机升级到11.2.0.4,应用最新PSU补丁程序. 目录: 一. 确认是 ...

  8. [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  9. [PHP][位转换积累]之异或运算的简单加密应用

    异或的符号是^.按位异或运算, 对等长二进制模式按位或二进制数的每一位执行逻辑按位异或操作. 操作的结果是如果某位不同则该位为1, 否则该位为0. xor运算的逆运算是它本身,也就是说两次异或同一个数 ...

随机推荐

  1. CSS笔记(六)链接

    参考:http://www.w3school.com.cn/css/css_link.asp 链接的四种状态 ① a:link - 普通的.未被访问的链接 ② a:visited - 用户已访问的链接 ...

  2. Java中正则Matcher类的matches()、lookAt()和find()的区别<转>

    在Matcher类中有matches.lookingAt和find都是匹配目标的方法,但容易混淆,整理它们的区别如下: matches:整个匹配,只有整个字符序列完全匹配成功,才返回True,否则返回 ...

  3. [转]ubuntu下安装程序的三种方法

    出处:http://www.cnblogs.com/xwdreamer/p/3623454.html 引言 在ubuntu当中,安装应用程序我所知道的有三种方法,分别是apt-get,dpkg安装de ...

  4. 12 Using_explain_plan

    The row source tree is the core of the execution plan. The tree shows the following information: An ...

  5. Python学习笔记11—函数

    建立第一个函数 /usr/bin/env Python #coding:utf-8 def add_function(a,b): c = a+b print c if __name__==" ...

  6. java语法糖3 深入剖析Java中的装箱和拆箱

    装箱 在Java SE5之前,如果要生成一个数值为10的Integer对象,必须这样进行: Integer i = new Integer(10); 而在从Java SE5开始就提供了自动装箱的特性, ...

  7. Linux下利用rsync实现多服务器文件同步

    windows做为文件服务器,使用rsync的windows服务版本,然后配置好就可以了.需要的朋友可以参考下. windows做为文件服务器,使用rsync的windows服务版本:cwRsyncS ...

  8. ORA-12705: Cannot access NLS data files or invalid environment specified

    ASM实例无法启动 [grid@data ~]$ sqlplus / as sysasm SQL*Plus: Release 11.2.0.4.0 Production on Fri Sep 11 0 ...

  9. hiho_1081_最短路径1

    题目 最短路模板题目,纯练习手速. 实现 #include<iostream> #include<string.h> #include<iostream> #inc ...

  10. hiho_1068_RMQ_st算法

    题目 给出一数组A,编号从1到n,然后进行q次查询,每次查询给出一个边界[beg, end],要求给出数组A中范围[beg, end]之内的最小值. 题目链接: RMQ_ST 分析 区间问题使用线段树 ...