二次联通门 : LibreOJ #517. 「LibreOJ β Round #2」计算几何瞎暴力
/*
LibreOJ #517. 「LibreOJ β Round #2」计算几何瞎暴力 叫做计算几何
实则毒瘤数据结构 看到xor后
考虑Trie树
Trie树的每一个节点保存的是以当前子树中每个二进制位的个数 给Trie打一个全局xor标记,如果标记这一位是1,就交换它的两个儿子 另外维护一个前缀和
前缀和存的是没sort过的值的和
Trie维护的是sort之后的值 1操作直接在前缀和后加就好
2操作在前缀和和Trie树中一起查 */
#include <cstdio>
#include <iostream>
#define rg register
const int BUF = ; char Buf[BUF], *buf = Buf; typedef long long LL;
inline void read (int &n)
{
bool temp = false;
for (n = ; !isdigit (*buf); ++ buf) if (*buf == '-') temp = true;
for (; isdigit (*buf); n = n * + *buf - '', ++ buf);
if (temp) n = -n;
}
#define Max 200009
struct T_D { T_D *c[]; int s, thb[]; }; int s[Max][], v[Max];
int _T, _S, C, T;
class Trie
{
private : T_D poor[Max * ], *Tail, *Root, *null;
private :
inline T_D *New ()
{ T_D *now = Tail ++; now->c[] = now->c[] = null, now->s = ; return now; }
public :
Trie ()
{
Tail = poor, null = Tail ++, null->c[] = null->c[] = null;
null->s = , Root = New ();
}
void Insert (const int &k)
{
T_D *n = Root; ++ T;
for (rg int i = , j; i >= ; -- i)
{
if (n->c[(k >> i) & ] == null) n->c[(k >> i) & ] = New ();
n = n->c[(k >> i) & ], ++ n->s;
for (j = ; j <= ; ++ j) n->thb[j] += (k >> j) & ;
}
}
inline void P (const int &key)
{
v[++ C] = key;
for (rg int i = ; i <= ; ++ i) s[C][i] = s[C - ][i] + ((key >> i) & );
}
inline void ReBuild () { for (; C; Insert (v[C --])); _S = _T; }
LL Find (int k)
{
T_D *n = Root; LL r = ; rg int i, j;
for (i = ; i >= ; -- i)
{
if (k == ) break;
if (k < n->c[(_S >> i) & ]->s) n = n->c[(_S >> i) & ];
else
{
T_D *p = n->c[(_S >> i) & ]; k -= p->s;
for (j = ; j <= ; ++ j)
if ((_T >> j) & ) r += (LL) (p->s - p->thb[j]) << j;
else r += (LL) p->thb[j] << j;
n = n->c[(_S >> i) & ^ ];
}
}
for (i = ; i <= ; ++ i)
{
if (((_T >> i) & ) && n->thb[i] == ) r += (LL) k << i;
if (((_T >> i) & ) == && n->thb[i]) r += (LL) k << i;
}
return r;
}
LL Q (int k)
{
if (k <= T) return Find (k); LL r = Find (T); k -= T;
for (rg int i = ; i <= ; ++ i)
if ((_T >> i) & ) r += (LL) (k - s[k][i]) << i;
else r += (LL) s[k][i] << i;
return r;
}
void Cg (const int key) { _T ^= key; return ; }
} D;
int main (int argc, char *argv[])
{
fread (buf, , BUF, stdin); int N, M, x, t, y; read (N); rg int i;
for (i = ; i <= N; ++ i) read (x), D.P (x);
for (read (M); M; -- M)
{
read (t);
if (t == ) read (x), D.P (x ^ _T);
else if (t == )
read (x), read (y), printf ("%I64d\n", D.Q(y) - D.Q(x - ));
else if (t == ) read (x), D.Cg (x);
else D.ReBuild ();
}
return ;
}