标题效果:特定n的数量,这种需求n数22 XOR的值前者k少

首先,我们建立了一个二进制的所有数字Trie木,您可以使用Trie木size域检查出一些其他的数字XOR值首先k少

然后,我们要保持一个堆。其他XOR的整数值首先2增加堆(第一小是自己异或自己。不在题目要求范围内)。当取出一个数异或值的第k小后,将第k+1小增加堆

一个异或值会被两个数分别取出一次。所以取出奇数次时输出,取2*k次就可以

时间复杂度O(nlogn)

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define M 100100
using namespace std;
typedef pair<int, pair<int,int> > abcd;
struct Trie{
int siz;
Trie *son[2];
Trie();
}*null=new Trie,*root=null;
Trie :: Trie()
{
siz=0;
son[0]=son[1]=null;
}
int n,a[M];
abcd heap[M];
int top;
void Push(abcd x)
{
heap[++top]=x;
int t=top;
while( t>1 && heap[t]<heap[t>>1] )
swap(heap[t],heap[t>>1]),t>>=1;
}
void Pop()
{
heap[1]=heap[top--];
int t=2;
while( t<=top )
{
if( t<top && heap[t+1]<heap[t] )
++t;
if( heap[t]<heap[t>>1] )
swap(heap[t],heap[t>>1]),t<<=1;
else
break;
}
}
void Insert(Trie*&p,int x,int pos)
{
if(p==null)
p=new Trie();
p->siz++;
if(!pos)
return ;
Insert(p->son[x&pos? 1:0],x,pos>>1);
}
int Get_Kth(Trie*p,int x,int pos,int k)
{
if(!pos)
return 0;
if(k<=p->son[x&pos?1:0]->siz)
return Get_Kth(p->son[x&pos?1:0],x,pos>>1,k);
else
return Get_Kth(p->son[x&pos?0:1],x,pos>>1,k-p->son[x&pos? 1:0]->siz)+pos;
}
int main()
{
int i,k;
cin>>n>>k;
for(i=1;i<=n;i++)
scanf("%d",&a[i]),Insert(root,a[i],1<<30);
for(i=1;i<=n;i++)
Push( make_pair( Get_Kth(root,a[i],1<<30,2) , make_pair(i,2) ) );
for(i=1;i<=k<<1;i++)
{
abcd temp=heap[1];Pop();
if(i&1)
printf("%d ",temp.first);
if(temp.second.second!=n)
{
int x=temp.second.first;
int y=temp.second.second;
Push( make_pair( Get_Kth(root,a[x],1<<30,y+1) , make_pair(x,y+1) ) );
}
}
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

BZOJ 3689 异或 Trie木+堆的更多相关文章

  1. bzoj 3689: 异或之 Trie+堆

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

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

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

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

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

  4. BZOJ 3689: 异或之

    字典树可以$o(logn)查找第k大$ 使用$可持久化Trie 区间查找第k大,然后首先把每个数异或之后的最小丢进小根堆中,然后一个一个取出,取出后就再丢次小,一共取k次$ 总的时间复杂度为$O(kl ...

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

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

  6. 【CodeForces】947 C. Perfect Security 异或Trie

    [题目]C. Perfect Security [题意]给定长度为n的非负整数数组A和数组B,要求将数组B重排列使得A[i]^B[i]的字典序最小.n<=3*10^5,time=3.5s. [算 ...

  7. CF 979D Kuro and GCD and XOR and SUM(异或 Trie)

    CF 979D Kuro and GCD and XOR and SUM(异或 Trie) 给出q(<=1e5)个操作.操作分两种,一种是插入一个数u(<=1e5),另一种是给出三个数x, ...

  8. bzoj 5495: [2019省队联测]异或粽子【可持久化trie+大根堆】

    和bzoj4504差不多,就是换了个数据结构 像超级钢琴一样把五元组放进大根堆,每次取一个出来拆开,(d,l,r,p,v)表示右端点为d,左端点区间为(l,r),最大区间和值为v左端点在p上 关于怎么 ...

  9. 异或之(bzoj 3689)

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

随机推荐

  1. CC2530存储空间——Code

    硬件平台:CC2530-F256 开发环境:IAR 8051(版本号 8.10) 參考: .<CC2530 User's Guide.pdf>(swru191c) .<IAR C/C ...

  2. javascript实现函数的默认參数值方法

    近期在学python,得益于python中的decorator思路,想到在javascript中參数是不能定义默认值的,可是能够通过decorator给它模拟出来,话不多说,上代码 <!DO ...

  3. SQL Server 2005,2008 正则表达式 替换函数应用详解

    CREATE function dbo.regexReplace ( @source ntext, --原字符串 ), --正则表达式 ), --替换值 , --是否是全局替换 --是否忽略大小写 ) ...

  4. 猪和python(pig and python)

    Python 真是无处不在国内. pig 0.9后python作为嵌入式语音,采用Jython解释器使用python2.5特征,此接口是最上层org.apache.pig.scripting.Pig首 ...

  5. POJ1458 Common Subsequence 【最长公共子序列】

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37614   Accepted: 15 ...

  6. UVA10375 Choose and divide 质因数分解

    质因数分解: Choose and divide Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %l ...

  7. TestNg它@Factory详细解释------如何更改参数值测试

    原创文章,版权所有所有.转载,归因:http://blog.csdn.net/wanghantong TestNg的@Factory注解从字面意思上来讲就是採用工厂的方法来创建測试数据并配合完毕測试 ...

  8. 如何成功实施SDL提供的官方Android平台Demo

    如何成功实施SDL提供的官方Android平台Demo 作者:雨水  日期:2014-4-30 编写说明:SDL的官方提供了一个Anroid的demo模板SDLActivity,无法直接执行,依照官方 ...

  9. SQL操作语句中的注意点

    一 查询语句 1 distinctkeyword消除反复行 当查询的结果数据中出现反复数据时.在查询条件中加上distinctkeyword消除反复行: 如:select distinct Sno f ...

  10. Spring AOP在pointcut expression解析表达式 并匹配多个条件

    Pointcut 方法是那些需要运行"AOP",由"Pointcut Expression"为了描述叙事. Pointcut以下方法可以通过定义任&&a ...