【树上带修莫队+分块】Haruna’s Breakfast

传送门:BZOJ4129


思路:

  • 树上带修莫队,用一个数据结构维护权值;
  • 由于每次移动都要对权值进行修改,权值线段树$O(nlogn)$会GG;
  • 而统计答案只有n次;
  • 所以用分块,这样修改$O(n^\frac{5}{3})$,查询$O(n^\frac{3}{2})$可以过;

注意:

  • 不用离散化,离散化会漏掉从未出现过的点
  • 如果某个值大于n那必定没用,直接不统计

代码如下:

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
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 300000
#define BASE 1200
#define block(x) ((x)/BASE+1)
int n,m,x,y,opt,ttmp,tot,clk,cnt;
int fa[N],deep[N],size[N],pos[N],head[N],l[N],r[N],top[N],f[N];
int a[N],b[N],last[N],xx[N],yy[N],op[N],ans[N],t[N];
bool vis[N];
struct edge{int v,nxt;}e[N];
struct modify{int x,y,z;}mdy[N];
struct query{int i,j,k,lca,ID;}qry[N];
bool cmp(query x,query y){
if (block(x.i)!=block(y.i)) return block(x.i)<block(y.i);
if (block(x.j)!=block(y.j)) return block(x.j)<block(y.j);
return x.k<y.k;
}
void add(int x,int y){e[++tot].v=y; e[tot].nxt=head[x]; head[x]=tot;}
void dfs1(int u){
if (!fa[u]) fa[u]=u; deep[u]=deep[fa[u]]+1; size[u]=1;
l[u]=++ttmp; pos[ttmp]=u;
for (int i=head[u],v;i;i=e[i].nxt)
if ((v=e[i].v)!=fa[u]){fa[v]=u; dfs1(v); size[u]+=size[v];}
r[u]=++ttmp; pos[ttmp]=u;
}
void dfs2(int u){
if (!top[u]) top[u]=u; int t=0;
for (int i=head[u],v;i;i=e[i].nxt) if ((v=e[i].v)!=fa[u] && size[v]>size[t]) t=v;
if (!t) return; top[t]=top[u]; dfs2(t);
for (int i=head[u];i;i=e[i].nxt) if (e[i].v!=fa[u] && e[i].v!=t) dfs2(e[i].v);
}
int lca(int u,int v){
for (;top[u]!=top[v];u=fa[top[u]]) if (deep[top[u]]<deep[top[v]]) swap(u,v);
return deep[u]<deep[v]?u:v;
}
void inc(int x){
vis[x]^=1; if (a[x]>n) return;
if (vis[x]){if (!f[a[x]]) b[block(a[x])]++; f[a[x]]++;}
else{f[a[x]]--; if (!f[a[x]]) b[block(a[x])]--;}
}
void change(int x,int y){
if (vis[x]){inc(x); a[x]=y; inc(x);} else a[x]=y;
return;
}
int solve(){
for (int i=1;;i++)
if (b[i]!=BASE)
for (int j=(i-1)*BASE;j<i*BASE;j++) if (!f[j]) return j;
return -1;
}
int main(){
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++){scanf("%d",&a[i]); last[i]=a[i];}
for (int i=1;i<n;i++){scanf("%d%d",&x,&y); add(x,y); add(y,x);}
for (int i=1;i<=m;i++)scanf("%d%d%d",&op[i],&xx[i],&yy[i]);
dfs1(1); dfs2(1);
for (int i=1;i<=m;i++){
opt=op[i]; x=xx[i]; y=yy[i];
if (opt==0){mdy[++clk]=(modify){x,last[x],y}; last[x]=y;}//last[x]不是last[i]
if (opt==1){
if (l[x]>l[y]) swap(x,y);
int LCA=lca(x,y);
if (LCA==x) qry[++cnt]=(query){l[x],l[y],clk,0,cnt};
else qry[++cnt]=(query){r[x],l[y],clk,LCA,cnt};
}
}
sort(qry+1,qry+cnt+1,cmp);
for (int i=1,j=0,k=0,xb=1;xb<=cnt;xb++){
while (k<qry[xb].k){k++; change(mdy[k].x,mdy[k].z);}
while (k>qry[xb].k){change(mdy[k].x,mdy[k].y); k--;}
while (i>qry[xb].i) inc(pos[--i]);
while (j<qry[xb].j) inc(pos[++j]);
while (i<qry[xb].i) inc(pos[i++]);
while (j>qry[xb].j) inc(pos[j--]);
if (qry[xb].lca) inc(qry[xb].lca);
ans[qry[xb].ID]=solve();
if (qry[xb].lca) inc(qry[xb].lca);
}
for (int i=1;i<=cnt;i++) printf("%d\n",ans[i]);
return 0;
}