QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#31716#4046. 钥匙 gsh100 ✓1375ms231640kbC++233.5kb2022-05-11 20:18:222022-05-11 20:18:23

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-11 20:18:23]
  • 评测
  • 测评结果:100
  • 用时:1375ms
  • 内存:231640kb
  • [2022-05-11 20:18:22]
  • 提交

answer

#include<algorithm>
#include<iostream>
#include<random>
#include<vector>
#include<cstdio>
using namespace std;
#define For(i,l,r) for(int i=l;i<=r;i++)
#define FOR(i,l,r) for(int i=l;i>=r;i--)
#define lowbit(x) x&-x
#define MAXN 1000001
int N,M,type[MAXN],c[MAXN],tot,dfn[MAXN],siz[MAXN],fa[MAXN],dep[MAXN],son[MAXN],top[MAXN],x[MAXN],y[MAXN],bit[MAXN],ans[MAXN];vector<int>E[MAXN],E2[MAXN],id[MAXN];vector<pair<int,int>>opt[MAXN],ask[MAXN];
int get(){int x=0,f=1;char c=getchar();while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return x*f;}
void dfs1(int u,int f){siz[u]=1,dep[u]=dep[f]+1,fa[u]=f;for(auto v:E[u])if(v!=f)dfs1(v,u),siz[u]+=siz[v],siz[v]>siz[son[u]]&&(son[u]=v);}
void dfs2(int u,int t){top[u]=t,dfn[u]=++tot;if(!son[u])return;dfs2(son[u],t);for(auto v:E[u])if(v!=fa[u]&&v!=son[u])dfs2(v,v);}
int lca(int x,int y){while(top[x]!=top[y]){if(dep[top[x]]<dep[top[y]])swap(x,y);x=fa[top[x]];}return dep[x]<dep[y]?x:y;}
int nxt(int x,int y){if(dfn[y]>=dfn[son[x]]&&dfn[y]<dfn[son[x]]+siz[son[x]])return son[x];while(fa[top[y]]!=x)y=fa[top[y]];return top[y];}
void add(int x,int y)
{
	int z=lca(x,y);if(z!=x&&z!=y){opt[dfn[x]].push_back({dfn[y],dfn[y]+siz[y]-1});opt[dfn[x]+siz[x]].push_back({-dfn[y],-(dfn[y]+siz[y]-1)});return;}
	if(z==x){x=nxt(x,y);opt[1].push_back({dfn[y],dfn[y]+siz[y]-1});opt[dfn[x]].push_back({-dfn[y],-(dfn[y]+siz[y]-1)});opt[dfn[x]+siz[x]].push_back({dfn[y],dfn[y]+siz[y]-1});return;}
	y=nxt(y,x);opt[dfn[x]].push_back({1,dfn[y]-1});opt[dfn[x]].push_back({dfn[y]+siz[y],N});
	opt[dfn[x]+siz[x]].push_back({-1,-(dfn[y]-1)});opt[dfn[x]+siz[x]].push_back({-dfn[y]-siz[y],-N});
}
void dfs3(int u,int f,int rt,int col,int val)
{
	for(auto v:E2[u])if(v!=f)
	{
		if(c[v]==col&&type[v]==2&&val==0){add(rt,v);continue;}
		int y=c[v]==col?type[v]==1?1:-1:0;dfs3(v,u,rt,col,val+y);
	}
}
void upd(int x,int v){for(int i=x;i<=N;i+=lowbit(i))bit[i]+=v;}int query(int x){int ans=0;for(int i=x;i;i-=lowbit(i))ans+=bit[i];return ans;}
void Upd(int l,int r){if(l<0||r<0)upd(-l,-1),upd(-r+1,1);else upd(l,1),upd(r+1,-1);}
int main()
{
	N=get(),M=get();For(i,1,N)type[i]=get(),c[i]=get(),id[c[i]].push_back(i);
	For(i,1,N-1){int u=get(),v=get();E[u].push_back(v),E[v].push_back(u);}For(i,1,M)x[i]=get(),y[i]=get();
	dfs1(1,0);dfs2(1,1);For(i,1,N)sort(id[i].begin(),id[i].end(),[&](const int&a,const int&b){return dfn[a]<dfn[b];});
	For(i,1,N)if(!id[i].empty())
	{
		vector<int>sta,nodes;sta.push_back(1);nodes.push_back(1);
		for(auto u:id[i])
		{
			int x=lca(sta.back(),u);if(u==sta.back())continue;
			while(x!=sta.back())
			{
				int y=sta[sta.size()-2];
				if(dep[x]>dep[y]){int z=sta.back();sta.pop_back();nodes.push_back(x);nodes.push_back(z);E2[z].push_back(x);E2[x].push_back(z);sta.push_back(x);break;}
				if(x==y){int z=sta.back();sta.pop_back();nodes.push_back(x);nodes.push_back(z);E2[z].push_back(x);E2[x].push_back(z);break;}
				int z=sta.back();sta.pop_back();nodes.push_back(y);nodes.push_back(z);E2[z].push_back(y);E2[y].push_back(z);
			}
			if(u!=sta.back())sta.push_back(u);
		}
		while(sta.size()>1)nodes.push_back(sta.back()),E2[sta.back()].push_back(sta[sta.size()-2]),E2[sta[sta.size()-2]].push_back(sta.back()),sta.pop_back();
		for(auto u:id[i])if(type[u]==1)dfs3(u,0,u,i,0);for(auto u:nodes)E2[u].clear();
	}
	For(i,1,M)ask[dfn[x[i]]].push_back({i,dfn[y[i]]});
	For(i,1,N){for(auto j:opt[i])Upd(j.first,j.second);for(auto j:ask[i])ans[j.first]=query(j.second);}
	For(i,1,M)cout<<ans[i]<<'\n';
}

詳細信息

Test #1:

score: 10
Accepted
time: 19ms
memory: 120816kb

input:

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

output:

0
0
0
2
0
0
1
0
0
0
0
1
2
0
0
0
0
0
0
1
1
1
2
1
0
1
1
0
0
0
0
0
0
0
0
0
0
0
1
2
0
1
1
0
0
2
0
1
0
0
0
1
0
0
0
0
0
0
0
0
1
0
0
0
0
1
0
2
0
1
0
0
0
1
0
0
0
0
1
0
0
2
0
0
0
0
1
0
0
1
0
0
3
0
0
0
0
0
0
1

result:

ok 100 numbers

Test #2:

score: 10
Accepted
time: 37ms
memory: 121880kb

input:

5000 5000
1 24
2 102
1 215
2 24
2 141
2 109
2 252
1 235
2 77
2 278
2 292
1 12
2 201
2 227
2 238
1 152
1 116
2 204
2 122
1 149
2 284
2 254
2 115
2 234
2 203
2 238
2 291
2 58
1 289
2 105
1 33
2 236
2 184
2 57
2 121
2 17
2 245
2 134
1 245
2 73
1 26
2 240
2 15
1 129
2 196
1 23
2 279
2 168
2 48
2 206
2 3...

output:

0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
1
0
1
0
1
0
1
0
0
0
0
0
1
1
0
0
1
1
0
0
0
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
1
0
0
0
0
0
1
0
1
0
0
0
0
0
0
0
1
0
0
1
0
1
1
0
0
0
1
0
1
0
0
0
1
0
0
0
1
1
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
1
1
0
1
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
1
0
0
1
1
0
0
0
0
0
0
...

result:

ok 5000 numbers

Test #3:

score: 10
Accepted
time: 28ms
memory: 121792kb

input:

5000 5000
2 230
2 243
2 77
2 149
2 298
2 176
1 103
2 131
1 127
2 110
1 115
1 220
1 23
2 259
2 290
2 77
2 211
2 249
2 232
2 163
2 55
2 277
2 148
2 171
2 14
2 226
2 70
1 194
1 269
2 277
2 4
2 107
1 246
2 226
2 79
2 219
1 21
2 203
2 4
2 129
2 87
1 114
2 180
2 37
2 202
2 5
2 39
1 28
2 168
2 35
1 184
2 1...

output:

0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
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 5000 numbers

Test #4:

score: 10
Accepted
time: 127ms
memory: 139832kb

input:

100000 100000
2 56
1 2499
2 5148
2 2178
2 106
2 5422
2 2276
1 1085
2 1681
1 528
1 4743
2 3591
2 4902
2 5943
1 2664
2 5377
2 1923
2 195
2 1765
2 3177
2 3947
2 649
2 4772
2 3331
2 547
2 2908
2 4684
2 2741
2 5343
2 2494
2 3140
2 3823
1 1817
1 5225
2 3689
2 2768
2 6070
1 3584
1 2328
1 1506
2 4544
2 3323...

output:

5205
2591
208
3767
2284
3926
1985
438
1821
416
2614
3977
96
1397
2536
322
1682
100
396
1550
3809
1219
0
594
2058
169
860
2089
2549
5999
1837
2585
23
424
3309
371
383
4510
446
1975
967
547
1136
5210
2279
3656
0
126
1893
311
7977
454
4762
258
2000
2440
3498
2989
7478
2691
608
2620
5296
4403
4699
1925
...

result:

ok 100000 numbers

Test #5:

score: 10
Accepted
time: 133ms
memory: 139288kb

input:

100000 100000
1 771
2 5185
2 1153
1 1611
2 2438
2 3680
2 5272
2 13
2 3069
2 3468
2 2209
2 6050
2 3287
2 1728
2 4981
2 1757
2 3630
2 1746
2 3057
1 726
2 2900
2 2033
2 2601
1 1478
2 214
1 5337
2 3948
2 1703
2 5112
2 5533
2 3558
2 1891
2 3887
2 2952
1 3425
1 177
1 1297
1 1916
2 1754
2 710
2 5212
1 1117...

output:

2149
1638
699
2224
2602
2596
401
1004
4
800
557
2105
3555
2186
2732
3767
1474
1972
3163
4410
2246
1899
1484
143
2349
388
1692
574
691
1414
3048
3147
1865
1487
1970
973
2104
1430
3776
618
750
887
3095
131
2560
3480
1730
3088
1026
3257
1164
122
3208
4370
3212
997
335
524
628
5280
1407
749
321
116
315
...

result:

ok 100000 numbers

Test #6:

score: 10
Accepted
time: 1298ms
memory: 218400kb

input:

500000 1000000
1 64861
1 119062
1 65144
1 143894
1 73264
2 33253
2 173191
2 52204
1 40950
2 71504
1 94677
2 28492
2 212348
2 155025
1 189945
2 71636
2 137081
2 129336
1 150409
2 200735
1 127414
2 155968
1 83751
2 29575
1 25125
1 111882
2 83939
1 64246
1 195870
1 14834
2 10887
2 98830
1 197278
2 1160...

output:

16
1
15
4
9
21
36
70
11
24
53
18
2
62
16
30
24
51
32
10
59
122
16
8
5
55
53
22
11
12
17
12
42
70
37
26
13
34
6
101
11
22
18
21
84
30
35
39
67
24
11
8
0
25
40
1
30
42
38
121
23
37
9
9
37
45
30
33
24
5
22
43
43
20
20
14
41
6
10
20
54
36
19
97
32
19
6
48
14
48
48
80
33
2
3
12
18
11
33
1
47
27
54
17
93
...

result:

ok 1000000 numbers

Test #7:

score: 10
Accepted
time: 1375ms
memory: 218400kb

input:

500000 1000000
2 99254
2 207080
2 40178
2 203616
2 143664
1 172563
1 235135
1 39267
2 34014
2 240014
2 122830
2 65547
1 190038
1 133892
1 195039
2 242508
1 16681
1 171326
1 179195
1 28872
2 91496
2 196836
1 12722
1 133176
1 49610
1 160238
2 137275
2 82747
2 22809
2 148101
2 18675
1 120676
1 86886
1 ...

