[BZOJ4771] 七彩樹
阿新 • • 發佈:2019-01-22
Problem
n個節點的有根數,每個節點擁有一個顏色。邊權為1。
現在有m個查詢,每個問題有兩個整數x和d,表示詢問x的字數中不超過的所有點中出現來多少種本質不同的顏色。
Solution
每個點的貢獻都是,那麼若有顏色相同的點對,那麼的貢獻值就要。於是子樹和即為答案。
序相鄰的兩個節點的是最深的。所以我們用當前節點與其相鄰的兩個點做一下即可。如果與它相鄰的確實是兩個點,那麼這兩個節點的處需要貢獻(容斥)。
這個相鄰我們可以給每一個顏色都開一個 來維護。然後按照深度去處理。
再來考慮深度限制。
維護一個主席樹,表示深度為~的點的貢獻,主席樹內是以序為下標的。
Code
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;
#define N 100010
inline char gc() {
static char now[1<<16], *S, *T;
if(S == T) {T = (S = now) + fread(now, 1 , 1<<16, stdin); if(S == T) return EOF;}
return *S++;
}
inline int read() {
int x = 0; char c = gc();
while(c < '0' || c > '9') c = gc();
while(c >= '0' && c <= '9') {x = x * 10 + c - 48; c = gc();}
return x;
}
int c[N], f[N][17], dep[N], head[N], id[N];
int n, m, cnt;
struct edge {
int to, next;
}e[N];
inline void ins(int x, int y) {
e[++cnt].to = y;
e[cnt].next = head[x];
head[x] = cnt;
}
inline void init_lca() {
for(int i = 1; i <= 16; ++i)
for(int j = 1; j <= n; ++j) {
f[j][i] = f[f[j][i - 1]][i - 1];
}
}
inline int lca(int x, int y) {
if(dep[x] < dep[y]) swap(x, y);
for(int i = 16; i >= 0; --i) {
if(dep[f[x][i]] >= dep[y]) {
x = f[x][i];
}
}
if(x == y) return x;
for(int i = 16; i >= 0; --i) {
if(f[x][i] != f[y][i]) {
x = f[x][i];
y = f[y][i];
}
}
return f[x][0];
}
int st[N], ed[N], real[N], tim;
void dfs(int x, int d) {
dep[x] = d; st[x] = ++tim; real[tim] = x;
for(int i = head[x]; i; i = e[i].next) {
dfs(e[i].to, d + 1);
}
ed[x] = tim;
}
inline bool cmp(int A, int B) {
return dep[A] < dep[B];
}
set<int> s[N];
set<int>::iterator it;
int rt[N];
#undef N
#define M 5000010
int L[M], R[M], v[M], len;
#undef M
void insert(int x, int &y, int l, int r, int pos, int delta) {
y = ++len; L[y] = R[y] = 0;
v[y] = v[x] + delta;
if(l == r) return ;
int mid = (l + r)>>1;
if(pos <= mid) R[y] = R[x], insert(L[x], L[y], l, mid, pos, delta);
else L[y] = L[x], insert(R[x], R[y], mid + 1, r, pos, delta);
}
int query(int x, int l, int r, int a, int b) {
if(!x || (a <= l && r <= b)) return v[x];
int mid = (l + r)>>1;
int ret = 0;
if(a <= mid) ret+= query(L[x], l, mid, a, b);
if(mid + 1 <= b) ret+= query(R[x], mid + 1, r, a, b);
return ret;
}
inline void work() {
cnt = 1;
n = read(); m = read();
for(int i = 1; i <= n; ++i) {
c[i] = read();
s[i].clear();
memset(f[i], 0, sizeof(f[i]));
head[i] = 0;
rt[i] = 0;
id[i] = i;
}
for(int i = 2; i <= n; ++i) {
f[i][0] = read();
ins(f[i][0], i);
}
init_lca(); tim = 0; dfs(1, 1);
sort(id+1, id+n+1, cmp);
len = 0;
for(int i = 1; i <= n; ++i) {
int u = id[i], a = 0, b = 0;
it = s[c[u]].lower_bound(st[u]);
insert(rt[dep[id[i - 1]]], rt[dep[u]], 1, n, st[u], 1);
if(it != s[c[u]].end()) {
b = real[*it];
insert(rt[dep[u]], rt[dep[u]], 1, n, st[lca(b, u)], -1);
}
if(it != s[c[u]].begin()) {
a = real[*(--it)];
insert(rt[dep[u]], rt[dep[u]], 1, n, st[lca(a, u)], -1);
}
if(a && b) {
insert(rt[dep[u]], rt[dep[u]], 1, n, st[lca(a, b)], 1);
}
s[c[u]].insert(st[u]);
}
int lastans = 0;
for(int i = 1; i <= m; ++i) {
int x = read() ^ lastans, d = read() ^ lastans;
lastans = query(rt[min(dep[x] + d, dep[id[n]])], 1, n, st[x], ed[x]);
printf("%d\n", lastans);
}
}
int main() {
int T = read();
while(T--) work();
return 0;
}