题目
解题思路
对整棵树进行轻重链剖分,将一棵树剖分成若干链。
按照树的剖分,我们可以将礼物划分到至多
l
o
g
2
n
log_2n
log2n个链上。
对于一条链而言,我们使用平衡树来维护礼物,从而做到插入删除和查询前k大异或和。
这里平衡树使用fhq-treap,这是一种无旋平衡树,平衡策略与treap相同。通过分离和合并操作构建出treap旋转后相同的平衡树。
fhq-treap代码量小,常数小,又因其无旋,也支持可持久化。是一种强大的平衡树。
整体复杂度
O
(
n
l
o
g
2
n
l
o
g
2
n
)
O(nlog_2nlog_2n)
O(nlog2nlog2n)。
代码
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <ctime>
#include <random>
using namespace std;
#define MP make_pair
typedef long long ll;
void read(int &x) {
x = 0; char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
}
void write(int x) {
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
const int N = 1e5 + 100;
struct node {
ll cnt; int id;
node() {}
node(ll cnt, int id) :cnt(cnt), id(id) {}
};
bool operator <= (node a, node b) {
if (a.cnt == b.cnt) return a.id <= b.id;
return a.cnt < b.cnt;
}
mt19937 ran(time(0));
namespace fhqtreap {
int ch[N * 2][2], pri[N * 2], siz[N * 2], sum[N * 2], sz;
node val[N * 2];
void update(int x) {
siz[x] = 1 + siz[ch[x][0]] + siz[ch[x][1]];
sum[x] = val[x].id ^ sum[ch[x][0]] ^ sum[ch[x][1]];
}
int newnode(node x) {
siz[++sz] = 1; val[sz] = x; sum[sz] = x.id; pri[sz] = ran();
return sz;
}
int merge(int x, int y) {
if (!x || !y) return x + y;
if (pri[x] < pri[y]) {
ch[x][1] = merge(ch[x][1], y);
update(x); return x;
}
else {
ch[y][0] = merge(x, ch[y][0]);
update(y); return y;
}
}
void split(int now, node k, int &x, int &y) {
if (!now) x = y = 0;
else {
if (val[now] <= k) x = now, split(ch[now][1], k, ch[now][1], y);
else y = now, split(ch[now][0], k, x, ch[now][0]);
update(now);
}
}
int query(int now, int k) {
int res = 0;
while (1) {
if (k <= siz[ch[now][0]]) now = ch[now][0];
else if (k == siz[ch[now][0]] + 1) return res ^ val[now].id ^ sum[ch[now][0]];
else k -= siz[ch[now][0]] + 1, res ^= val[now].id ^ sum[ch[now][0]], now = ch[now][1];
}
}
void clr() {
while (sz) ch[sz][0] = ch[sz][1] = 0, sz--;
}
}
using fhqtreap::newnode;
using fhqtreap::merge;
using fhqtreap::split;
using fhqtreap::clr;
int siz[N], faz[N], dep[N], son[N], top[N];
vector<int> V[N];
void dfs1(int u, int fa) {
siz[u] = 1;
faz[u] = fa;
dep[u] = dep[fa] + 1;
for (int v : V[u]) {
if (v == fa) continue;
dfs1(v, u);
siz[u] += siz[v];
if (siz[son[u]] < siz[v]) son[u] = v;
}
}
void dfs2(int u, int t) {
top[u] = t;
if (son[u]) dfs2(son[u], t);
for (int v : V[u])
if (v != faz[u] && v != son[u])
dfs2(v, v);
}
vector<node> Inc[N], Dec[N];
void insert(int x, int y, node qu) {
while (top[x] != top[y]) {
if (dep[top[x]] > dep[top[y]]) swap(x, y);
Inc[y].push_back(qu);
Dec[top[y]].push_back(node(qu.cnt, qu.id));
y = faz[top[y]];
}
if (dep[x] > dep[y]) swap(x, y);
Inc[y].push_back(qu); Dec[x].push_back(qu);
}
int n, q;
int ans[N];
ll cnt[N];
vector<pair<int, int> > Q[N];
int solve(int u) {
int r = 0;
if (son[u]) r = solve(son[u]);
for (node q : Inc[u]) {
int x = 0, y = 0, z = 0;
if (cnt[q.id] == 0) y = newnode(q);
else {
split(r, node(cnt[q.id], q.id - 1), x, y);
split(y, node(cnt[q.id], q.id), y, z);
r = merge(x, z);
fhqtreap::val[y].cnt += q.cnt;
}
cnt[q.id] += q.cnt;
split(r, node(cnt[q.id], q.id), x, z);
r = merge(merge(x, y), z);
}
for (pair<int, int> q : Q[u]) {
if (q.first >= fhqtreap::siz[r]) ans[q.second] = fhqtreap::sum[r];
else ans[q.second] = fhqtreap::query(r, q.first);
}
for (node q : Dec[u]) {
int x = 0, y = 0, z = 0;
split(r, node(cnt[q.id], q.id - 1), x, y);
split(y, node(cnt[q.id], q.id), y, z);
r = merge(x, z);
cnt[q.id] -= q.cnt;
if (cnt[q.id] != 0) {
fhqtreap::val[y].cnt -= q.cnt;
split(r, node(cnt[q.id], q.id), x, z);
r = merge(merge(x, y), z);
}
}
return r;
}
void dfs(int u) {
if (u == top[u]) solve(u), clr();
for (int v : V[u]) if (v != faz[u]) dfs(v);
}
int main() {
//freopen("0.txt", "r", stdin);
int a, b, c, d;
read(n);
for (int i = 1; i < n; i++) {
read(a); read(b);
V[a].push_back(b);
V[b].push_back(a);
}
dfs1(1, 0);
dfs2(1, 1);
read(q);
while (q--) {
read(a); read(b); read(c); read(d);
insert(a, b, node(c, d));
}
read(q);
for (int i = 1; i <= q; i++) {
read(a); read(b);
Q[a].push_back(MP(b, i));
}
a = 0;
dfs(1);
for (int i = 1; i <= q; i++) write(ans[i]), puts("");
return 0;
}