QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#403241#2857. 树点涂色rzh123100 ✓296ms39296kbC++234.2kb2024-05-01 22:57:382024-05-01 22:57:39

Judging History

你现在查看的是最新测评结果

  • [2024-05-01 22:57:39]
  • 评测
  • 测评结果:100
  • 用时:296ms
  • 内存:39296kb
  • [2024-05-01 22:57:38]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
constexpr int N=1e5+5;
int n,m,ec,eh[N];
int sz[N],dep[N],fa[N],hs[N],tp[N],dfn[N],dfnr[N],dfp[N],dft;
int down[N+N],up[N+N];
bool isup[N];
struct{int nxt,to;}e[N<<1];
inline void adde(int u,int v){
	e[++ec]={eh[u],v},eh[u]=ec;
}
enum OpTy{COV,ADD};
struct LCA{
	int f[20][N];
	static inline bool cmp(int x,int y){return (dep[x]!=dep[y])?(dep[x]<dep[y]):(dfn[x]>dfn[y]);}
	inline void init(int n){for(int i{1};i<=n;++i)f[0][i]=dfp[i];
	for(int i{1};i<=__lg(n);++i)for(int j{1};j<=n-(1<<i)+1;++j)f[i][j]=min(f[i-1][j],f[i-1][j+(1<<(i-1))],cmp);}
	inline int query(int l,int r)const{int t{__lg(r-l+1)};return min(f[t][l],f[t][r-(1<<t)+1],cmp);}
	inline int operator()(int u,int v){if(u==v)return u;if(dfn[u]>dfn[v])swap(u,v);return fa[query(dfn[u]+1,dfn[v])];}
	inline int lstson(int u,int v){return query(dfn[u]+1,dfn[v]);}
}lca;
struct Seg{
	int id;
	Seg(int i):id{i}{}
	struct{
		int l,r,mx,lz1,lz2;
	}tr[N<<2];
	inline void apply1(int k,int v){
		tr[k].lz1=v,tr[k].lz2=0,
		tr[k].mx=v;
	}
	inline void apply2(int k,int v){
		tr[k].lz2+=v,tr[k].mx+=v;
	}
	inline void pdn(int k){
		if(tr[k].lz1){
			apply1(k<<1,tr[k].lz1),apply1(k<<1|1,tr[k].lz1),
			tr[k].lz1=0;
		}
		if(tr[k].lz2){
			apply2(k<<1,tr[k].lz2),apply2(k<<1|1,tr[k].lz2),
			tr[k].lz2=0;
		}
	}
	inline void pup(int k){
		tr[k].mx=max(tr[k<<1].mx,tr[k<<1|1].mx);
	}
	void build(int k,int l,int r){
		tr[k].l=l,tr[k].r=r,tr[k].lz1=tr[k].lz2=0;
		if(l==r){
			if(id==1) tr[k].mx=dfp[l];
			else tr[k].mx=dep[dfp[l]];
			return;
		}
		int mi((l+r)>>1);
		build(k<<1,l,mi),build(k<<1|1,mi+1,r);
		pup(k);
	}
	void modify(int k,int l,int r,int v,OpTy t){
		if(tr[k].l>=l&&tr[k].r<=r) return t==ADD?apply2(k,v):apply1(k,v);
		pdn(k); int mi((tr[k].l+tr[k].r)>>1);
		if(l<=mi) modify(k<<1,l,r,v,t);
		if(r>mi) modify(k<<1|1,l,r,v,t);
		pup(k);
	}
	int query(int k,int l,int r){
		if(tr[k].l>=l&&tr[k].r<=r) return tr[k].mx;
		pdn(k); int mi((tr[k].l+tr[k].r)>>1);
		if(r<=mi) return query(k<<1,l,r);
		else if(l>mi) return query(k<<1|1,l,r);
		else return max(query(k<<1,l,r),query(k<<1|1,l,r));
	}
}seg1(1),seg2(2);

