题解 松鼠的新家

题目链接 小熊维尼是个好吃懒作的家伙。

给定一棵树,和一个序列。要求维尼按序列的顺序走,每当维尼走过一个节点,节点点权加一。

事实上就是对一段链进行加法。可以使用树链剖分。

但是我不会

所以我们用树上差分的方法。对于一段$(u,v)$的路程,所有的点权加一的差分数组就是

$$ c[u]++,c[v]++,c[lca]-=2,ans[lca++] $$

其中c数组是差分数组,ans数组是答案数组。因为树上差分最后每个点的答案就是当前的点的ans数组加上子树的差分值。因为lca的差分数组减了2次,所以给ans[lca]++。

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
namespace Sonoda
{
template<typename T> void swap(T &a,T &b)
{
T t;
t=a;
a=b;
b=t;
}
template<typename T> T GCD(T a,T b)
{
if(b==0)
{
return a;
}
return GCD(b,a%b);
}
template<typename T>T Qpow(T a,T b,T p)
{
T res=1;
while(b)
{
if(b&1)
{
res*=a;
res%=p;
b--;
}
else
{
a*=a;
a%=p;
b>>=1;
}
}
return res;
}
template <typename T> void Ex_GCD(T a,T b,T &x,T &y)
{
if(b==0)
{
x=1;
y=0;
return;
}
Ex_GCD(b,a%b,x,y);
T t=x;
x=y;
y=t-a/b*y;
}
template<typename T> inline T read()
{
T num = 0, w = 1;
char c = 0;
while (c != '-' && !isdigit(c)) c = getchar();
if (c == '-') w = -1, c = getchar();
while (isdigit(c)) num = num * 10 + c - '0', c = getchar();
return num * w;
}
template<typename T> inline void write(T x)
{
if (x < 0) putchar('-'), x = -x;
if (x / 10) write(x / 10);
putchar(x % 10 + '0');
}
}
const int MAXN=600005;
int v[MAXN];
struct EDGE
{
int _next,to;
};
EDGE edge[MAXN];
int head[MAXN],_count;
int depth[MAXN];
int father[MAXN][31];

void _Init(int NowNode,int fa)
{
depth[NowNode]=depth[fa]+1;
father[NowNode][0]=fa;
for(int i=1;(1<<i<=depth[NowNode]);i++)
{
father[NowNode][i]=father[father[NowNode][i-1]][i-1];
}
for(int i=head[NowNode];i;i=edge[i]._next)
{
int NextNode=edge[i].to;
if(NextNode==fa)
{
continue;
}
_Init(NextNode,NowNode);
}
}
void Add_Line(const int &a,const int &b)
{
_count++;
edge[_count].to=b;
edge[_count]._next=head[a];
head[a]=_count;
}
int LCA(int a,int b)
{
if(depth[a]<depth[b])
{
Sonoda::swap(a,b);
}
for(int i=30;i>=0;i--)
{
if(depth[a]-depth[b]>=(1<<i))
{
a=father[a][i];
}
}
if(a==b)
{
return a;
}
for(int i=30;i>=0;i--)
{
if(father[a][i]==father[b][i])
{
continue;
}
a=father[a][i];
b=father[b][i];
}
return father[a][0];
}
int c[MAXN];
int ans[MAXN];
void FUCK(int NowNode,int fa)
{
for(int i=head[NowNode];i;i=edge[i]._next)
{
int NextNode=edge[i].to;
if(NextNode==fa)
{
continue;
}
FUCK(NextNode,NowNode);
c[NowNode]+=c[NextNode];
}
ans[NowNode]+=c[NowNode];
}
int main()
{
int N=Sonoda::read<int>();
for(int i=1;i<=N;i++)
{
v[i]=Sonoda::read<int>();
}
for(int i=1;i<N;i++)
{
int a=Sonoda::read<int>();
int b=Sonoda::read<int>();
Add_Line(a,b);
Add_Line(b,a);
}
_Init(1,0);
for(int i=1;i<N;i++)
{
int a=v[i];
int b=v[i+1];
int lca=LCA(a,b);
c[a]++;
c[b]++;
c[lca]-=2;
ans[lca]++;
}
FUCK(1,0);
for(int i=2;i<=N;i++)
{
ans[v[i]]--;
}

for(int i=1;i<=N;i++)
{
printf("%d\n",ans[i]);
}
return 0;
}

对于这道题,上一次走的房间这次就不用走了,所以最后从序列第二个到最后一个的ans–。