UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#196386#2409. 大茶馆snow_trace1002093ms104364kbC++113.1kb2023-10-24 11:51:482023-10-24 12:22:31

answer

#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) x&(-x)
#define int long long
unordered_map<int,vector<int> >mp;
int n,m;int nw ;
vector<int>p[400005];
vector<int>tag[400005];
vector<pair<pair<int,int>,int> >query;
vector<int>q[400005];
int lca[300005][20],dep[300005],fa[400005],rt[400005],vis[300005],ans[300005];
int tree[400005];
void upd(int pos,int add){
	for(int i = pos;i<400000;i+=lowbit(i))tree[i]+=add;return;
}int queryy(int pos){int res =0 ;
	for(int i = pos;i>0;i-=lowbit(i))res+=tree[i];
	return res;
}
int find(int x){
	if(fa[x] == x)return x;
	return fa[x] = find(fa[x]);
}void merge(int x,int y){
	int a = find(x),b=  find(y);
	if(a==b)return;
	int nd = ++nw;
	p[rt[a]].push_back(nd),p[nd].push_back(rt[a]);
	p[rt[b]].push_back(nd),p[nd].push_back(rt[b]);
//	cout << a << "  " << b << "  " << nd << endl;
	rt[b] = nd;fa[a] = b;
	return;
}void dfs(int now,int f){vis[now] = 1;
	dep[now] = dep[f]+1;lca[now][0] = f;
	for(int i = 1;i<18;i++)lca[now][i] = lca[lca[now][i-1]][i-1];
	for(int i = 0;i<p[now].size();i++){
		if(p[now][i] == f)continue;
		dfs(p[now][i],now);
	}
}int lcaa(int x,int y){
	if(dep[x]<dep[y])swap(x,y);
	//if(rt[find(x)]!=rt[find(y)])return 0;
	int dis = abs(dep[x]-dep[y]);
	for(int i =0;dis;i++,dis>>=1){
		if(dis&1)x = lca[x][i];
	}
	if(x == y)return x;
	for(int i = 17;i>=0;i--){
		if(lca[x][i]!=lca[y][i]){
			x = lca[x][i],y = lca[y][i];
		}
	}if(lca[x][0]!=lca[y][0])return 0;
	return lca[x][0];
}void dfs2(int now,int f){vis[now] = 1;
	for(int i = 0;i<tag[now].size();i++){
		upd(tag[now][i],1);
	}for(int i = 0;i<q[now].size();i++){
		ans[q[now][i]] += queryy(q[now][i]-1);
	}for(int i =0;i<p[now].size();i++){
		if(p[now][i] == f)continue;
		dfs2(p[now][i],now);
	}for(int i =0;i<tag[now].size();i++){
		upd(tag[now][i],-1);
	}
} 
signed main(){
	ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
	memset(ans,-1,sizeof(ans));
	cin >> n >> m;nw = n;
	for(int i= 1;i<=n;i++)fa[i] = i,rt[i] = i;
	for(int i = 1;i<=m;i++){
		int op;cin >> op;
		if(op == 1){
			int x,y;cin >> x >> y;
			if(x>y)swap(x,y);
		//	cout <<  "     " << x << " " << y << endl;
			mp[x*n+y].push_back(i);merge(x,y);
		}if(op == 2){
			int x;cin >> x;
			tag[rt[find(x)]].push_back(i);
		//	cout << " " << x << " "<< rt[find(x)] << " " << i << endl;
		}if(op == 3){
			int x,y;cin >>x  >> y;
			if(x>y)swap(x,y);
			query.push_back({{x,y},i});
		}
	}for(int i =nw;i>=1;i--){
		if(!vis[i])dfs(i,i);
	}//cout << 1 << endl;
//	cout << "   " << mp[{10,10}] << endl;
	for(int i =0;i<query.size();i++){
		int x = query[i].first.first,y = query[i].first.second,t = query[i].second;
		int l = lcaa(x,y);
	//	cout  << "  "<< x << " " << y << " " << l << endl;mp[x*n+y].push_back((n+1)*(n+1));
		q[l].push_back(t);ans[t] = lower_bound(mp[x*n+y].begin(),mp[x*n+y].end(),t)-mp[x*n+y].begin();
	//	cout << ans[t] << endl;
	}memset(vis,0,sizeof(vis));
	//cout << 11 << endl;
	for(int i = nw;i>=1;i--){
		if(!vis[i])dfs2(i,i);
	}for(int i = 1;i<=m;i++){
		if(ans[i]!=-1)cout << ans[i] << '\n';
	}
	return 0;
}/*
8 10
1 3 6
2 6
2 2
2 7
1 7 3
3 2 6
1 4 2
3 3 7
1 2 6
2 4
*/

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 4
Accepted
time: 12ms
memory: 34164kb

