题解1的程序:
```cpp
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
char buf[1 << 21], *p1 = buf, *p2 = buf;
using namespace std;
const int MAXN = 1e5 + 10;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, Q;
int belong[MAXN], block;
struct Query {
int l, r, ID, lca, ans;
bool operator < (const Query &rhs) const{
return belong[l] == belong[rhs.l] ? r < rhs.r : belong[l] < belong[rhs.l];
// return belong[l] < belong[rhs.l];
}
}q[MAXN];
vector<int>v[MAXN];
int a[MAXN], date[MAXN];
void Discretization() {
sort(date + 1, date + N + 1);
int num = unique(date + 1, date + N + 1) - date - 1;
for(int i = 1; i <= N; i++) a[i] = lower_bound(date + 1, date + num + 1, a[i]) - date;
}
int deep[MAXN], top[MAXN], fa[MAXN], siz[MAXN], son[MAXN], st[MAXN], ed[MAXN], pot[MAXN], tot;
void dfs1(int x, int _fa) {
fa[x] = _fa; siz[x] = 1;
st[x] = ++ tot; pot[tot] = x;
for(int i = 0; i < v[x].size(); i++) {
int to = v[x][i];
if(deep[to]) continue;
deep[to] = deep[x] + 1;
dfs1(to, x);
siz[x] += siz[to];
if(siz[to] > siz[son[x]]) son[x] = to;
}
ed[x] = ++tot; pot[tot] = x;
}
void dfs2(int x, int topfa) {
top[x] = topfa;
if(!son[x]) return ;
dfs2(son[x], topfa);
for(int i = 0; i < v[x].size(); i++) {
int to = v[x][i];
if(top[to]) continue;
dfs2(to, to);
}
}
int GetLca(int x, int y) {
while(top[x] != top[y]) {
if(deep[top[x]] < deep[top[y]]) swap(x, y);
x = fa[top[x]];
}
return deep[x] < deep[y] ? x : y;
}
void DealAsk() {
for(int i = 1; i <= Q; i++) {
int x = read(), y = read();
if(st[x] > st[y]) swap(x, y);
int _lca = GetLca(x, y);
q[i].ID = i;
if(_lca == x) q[i].l = st[x], q[i]. r = st[y];
else q[i].l = ed[x], q[i].r = st[y], q[i].lca = _lca;
}
}
int Ans, out[MAXN], used[MAXN], happen[MAXN];
void add(int x) {
if(++happen[x] == 1) Ans++;
}
void delet(int x) {
if(--happen[x] == 0) Ans--;
}
void Add(int x) {
used[x] ? delet(a[x]) : add(a[x]); used[x] ^= 1;
}
void Mo() {
sort(q + 1, q + Q + 1);
int l = 1, r = 0, fuck = 0;
for(int i = 1; i <= Q; i++) {
while(l < q[i].l) Add(pot[l]), l++, fuck++;
while(l > q[i].l) l--, Add(pot[l]), fuck++;
while(r < q[i].r) r++, Add(pot[r]), fuck++;
while(r > q[i].r) Add(pot[r]), r--, fuck++;
if(q[i].lca) Add(q[i].lca);
q[i].ans = Ans;
if(q[i].lca) Add(q[i].lca);
}
for(int i = 1; i <= Q; i++) out[q[i].ID] = q[i].ans;
for(int i = 1; i <= Q; i++)
printf("%d\n", out[i]);
}
int main() {
freopen("COT2.in","r",stdin);
freopen("COT2_std.out","w",stdout);
N = read(); Q = read();
//block = 1.5 * sqrt(2 * N) + 1;
//block = pow(N, 0.66666666666);
block = sqrt(N);
for(int i = 1; i <= N; i++) a[i] = date[i] = read();
for(int i = 1; i <= N * 2; i++) belong[i] = i / block + 1;
Discretization();
for(int i = 1; i <= N - 1; i++) {
int x = read(), y = read();
v[x].push_back(y); v[y].push_back(x);
}
deep[1] = 1; dfs1(1, 0);
dfs2(1, 1);
/* for(int i = 1; i <= N; i++)
for(int j = 1; j <= i - 1; j++)
printf("%d %d %d\n", i, j, GetLca(i, j));*/
DealAsk();
Mo();
return 0;
}
```
by Owen_codeisking @ 2018-10-02 21:32:35
造数据的程序:
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn=40000;
const int maxm=100000;
int main()
{
srand(time(0));
freopen("COT2.in","w",stdout);
int n=maxn,m=maxm;
printf("%d %d\n",n,m);
for(int i=1;i<=n;i++)
printf("%d%c",rand()*rand()%maxn+1,(i==n)?'\n':' ');
for(int i=2;i<=n;i++){
int fa=rand()*rand()%(i-1)+1;
printf("%d %d\n",fa,i);
}
for(int i=1;i<=m;i++){
int u=rand()*rand()%n+1,v=rand()*rand()%n+1;
printf("%d %d\n",u,v);
}
return 0;
}
```
对拍的程序:
```cpp
#include <bits/stdc++.h>
using namespace std;
int main()
{
for(int i=1;;i++){
printf("Test case:%d\n",i);
system("data.exe");
clock_t t=clock();
system("czx.exe");
t=clock()-t;
system("std.exe");
if(system("fc COT2.out COT2_std.out")){
printf("WA\n");
break;
}
else printf("AC %dms\n",t);
}
return 0;
}
```
为什么我对拍了两个小时都是$AC$,交上去的时候$WA$?
by Owen_codeisking @ 2018-10-02 21:32:46
@[我是一个菜鸡](/space/show?uid=20309)
by Owen_codeisking @ 2018-10-02 21:34:35
我又不会树上莫队
by Fading @ 2018-10-02 21:52:02
@[Owen_codeisking](/space/show?uid=35069) 那么大的maxn来产生数字,能跑出错就怪了。。大概率答案等于两点距离
by pdalwj @ 2019-01-17 22:17:13
其实后来我换成树剖就过了 @[lapfsjg](/space/show?uid=165624)
by Owen_codeisking @ 2019-01-18 07:45:27
感谢楼主的对拍程序,我也过了 QWQ
by warzone @ 2020-11-10 21:56:28
lz orz对拍程序
by syk666 @ 2022-08-31 20:58:29