void dfs1(int u,int fa){
	dep[u]=dep[::fa[u]=fa]+(sz[u]=1);
	for(int i{eh[u]};i;i=e[i].nxt){
		int v{e[i].to};
		if(v==fa) continue;
		dfs1(v,u);
		sz[u]+=sz[v];
		if(sz[v]>sz[hs[u]]) hs[u]=v;
	}
}
void dfs2(int u,int fa,int t){
	tp[u]=t,
	dfp[dfn[u]=++dft]=u;
	if(hs[u]) dfs2(hs[u],u,t);
	for(int i{eh[u]};i;i=e[i].nxt){
		int v{e[i].to};
		if(v==fa||v==hs[u]) continue;
		dfs2(v,u,v);
	}
	dfnr[u]=dfn[u]+sz[u]-1;
}
template <typename DS>
void modify(int u,int v,int w,DS &seg){
	while(tp[u]!=tp[v]){
		if(dep[tp[u]]>dep[tp[v]]){
			seg.modify(1,dfn[tp[u]],dfn[u],w,COV);
			u=fa[tp[u]];
		}
		else{
			seg.modify(1,dfn[tp[v]],dfn[v],w,COV);
			v=fa[tp[v]];
		}
	}
	if(dep[u]<dep[v]) seg.modify(1,dfn[u],dfn[v],w,COV);
	else seg.modify(1,dfn[v],dfn[u],w,COV);
}
void setcol(int u,int v,int w){
	modify(u,v,w,seg1);
}
void addf(int l,int r,int w){
	seg2.modify(1,l,r,w,ADD);
}
void setf(int u,int v,int w){
	modify(u,v,w,seg2);
}
inline int getcol(int u){
	return seg1.query(1,dfn[u],dfn[u]);
}
inline int getf(int u){
	return seg2.query(1,dfn[u],dfn[u]);
}


void access(int x,int id){
	int x0{x};
	for(int lst{0};x;){
		int cx{getcol(x)},fx{getf(x)};
		int cson{0},top{up[cx]};
		isup[top]=false;
		if(down[cx]!=x) cson=lca.lstson(x,down[cx]),isup[up[cx]=cson]=true;

		addf(dfn[top],dfnr[top],1-fx);
		if(cson) addf(dfn[cson],dfnr[cson],1);
		if(lst) addf(dfn[lst],dfnr[lst],fx-1);
		lst=top;
		x=fa[top];
	}
	setcol(1,x0,id);
	down[id]=x0,up[id]=1,isup[1]=true;
}
int query2(int x,int y){
	int z{lca(x,y)};
	int ret{getf(x)+getf(y)-1};
	if(fa[z]){
		int ff{getf(fa[z])};
		if(!isup[z]) ret-=2*(ff-1);
		else ret-=2*ff;
	}
	return ret;
}
inline int query3(int x){
	return seg2.query(1,dfn[x],dfnr[x]);
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i{1};i<n;++i){
		int a,b; scanf("%d%d",&a,&b);
		adde(a,b),adde(b,a);
	}
	for(int i{1};i<=n;++i) up[i]=down[i]=i,isup[i]=true;
	dfs1(1,0),dfs2(1,0,1);
	lca.init(dft);
	seg1.build(1,1,n),seg2.build(1,1,n);
	for(int id{n+1};id<=n+m;++id){
		int t,x,y{0}; scanf("%d%d",&t,&x);
		if(t==1){
			access(x,id);
		}
		else if(t==2){
			scanf("%d",&y);
			printf("%d\n",query2(x,y));
		}
		else if(t==3){
			printf("%d\n",query3(x));
		}
	}
	return 0;
}

詳細信息

Test #1:

score: 10
Accepted
time: 4ms
memory: 16304kb

input:

1000 1000
1 2
2 3
1 4
4 5
3 6
4 7
7 8
8 9
9 10
8 11
10 12
10 13
13 14
13 15
12 16
15 17
15 18
14 19
17 20
18 21
18 22
21 23
19 24
22 25
21 26
22 27
24 28
27 29
25 30
27 31
28 32
30 33
29 34
33 35
34 36
33 37
35 38
37 39
38 40
40 41
39 42
39 43
41 44
44 45
45 46
46 47
45 48
45 49
47 50
50 51
50 52
48...