output:

90
11
22
19
119
155
49
44
37
66
62
24
11
62
89
56
28
60
10
22
177
10
95
67
57
52
84
126
93
5
106
34
53
37
8
47
53
33
135
30
105
50
51
57
4
101
91
168
11
70
1
46
142
3
131
70
49
98
121
13
61
1
68
61
38
94
20
112
31
159
23
47
47
4
155
135
109
78
142
35
36
9
24
49
0
34
58
95
98
187
91
126
6
32
31
58
63...

result:

ok 1000000 numbers

Test #8:

score: 10
Accepted
time: 1242ms
memory: 218500kb

input:

500000 1000000
1 89685
2 77808
2 60421
1 3478
1 174801
2 79073
1 171086
2 189510
2 141242
2 42469
2 197806
1 17953
2 165030
1 200212
2 65004
2 67818
2 86070
1 111422
1 229646
2 68851
1 14764
1 230949
2 71728
1 45456
1 5844
1 191082
1 54319
2 135488
2 24463
1 2976
2 226836
1 57817
2 45114
2 58082
1 3...

output:

21
33
31
22
54
59
25
2
38
39
7
42
45
132
98
12
18
24
5
34
38
36
4
11
41
14
36
36
18
2
33
8
35
42
28
8
13
64
18
41
12
29
67
32
6
34
26
22
33
80
39
66
8
11
84
52
45
120
74
53
9
37
28
59
65
36
29
11
4
38
18
10
6
66
20
22
38
32
21
27
12
37
35
22
29
37
31
6
39
33
38
13
36
68
39
21
77
28
46
38
33
30
12
7
...

result:

ok 1000000 numbers

Test #9:

score: 10
Accepted
time: 1373ms
memory: 231640kb

input:

500000 1000000
1 6055
2 7862
2 1392
2 1440
2 15064
2 460
1 29587
2 3993
2 29185
2 29543
2 583
1 19479
2 28783
2 26722
1 17041
2 10894
2 3234
2 25053
1 15206
1 6122
2 25838
1 28948
2 25858
2 27787
2 19146
2 27381
2 20541
2 24645
1 16937
2 3163
2 3194
2 25925
2 11649
2 20379
1 7937
2 1913
2 18048
1 94...

output:

411
2
789
108
281
204
76
509
45
647
9
16
279
164
433
63
173
217
324
86
107
43
202
535
464
94
64
227
267
46
13
350
216
218
575
73
128
230
365
285
131
200
75
364
9
46
8
35
271
81
100
563
436
225
545
129
255
115
150
854
115
133
119
108
517
108
726
255
32
147
292
190
78
319
79
64
176
100
360
100
289
349...

result:

ok 1000000 numbers

Test #10:

score: 10
Accepted
time: 1359ms
memory: 231548kb

input:

500000 1000000
1 21782
1 23178
2 3615
1 22855
2 689
2 18959
2 17609
2 21923
1 28277
2 16406
2 7256
1 10175
2 25925
2 15485
2 25959
2 25908
1 20117
2 14563
1 25592
2 15719
1 9408
2 9539
1 28533
2 14576
2 2884
2 2701
1 13792
1 4222
2 21169
1 9207
2 17859
2 21128
1 3543
1 25941
2 4987
2 2529
2 20743
2 ...

output:

22
96
178
186
24
410
355
316
152
349
7
628
247
152
573
216
101
30
172
189
76
8
127
95
351
69
181
139
204
175
261
6
195
96
211
39
159
550
126
40
259
324
252
48
126
342
143
513
235
331
142
173
153
257
133
120
252
318
178
775
161
216
146
18
221
78
214
248
180
172
196
180
262
501
155
149
149
228
127
187...

result:

ok 1000000 numbers

Extra Test:

score: 0
Extra Test Passed