input:

10 100
1 3 7
1 1 6
3 10 3
1 3 10
1 5 10
2 7
2 6
3 6 10
3 10 3
1 4 6
2 1
2 3
2 3
1 4 10
2 10
3 6 7
3 ...

output:

0
0
2
1
4
2
3
0
0
0
0
3
0
4
0
3
4
4
5
1
11
9
6
12
11
16
19
18
18
17
17
17
14
15
14
14
16
19
18
22
18...

result:

ok 43 lines

Test #2:

score: 4
Accepted
time: 0ms
memory: 34188kb

input:

100 100
1 13 17
1 81 86
3 40 3
1 93 90
1 45 20
2 67
2 56
3 16 30
3 100 73
1 74 76
2 51
2 63
2 63
1 3...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

result:

ok 39 lines

Test #3:

score: 4
Accepted
time: 4ms
memory: 34164kb

input:

10 100
1 3 7
1 1 6
3 10 3
1 3 10
1 5 10
2 7
2 6
3 6 10
3 10 3
1 4 6
2 1
2 3
2 3
1 4 10
2 10
3 6 7
3 ...

output:

0
0
2
1
4
2
3
0
0
0
0
3
0
4
0
3
4
4
5
1
11
9
6
12
11
16
19
18
18
17
17
17
14
15
14
14
16
19
18
22
18...

result:

ok 43 lines

Test #4:

score: 4
Accepted
time: 3ms
memory: 34188kb

input:

100 100
1 13 17
1 81 86
3 40 3
1 93 90
1 45 20
2 67
2 56
3 16 30
3 100 73
1 74 76
2 51
2 63
2 63
1 3...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

result:

ok 39 lines

Test #5:

score: 4
Accepted
time: 16ms
memory: 35316kb

input:

1000 10000
1 13 517
1 281 586
3 540 103
1 893 890
1 45 320
2 667
2 556
3 716 630
3 1000 173
1 174 27...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 3341 lines

Test #6:

score: 4
Accepted
time: 12ms
memory: 35084kb

input:

500 10000
1 221 62
1 489 199
2 459
1 254 297
3 446 242
1 158 398
1 382 460
2 114
1 95 363
3 326 155
...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 3284 lines

Test #7:

score: 4
Accepted
time: 11ms
memory: 35320kb

input:

1000 10000
2 903
2 488
2 269
2 282
3 532 489
1 872 426
3 275 340
1 524 804
3 629 49
3 457 904
2 229
...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 3386 lines

Test #8:

score: 4
Accepted
time: 174ms
memory: 69468kb

input:

100000 200000
3 79013 45517
3 15281 69586
3 2540 52103
3 2893 60890
3 70045 39320
3 27667 63305
3 12...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 200000 lines

Test #9:

score: 4
Accepted
time: 306ms
memory: 81520kb

input:

100000 300000
3 79013 45517
3 15281 69586
3 2540 52103
3 2893 60890
3 70045 39320
3 27667 63305
3 12...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 300000 lines

