您的位置:首页 > 其它

BZOJ 3689: 异或之

2017-11-07 19:52 363 查看
二次联通门 : BZOJ 3689: 异或之









/*
BZOJ 3689: 异或之

zz题
*/
#include <cstdio>
#include <iostream>
#include <queue>
#define rg register
struct IO
{
static const int BUF = 12323233;
char p[BUF], *s, *t, e[BUF]; int a[25];
IO () : s (p), t (e) { fread (s, 1, BUF, stdin); }
~IO () { fwrite (e, 1, t - e, stdout); }
operator int ()
{
rg int j = 1, v = 0;
for (; *s < 48; j = *s ++ ^ 45);
do v = v * 10 + *s ++ - 48; while (*s >= 48);
return j ? v : -v;
}

void pt (int v)
{
static int *q = a;
if (!v) *t ++ = 48;
else
{
if (v < 0) *t ++ = 45, v *= -1;

for (; v; *q ++ = v % 10 + 48, v /= 10);
for (; q != a; *t ++ = *-- q);
}
*t ++ = ' ';
}
} ip;

#define Max 200203
int tr[Max * 20][2], TC, s[Max * 20];

void In (int key)
{
int n = 0;
for (rg int t, i = 30; i >= 0; -- i)
{
t = (key >> i) & 1;
if (!tr
[t]) tr
[t] = ++ TC;
n = tr
[t], ++ s
;
}
}

int Ask (int key, int k)
{
int res = 0, n = 0;
for (rg int i = 30, t; i >= 0; -- i)
{
t = (key >> i) & 1;
if (s[tr
[t]] >= k) n = tr
[t];
else k -= s[tr
[t]], n = tr
[t ^ 1], res += (1 << i);
}
return res;
}
struct D
{
int key, rk, x;

D (int a = 0, int b = 0, int c = 0) : key (a), rk (b), x (c) {}

bool operator < (const D &rhs) const
{ return x > rhs.x; }
};

int a[Max];
std :: priority_queue <D> Q;

int main (int argc, char *argv[])
{
int N = ip, K = ip; rg int i; D n;

for (i = 1; i <= N; ++ i) a[i] = ip, In (a[i]);

for (i = 1; i <= N; ++ i) Q.push (D (a[i], 2, Ask (a[i], 2)));

for (i = 1; i < (K << 1); ++ i)
{
n = Q.top (), Q.pop ();
if (i & 1) ip.pt (n.x);
++ n.rk, n.x = Ask (n.key, n.rk), Q.push (n);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: