题解
一写过一交A的一道数据结构水题
我们发现大于C可以转化为这条路径上有多少个在某天之前开始调查的情报员,离线全部读入,变成树上路径查询某个区间的数出现过多少次,构建一棵根缀的主席树,查询的时候用两边的主席树减掉lca的主席树,然后判断一下lca是否合法
代码
#include <bits/stdc++.h>
//#define ivorysi
#define enter putchar('
')
#define space putchar(' ')
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eps 1e-8
#define mo 974711
#define MAXN 200005
#define pii pair<int,int>
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {putchar('-');x = -x;}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N,Q;
int P[MAXN],ans[MAXN],T[MAXN];
int X[MAXN],Y[MAXN],C[MAXN],tot,dep[MAXN];
int pos[MAXN],st[MAXN * 2][20],len[MAXN * 2],cnt;
struct node {
int to,next;
}E[MAXN * 2];
int head[MAXN],sumE;
struct Tr_node {
int lc,rc;
int siz;
}tr[MAXN * 20];
int rt[MAXN],Ncnt;
void add(int u,int v) {
E[++sumE].to = v;
E[sumE].next = head[u];
head[u] = sumE;
}
void Insert(const int &x,int &y,int L,int R,int p) {
y = ++Ncnt;
tr[y] = tr[x];
tr[y].siz++;
if(L == R) return;
int mid = (L + R) >> 1;
if(p <= mid) Insert(tr[x].lc,tr[y].lc,L,mid,p);
else Insert(tr[x].rc,tr[y].rc,mid + 1,R,p);
}
void dfs(int u,int fa) {
dep[u] = dep[fa] + 1;
Insert(rt[fa],rt[u],1,Q,T[u]);
st[++cnt][0] = u;
pos[u] = cnt;
for(int i = head[u] ; i ; i = E[i].next) {
int v = E[i].to;
if(v != fa) {
dfs(v,u);
st[++cnt][0] = u;
}
}
}
int min_dep(int a,int b) {
return dep[a] < dep[b] ? a : b;
}
int lca(int a,int b) {
a = pos[a],b = pos[b];
if(a > b) swap(a,b);
int l = len[b - a + 1];
return min_dep(st[a][l],st[b - (1 << l) + 1][l]);
}
int Query(int s,int f,int t,int C) {
if(C < 1) return 0;
int res = (T[f] <= C);
f = rt[f],s = rt[s],t = rt[t];
int L = 1,R = Q;
while(1) {
int mid = (L + R) >> 1;
if(R <= C) {
res += tr[s].siz - tr[f].siz + tr[t].siz - tr[f].siz;
break;
}
if(mid <= C) {
res += tr[tr[s].lc].siz - tr[tr[f].lc].siz + tr[tr[t].lc].siz - tr[tr[f].lc].siz;
L = mid + 1;
s = tr[s].rc;f = tr[f].rc;t = tr[t].rc;
}
else {
R = mid;
s = tr[s].lc;f = tr[f].lc;t = tr[t].lc;
}
if(C < L) break;
}
return res;
}
void Solve() {
read(N);
for(int i = 1 ; i <= N ; ++i) {
read(P[i]);
add(i,P[i]);add(P[i],i);
}
read(Q);
int op,p;
for(int i = 1 ; i <= Q ; ++i) {
read(op);
if(op == 1) {
++tot;
read(X[tot]);read(Y[tot]);read(C[tot]);
C[tot] = i - C[tot] - 1;
}
else {
read(p);
T[p] = i;
}
}
for(int i = 1 ; i <= N ; ++i) {
if(!T[i]) T[i] = Q;
}
dfs(1,0);
for(int i = 2 ; i <= cnt; ++i) len[i] = len[i / 2] + 1;
for(int j = 1 ; j <= 18 ; ++j) {
for(int i = 1 ; i <= cnt ; ++i) {
if(i + (1 << j) - 1 > cnt) break;
st[i][j] = min_dep(st[i][j - 1],st[i + (1 << (j - 1))][j - 1]);
}
}
for(int i = 1 ; i <= tot ; ++i) {
int f = lca(X[i],Y[i]);
out(dep[X[i]] + dep[Y[i]] - 2 * dep[f] + 1);space;
out(Query(X[i],f,Y[i],C[i]));enter;
}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}