output:

3
9
51
49
65
20
65
11
4
7
4
3
21
2
12
5
10
4
38
5
6
20
3
1
2
20
16
2
6
7
12
12
9
4
12
2
12
6
12
8
5
11
12
5
4
13
5
9
5
2
13
2
6
13
3
5
2
2
14
3
5
7
12
4
3
7
15
4
14
3
9
13
12
3
2
4
5
5
2
10
13
1
3
8
9
4
4
13
13
14
14
4
13
10
13
13
5
7
2
3
2
3
12
7
1
6
13
5
4
4
9
5
4
2
4
14
4
10
7
3
8
5
3
6
4
5
15
4
...

result:

ok 647 lines

Test #2:

score: 10
Accepted
time: 210ms
memory: 39192kb

input:

100000 100000
1 2
2 3
3 4
4 5
5 6
5 7
7 8
7 9
8 10
9 11
11 12
12 13
13 14
13 15
14 16
15 17
17 18
18 19
19 20
20 21
21 22
21 23
22 24
23 25
25 26
26 27
26 28
28 29
29 30
29 31
30 32
32 33
33 34
34 35
34 36
35 37
36 38
37 39
38 40
40 41
40 42
42 43
42 44
44 45
44 46
45 47
47 48
47 49
48 50
49 51
50 5...

output:

39594
11295
11295
2631
4
2631
839
4
839
839
3
840
842
842
5
6
845
3
842
843
4
2
845
842
842
843
3
844
844
844
842
3
843
843
843
843
3
165
3
843
843
5
845
845
845
4
3
843
843
844
844
844
844
844
3
3
845
847
3
846
2
846
846
2
6
658
848
846
3
8
848
846
846
2
849
849
850
850
4
849
4
850
850
5
3
847
2
84...

result:

ok 50169 lines

Test #3:

score: 10
Accepted
time: 204ms
memory: 35128kb

input:

100000 100000
1 2
2 3
3 4
4 5
3 6
3 7
5 8
8 9
7 10
8 11
10 12
11 13
11 14
13 15
13 16
16 17
14 18
17 19
16 20
20 21
20 22
19 23
23 24
22 25
23 26
23 27
27 28
27 29
26 30
28 31
28 32
29 33
33 34
31 35
33 36
34 37
36 38
35 39
38 40
40 41
41 42
41 43
43 44
44 45
45 46
43 47
46 48
46 49
48 50
47 51
51 5...

output:

5
3
5
1452
8
2
1452
1453
1454
1453
2
8
5
436
436
437
9
438
3
12
3
6
439
439
438
6
439
440
442
17
9
7
8
10
10
4
2
2
437
3
7
2
438
438
2
3
2
4
438
4
8
439
3
3
3
3
438
4
4
6
439
439
6
14
440
9
441
439
439
4
440
2
6
440
440
5
2
441
4
12
443
6
442
4
442
363
5
7
438
7
439
33
439
439
439
439
5
3
3
7
440
4
...

result:

ok 49945 lines

Test #4:

score: 10
Accepted
time: 210ms
memory: 39296kb

input:

100000 100000
1 2
2 3
3 4
3 5
5 6
5 7
7 8
7 9
8 10
9 11
11 12
12 13
13 14
14 15
14 16
15 17
16 18
17 19
19 20
20 21
21 22
22 23
22 24
23 25
25 26
25 27
26 28
27 29
29 30
29 31
31 32
32 33
33 34
33 35
35 36
36 37
36 38
38 39
38 40
39 41
40 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
49 51
51 5...

output:

12519
7543
1
2
3
3
3
2
1
3
3008
1
1
2
4
2626
3
1
1
1
2
3
1
8
3
3
3
2
4
6
4
3
2
2
605
1
1
3
1
2
1
2
4
4
3
1
2
1
1
5
2
1
882
3
4
3
4
1
4
1
2
4
6
2
1133
7
3
1
6
3
3
3
6
4
1
3
2
4
1
4
7
2
3
4
7
5
719
2
1
1
3
2
3
2
2
4
3
5
3
2
3
6
2
2
1
5
5
6
3
2
2
1
2
5
2
2
3
5
4
2
4
5
3
2
7
5
1
2
7
3
1
4
602
3
3
2
84
3...

result:

ok 49825 lines

Test #5:

score: 10
Accepted
time: 224ms
memory: 34632kb

input:

100000 100000
1 2
2 3
1 4
4 5
1 6
5 7
5 8
7 9
6 10
7 11
9 12
11 13
9 14
10 15
13 16
16 17
14 18
18 19
18 20
19 21
18 22
22 23
21 24
23 25
25 26
23 27
25 28
25 29
27 30
27 31
29 32
28 33
33 34
30 35
34 36
32 37
36 38
35 39
38 40
40 41
37 42
40 43
40 44
44 45
45 46
42 47
43 48
46 49
45 50
49 51
48 52
...

output:

299
5
1
4
4
6
5
3
12
3
3
4
8
1289
10
2
3
3
1406
2
7
5
4
7
1
5
8
5
2
4
9
5
5
3
3
9
3
12
2
5
6
12
2
1
4
5
4
6
6
9
5
7
9
2
5
11
14
1
8
3
9
3
5
22
4
5
4
9
1
3
4
4
2
3
2
4
6
2
8
3
12
3
11
13
8
4
12
5
7
5
7
10
8
5
3
2
6
6
8
12
6
8
7
7
5
10
3
5
7
4
4
5
6
5
3
4
2
4
1
1
8
10
4
4
2
3
7
7
1
3
89
107
4
8
1
3
5
...

result:

ok 50152 lines

Test #6:

score: 10
Accepted
time: 296ms
memory: 34000kb

input:

100000 100000
1 2
2 3
1 4
4 5
4 6
5 7
7 8
4 9
4 10
6 11
7 12
7 13
7 14
13 15
9 16
10 17
14 18
10 19
18 20
19 21
4 22
8 23
15 24
5 25
21 26
14 27
12 28
23 29
17 30
7 31
26 32
28 33
30 34
17 35
21 36
11 37
14 38
7 39
13 40
38 41
8 42
11 43
38 44
21 45
7 46
37 47
22 48
42 49
17 50
37 51
31 52
16 53
22 ...

output:

19
21
21
8
11
20
22
10
14
10
11
13
9
20
17
17
12
17
10
11
8
13
15
18
23
18
22
13
5
14
18
18
9
18
13
16
11
9
18
11
10
8
7
12
10
6
10
13
20
23
13
15
18
12
10
16
21
21
19
24
10
19
24
15
14
24
13
13
9
15
21
7
8
13
7
13
12
12
11
14
16
10
9
11
8
9
15
7
9
13
12
13
8
21
17
16
7
17
10
19
17
15
4
7
14
15
15
8...

result:

ok 66508 lines

Test #7:

score: 10
Accepted
time: 78ms
memory: 28560kb

input:

50000 50000
1 2
2 3
3 4
4 5
4 6
5 7
6 8
7 9
9 10
10 11
10 12
12 13
13 14
13 15
15 16
15 17
17 18
17 19
18 20
20 21
20 22
21 23
23 24
24 25
25 26
25 27
26 28
28 29
29 30
30 31
30 32
32 33
33 34
34 35
35 36
35 37
36 38
38 39
39 40
40 41
41 42
42 43
43 44
43 45
44 46
45 47
46 48
47 49
49 50
49 51
51 52...

output:

6
4
2
3
1379
2
945
3
3
3
2
2910
1
4
519
4
519
520
4
2
520
5
2
520
521
5
5
5
521
3
1
2
519
520
3
1
1
2
2
3
1
2
520
520
4
521
1
3
5
3
4
1
521
521
521
521
1
4
3
3
2
521
2
5
2
3
522
3
2
4
520
520
3
4
4
3
521
3
2
2
521
4
3
1
127
1
1
2
127
127
3
2
128
3
128
2
129
1
3
129
1
1
2
2
1
6
129
1
2
6
4
130
2
130
...