Test #10:

score: 4
Accepted
time: 5ms
memory: 34164kb

input:

10 100
1 3 7
1 1 6
3 10 3
1 3 10
1 5 10
2 7
2 6
3 6 10
3 10 3
1 4 6
2 1
2 3
2 3
1 4 10
2 10
3 6 7
3 ...

output:

0
0
2
1
4
2
3
0
0
0
0
3
0
4
0
3
4
4
5
1
11
9
6
12
11
16
19
18
18
17
17
17
14
15
14
14
16
19
18
22
18...

result:

ok 43 lines

Test #11:

score: 4
Accepted
time: 12ms
memory: 34188kb

input:

100 100
1 13 17
1 81 86
3 40 3
1 93 90
1 45 20
2 67
2 56
3 16 30
3 100 73
1 74 76
2 51
2 63
2 63
1 3...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

result:

ok 39 lines

Test #12:

score: 4
Accepted
time: 13ms
memory: 35320kb

input:

1000 10000
1 13 517
1 281 586
3 540 103
1 893 890
1 45 320
2 667
2 556
3 716 630
3 1000 173
1 174 27...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 3341 lines

Test #13:

score: 4
Accepted
time: 12ms
memory: 35080kb

input:

500 10000
1 221 62
1 489 199
2 459
1 254 297
3 446 242
1 158 398
1 382 460
2 114
1 95 363
3 326 155
...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 3284 lines

Test #14:

score: 4
Accepted
time: 8ms
memory: 35324kb

input:

1000 10000
2 903
2 488
2 269
2 282
3 532 489
1 872 426
3 275 340
1 524 804
3 629 49
3 457 904
2 229
...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 3386 lines

Test #15:

score: 4
Accepted
time: 177ms
memory: 69468kb

input:

100000 200000
3 79013 45517
3 15281 69586
3 2540 52103
3 2893 60890
3 70045 39320
3 27667 63305
3 12...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 200000 lines

Test #16:

score: 4
Accepted
time: 288ms
memory: 81520kb

input:

100000 300000
3 79013 45517
3 15281 69586
3 2540 52103
3 2893 60890
3 70045 39320
3 27667 63305
3 12...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 300000 lines

Test #17:

score: 4
Accepted
time: 161ms
memory: 80412kb

input:

100000 200000
1 1 81089
1 1 44225
2 1
1 1 96265
2 1
1 1 78155
2 1
2 1
1 1 75754
2 1
2 1
2 1
2 1
1 1 ...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 22336 lines

Test #18:

score: 4
Accepted
time: 116ms
memory: 49592kb

input:

2000 200000
1 1013 1517
1 1281 1586
3 540 103
1 893 890
1 45 1320
2 1667
2 556
3 1716 1630
3 2000 11...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 66340 lines

Test #19:

score: 4
Accepted
time: 140ms
memory: 53536kb

input:

10000 200000
1 9013 5517
1 5281 9586
3 2540 2103
1 2893 890
1 45 9320
2 7667
2 2556
3 3716 9630
3 80...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 66350 lines

Test #20:

score: 4
Accepted
time: 262ms
memory: 84168kb

input:

100000 200000
1 79013 45517
1 15281 69586
3 2540 52103
1 2893 60890
1 70045 39320
2 27667
2 82556
3 ...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 66352 lines

Test #21:

score: 4
Accepted
time: 361ms
memory: 104364kb

input:

100000 299997
1 89714 45945
2 89714
3 89714 45945
1 45945 14996
2 89714
3 14996 45945
1 14996 3009
2...

output:

2
2
4
5
1
6
6
3
5
2
7
4
3
1
14
7
7
17
13
2
18
23
12
8
1
21
6
13
17
21
5
27
5
26
2
19
3
39
40
25
22
9...

result:

ok 99999 lines

Extra Test:

score: 0
Extra Test Passed