[bzoj] 3757: 苹果树

题意

一棵树上每个节点都有颜色,每次回答树链上有几种颜色,有时询问视两种颜色为同一种颜色

传送门

分析

莫队算法

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <cstdio>
#define Rep(i,l,r) for(i=(l);i<=(r);i++)
#define Rev(i,r,l) for(i=(r);i>=(l);i--)
int CH , NEG ;
template <typename TP>
inline void read(TP& ret) {
ret = NEG = 0 ; while (CH=getchar() , CH<'!') ;
if (CH == '-') NEG = true , CH = getchar() ;
while (ret = ret*10+CH-'0' , CH=getchar() , CH>'!') ;
if (NEG) ret = -ret ;
}
template <typename TP>
inline void reads(TP& ret) {
truewhile (ret=getchar() , ret<'!') ;
truewhile (CH=getchar() , CH>'!') ;
}

#include <algorithm>
#include <cmath>

#define maxn 50010LL
#define maxm 50010LL
#define maxq 100010LL
#define maxk 17LL

struct FST { int to , next ; } e[maxm<<1] ;
int star[maxn] = {0} , tote = 1 ;
void AddEdge(int u , int v) { e[++tote].to = v ; e[tote].next = star[u] ; star[u] = tote ; }

int n , m , root , ansnow ;
int cntblc , szblc , idx , top ;

int col[maxn] , cnt[maxn] = {0} ;
int dfn[maxn] , sta[maxn] , belong[maxn] , dep[maxn] , size[maxn] , par[maxn][maxk] ;
int ans[maxq] ;
bool vis[maxn] = {0} ;

struct QUERY {
trueint u , v , a , b , id ;
truebool operator < (const QUERY& b) const
true{ return belong[u]==belong[b.u]? dfn[v]<dfn[b.v] : belong[u]<belong[b.u] ; }
} q[maxq] ;

void dfs(int u , int fa) {
int v , p ;
truesta[++top] = u ;
truepar[u][0] = fa ;
truedfn[u] = ++idx ;
truedep[u] = dep[fa]+1 ;
truesize[u] = 0 ;
truefor (p = star[u] ; p ; p = e[p].next)
truetrueif (v=e[p].to , v!=fa) {
truetruetruedfs(v , u) ;
truetruetruesize[u] += size[v] ;
truetruetrueif (size[u] >= szblc) {
truetruetruetruesize[u] = 0 ;
truetruetruetruecntblc ++ ;
truetruetruetruewhile (sta[top] != u) belong[sta[top--]] = cntblc ;
truetruetrue}
truetrue}
truesize[u] ++ ;
}

void ST() {
int i , k ;
trueRep (k,1,maxk-1)
truetrueRep (i,1,n)
truetruetruepar[i][k] = par[par[i][k-1]][k-1] ;
}

int LCA(int u , int v) {
trueint k ;
trueif (dep[u] < dep[v]) std::swap(u,v) ;
trueRev (k,maxk-1,0)
truetrueif (dep[par[u][k]] >= dep[v]) u = par[u][k] ;
trueif (u == v) return u ;
trueRev (k,maxk-1,0)
truetrueif (par[u][k] != par[v][k])
truetruetrueu = par[u][k] , v = par[v][k] ;
truereturn par[u][0] ;
}

void Reverse(int u) {
trueif (!vis[u]) { vis[u]=true;cnt[col[u]]++;if(cnt[col[u]]==1)ansnow++; }
trueelse { vis[u]=false;cnt[col[u]]--;if(cnt[col[u]]==0)ansnow--; }
}

void MoveTo(int u,int v) {
truewhile (u != v)
truetrueif (dep[u] > dep[v]) Reverse(u) , u = par[u][0] ;
truetrueelse Reverse(v) , v = par[v][0] ;
}

void GetAns(int i , int lca) {
trueReverse(lca) ;
trueans[q[i].id] = ansnow ;
trueif (q[i].a!=q[i].b && cnt[q[i].a] && cnt[q[i].b]) ans[q[i].id] -- ;
trueReverse(lca) ;
}

int main() {
int i , u , v , lca ;
// #define READ
true#ifdef READ
truetruefreopen(".in" ,"r",stdin ) ;
truetruefreopen(".out","w",stdout) ;
true#endif
trueread(n) , read(m) ;
trueif (n < 5) szblc = n ;
trueelse szblc = (int)pow(n,2.0/3.0) ;
trueRep (i,1,n) read(col[i]) ;
trueRep (i,1,n) {
truetrueread(u) , read(v) ;
truetrueif (!u) root = v ;
truetrueelse if (!v) root = u ;
truetrueelse AddEdge(u,v) , AddEdge(v,u) ;
true}
truecntblc = idx = dep[0] = ansnow = top = 0 ;
truedfs(root,0) ;
truecntblc ++ ;
truewhile (top) belong[sta[top--]] = cntblc ;
trueST() ;
trueRep (i,1,m) {
truetrueread(u) , read(v) ;
truetrueif (belong[u] > belong[v]) std::swap(u,v) ;
truetrueq[i].u = u , q[i].v= v ;
truetrueread(q[i].a) , read(q[i].b) ;
truetrueq[i].id = i ;
true}
truestd::sort(q+1,q+m+1) ;
truelca = LCA(q[1].u,q[1].v) ;
trueMoveTo(q[1].u,q[1].v) ;
trueGetAns(1,lca) ;
trueRep (i,2,m) {
truetrueMoveTo(q[i-1].u,q[i].u) ;
truetrueMoveTo(q[i-1].v,q[i].v) ;
truetruelca = LCA(q[i].u,q[i].v) ;
truetrueGetAns(i,lca) ;
true}
trueRep (i,1,m) printf("%d\n", ans[i]) ;
true#ifdef READ
truetruefclose(stdin) ; fclose(stdout) ;
true#else
truetruegetchar() ; getchar() ;
true#endif
truereturn 0 ;
}

热评文章