# [BZOJ 4103] [Thu Summer Camp 2015] 异或运算 【可持久化Trie】

### 题目分析

THUSC滚粗之后一直没有写这道题，从来没写过可持久化Trie，发现其实和可持久化线段树都是一样的。嗯，有些东西就是明白得太晚。

### 代码

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

const int MaxN = 1000 + 5, MaxM = 300000 + 5, MaxNode = 10000000 + 5;

int n, m, p, Index, Ans;
int A[MaxN], B[MaxM], Root[MaxM], Son[MaxNode][2], T[MaxNode], F[MaxN], Q[MaxN];

bool OK[MaxN];

void Build(int &x, int y, int Num, int Bit)
{
if (!x) x = ++Index;
if (Bit == 0)
{
T[x] = T[y] + 1;
return;
}
if (Num & (1 << (Bit - 1)))
{
Son[x][0] = Son[y][0];
Build(Son[x][1], Son[y][1], Num, Bit - 1);
}
else
{
Son[x][1] = Son[y][1];
Build(Son[x][0], Son[y][0], Num, Bit - 1);
}
T[x] = T[Son[x][0]] + T[Son[x][1]];
}

int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) scanf("%d", &A[i]);
for (int i = 1; i <= m; ++i)
{
scanf("%d", &B[i]);
Build(Root[i], Root[i - 1], B[i], 31);
}
scanf("%d", &p);
int u, d, l, r, kk;
for (int i = 1; i <= p; ++i)
{
scanf("%d%d%d%d%d", &u, &d, &l, &r, &kk);
kk = (d - u + 1) * (r - l + 1) - kk + 1;
Ans = 0;
for (int j = u; j <= d; ++j)
{
F[j] = Root[r];
Q[j] = Root[l - 1];
}
for (int j = 30; j >= 0; --j)
{
int x, Temp = 0;
for (int k = u; k <= d; ++k)
{
x = (A[k] & (1 << j));
if (x) Temp += T[Son[F[k]][1]] - T[Son[Q[k]][1]];
else Temp += T[Son[F[k]][0]] - T[Son[Q[k]][0]];
}
if (Temp >= kk)
{
for (int k = u; k <= d; ++k)
{
x = (A[k] & (1 << j));
if (x)
{
F[k] = Son[F[k]][1];
Q[k] = Son[Q[k]][1];
}
else
{
F[k] = Son[F[k]][0];
Q[k] = Son[Q[k]][0];
}
}
}
else
{
kk -= Temp;
Ans |= (1 << j);
for (int k = u; k <= d; ++k)
{
x = (A[k] & (1 << j));
if (x)
{
F[k] = Son[F[k]][0];
Q[k] = Son[Q[k]][0];
}
else
{
F[k] = Son[F[k]][1];
Q[k] = Son[Q[k]][1];
}
}
}
}
printf("%d\n", Ans);
}
return 0;
}


posted @ 2015-07-11 11:29  JoeFan  阅读(634)  评论(0编辑  收藏  举报