result:

ok 33376 lines

Test #8:

score: 10
Accepted
time: 140ms
memory: 29332kb

input:

50000 100000
1 2
2 3
3 4
1 5
3 6
6 7
5 8
7 9
6 10
9 11
7 12
12 13
13 14
10 15
11 16
15 17
16 18
18 19
18 20
19 21
20 22
20 23
22 24
22 25
22 26
23 27
27 28
25 29
29 30
29 31
28 32
30 33
33 34
30 35
35 36
36 37
36 38
37 39
35 40
39 41
40 42
41 43
40 44
41 45
44 46
43 47
47 48
48 49
45 50
48 51
48 52
...

output:

4342
5544
142
12
18
2320
2028
404
2
961
6
2
57
2
3
9
4
10
3
4
6
271
10
271
5
9
6
6
1
272
7
14
3
8
9
272
5
4
4
273
8
90
11
3
9
10
5
277
5
33
6
6
4
2
2
10
13
273
8
5
3
4
5
4
1
274
7
2
2
2
6
4
4
8
6
4
3
6
276
8
4
6
4
4
13
5
2
1
1
5
2
4
9
275
275
275
275
275
7
4
7
8
3
6
2
13
15
4
11
127
9
9
8
5
2
11
3
6...

result:

ok 66993 lines

Test #9:

score: 10
Accepted
time: 174ms
memory: 37128kb

input:

100000 100000
1 2
2 3
1 4
1 5
5 6
3 7
7 8
6 9
6 10
7 11
8 12
9 13
11 14
11 15
14 16
15 17
14 18
16 19
19 20
19 21
18 22
21 23
20 24
22 25
24 26
25 27
24 28
25 29
26 30
29 31
28 32
31 33
30 34
31 35
35 36
35 37
37 38
35 39
38 40
40 41
39 42
41 43
43 44
42 45
44 46
44 47
44 48
46 49
46 50
47 51
51 52
...

output:

11194
19305
1
5586
17901
3
7069
6
2
9
227
5
6
498
502
12312
3
8976
5
6
12288
6
21
3
2
1676
1676
4
5
5
8
3
2
2
8
4
4
3
1680
3
2
1680
11
1681
7
3
127
4
5
4
8
5
3
2
6
6
3
4
3
3
396
396
4
4
397
4
9
9
2
8
3
7
2
7
3
3
14
5
4
6
8
9
401
6
401
5
8
9
396
2
397
1
1
1
6
4
5
5
4
8
5
3
399
3
7
399
6
11
11
4
8
7
4...

result:

ok 66877 lines

Test #10:

score: 10
Accepted
time: 182ms
memory: 33928kb

input:

100000 100000
1 2
2 3
1 4
2 5
4 6
6 7
5 8
6 9
8 10
10 11
10 12
11 13
12 14
13 15
13 16
14 17
15 18
16 19
18 20
20 21
19 22
20 23
23 24
24 25
23 26
24 27
26 28
28 29
29 30
30 31
29 32
31 33
32 34
32 35
33 36
34 37
37 38
38 39
37 40
40 41
41 42
41 43
42 44
42 45
45 46
46 47
45 48
47 49
49 50
50 51
51 ...

output:

37729
50002
4730
30097
2
4
11427
3
4
7190
4153
7
2
3
11
3
2
2
2107
2
1761
4
4
2
3
3
697
2109
6
509
2110
2110
2110
704
3
6
214
4
2111
2111
9
3
2110
2
2111
2112
2112
7
6
5
6
8
2112
3
1
4
9
6
4
2109
2109
2109
3
8
3
4
2110
6
527
5
1548
1548
5
1548
1549
1
2
1549
3
2
279
5
3
1
1331
5
4
4
5
4
9
7
4
699
699...

result:

ok 66671 lines