QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#42473#4401. Prizecatthomas10 2402ms264996kbC++3.8kb2022-08-02 15:36:372022-08-02 15:36:39

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-08-02 15:36:39]
  • 评测
  • 测评结果:10
  • 用时:2402ms
  • 内存:264996kb
  • [2022-08-02 15:36:37]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
inline void Flu(){fflush(stdout);}
const int maxm=100025;
const int maxn=1000025;
int n,K,Q,T,m,i,j,t,k,s,Log[maxn];
struct Edge
{
	int nxt,aim;
};
bool lit[maxn];
int seq[maxm],tsq,stk[maxm],tp,tof[maxm],quer[maxm][4],ord[maxm];
struct Tree
{
	int N,head[maxn],nrt,fat[maxn][21],dfn[maxn],bac[maxn];
	int dep[maxn],tod[maxn],dep2[maxn];
	Edge edge[maxn];
	inline void add_edge(int x,int y)
	{
		if (x==-1){nrt=y;return;}
		fat[y][0]=x;
		edge[++N]=(Edge){head[x],y};head[x]=N;
		//edge[++N]=(Edge){head[y],x};head[y]=N;
	}
	void dfs_gt_seq(int x)
	{
		lit[x]=1;ord[++tsq]=x;if (tsq>=K) return;
		for (int i=head[x];i;i=edge[i].nxt)
		{
			int des=edge[i].aim;
			dfs_gt_seq(des);if (tsq>=K) return;
		}
	}
	void dfs1(int x)
	{
		dfn[x]=++t;bac[t]=x;dep[x]=dep[fat[x][0]]+1;
		for (int i=1;i<=Log[n];++i) fat[x][i]=fat[fat[x][i-1]][i-1];
		for (int i=head[x];i;i=edge[i].nxt)
		{
			int des=edge[i].aim;
			dfs1(des);
		}
	}
	int LCA(int x,int y)
	{
		if (dep[x]<dep[y]) swap(x,y);
		for (int i=Log[n];~i;--i) if (dep[fat[x][i]]>=dep[y]) x=fat[x][i];
		if (x==y) return x;
		for (int i=Log[n];~i;--i) if (fat[x][i]^fat[y][i])
		 x=fat[x][i],y=fat[y][i];
		return fat[x][0];
	}
	void dfs2(int x)
	{
		dep2[x]=dep2[fat[x][0]]+tod[x];
		for (int i=head[x];i;i=edge[i].nxt)
		{
			int des=edge[i].aim;
			dfs2(des);
		}
	}
	void Process1()
	{
		tsq=0;
		for (int i=1;i<=n;++i)
		 if (lit[bac[i]]) seq[++tsq]=bac[i];
		for (int i=1;i<=tsq;++i) printf("%d ",seq[i]);puts("");
		for (int i=1;i<tsq;++i) printf("? %d %d\n",seq[i],seq[i+1]);
		puts("!");Flu();
		for (int i=1;i<tsq;++i)
		 scanf("%d%d%d%d",&quer[i][0],&quer[i][1],
		  &quer[i][2],&quer[i][3]);
		stk[tp=1]=seq[1];
		for (int i=2;i<=tsq;++i)
		{
			int lc=LCA(stk[tp],seq[i]),lst=-1,len=quer[i-1][0];
			while (tp&&dep[stk[tp]]>dep[lc])
			{
				if (lst!=-1) tod[lst]=tof[tp+1],len-=tof[tp+1];
				lst=stk[tp];--tp;
			}
			if (stk[tp]^lc)
			{
				stk[++tp]=lc;
				if (lst!=-1) tod[lst]=len;
				tof[tp]-=len;
			}else if (lst!=-1) tod[lst]=len;
			stk[++tp]=seq[i];tof[tp]=quer[i-1][1];
		}
		while (tp>1) tod[stk[tp]]=tof[tp],--tp;
		dfs2(nrt);
	}
	int pre[maxn],nxt[maxn],qv[maxn][2];
	void Process2()
	{
		for (int i=1;i<tsq;++i)
		{
			int t0=seq[i],t1=seq[i+1];
			nxt[t0]=t1;pre[t1]=t0;
			qv[t0][0]=quer[i][2];qv[t0][1]=quer[i][3];
		}
		int nowtt=tsq;
		while (nowtt>1)
		{
			int now=ord[nowtt];--nowtt;
			int t0=pre[now],t1=nxt[now];
			if (!t1)
			{
				int lc0=LCA(t0,now);
				nxt[t0]=0;
				tod[now]=lc0;dep2[now]=qv[t0][1];
				continue;
			}
			else if (!t0)
			{
				int lc1=LCA(t1,now);
				pre[t1]=0;
				tod[now]=lc1;dep2[now]=qv[now][0];
				continue;
			}
			int lc0=LCA(t0,now),lc1=LCA(t1,now);
			tod[now]=lc0;dep2[now]=qv[t0][1];
			qv[t0][1]=0ll+qv[t0][1]+qv[now][0]+qv[now][1]
			 -2ll*min(qv[t0][1],qv[now][0]);
			pre[t1]=t0;nxt[t0]=t1;
		}
		dep2[nrt]=0;
		for (int i=2;i<=tsq;++i)
		 dep2[ord[i]]+=dep2[tod[ord[i]]];
	}
	int gtds(int x,int y){return 0ll+dep2[x]-2ll*dep2[LCA(x,y)]+dep2[y];}
}tr1,tr2;

int ask[maxm][2];

int main()
{
	for (i=2;i<maxn;++i)
	 Log[i]=Log[i>>1]+1;
	scanf("%d%d%d%d",&n,&K,&Q,&T);
	for (i=1;i<=n;++i)
	{
		scanf("%d",&t);
		tr1.add_edge(t,i);
	}
	for (i=1;i<=n;++i)
	{
		scanf("%d",&t);
		tr2.add_edge(t,i);
	}
	tr2.dfs_gt_seq(tr2.nrt);
	t=0;tr1.dfs1(tr1.nrt);t=0;tr2.dfs1(tr2.nrt);
	tr1.Process1();
	tr2.Process2();
	Flu();
	for (i=1;i<=T;++i)
	 scanf("%d%d",&ask[i][0],&ask[i][1]),Flu();
	for (i=1;i<=T;++i)
	{
		printf("%d %d\n",tr1.gtds(ask[i][0],ask[i][1]),tr2.gtds(ask[i][0],ask[i][1]));
	}
	Flu();
	return 0;
}
/*
9 3 2 3
2 -1 2 1 1 5 1 4 5
9 4 5 5 7 3 -1 3 7

0 3 5 0
3 4 0 1
1 7
7 9
1 9

*/

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 1040ms
memory: 138028kb

input:

500000 64682 64681 100000
46115
470589
209303
2979
473162
343535
79503
299539
404621
102085
237721
279170
392890
165201
441593
456314
218991
358478
86614
410800
159785
169761
95368
285837
297549
370283
378974
26449
444381
39320
149913
404523
144109
174828
263837
49847
468694
478535
152644
216598
301...

output:

422989 414496 290928 388223 160563 301045 470257 259625 222733 231286 345214 169817 435263 277447 386014 210139 455433 225855 264772 199736 355788 288506 233893 146148 454958 267562 498596 183745 352665 151125 266374 43142 9414 204593 212097 311775 25324 300764 6643 94847 396968 428563 311355 255767...

result:

ok good job!

Test #2:

score: 0
Accepted
time: 1134ms
memory: 138820kb

input:

500000 90967 90966 100000
122547
312039
290084
118442
352297
175176
294396
496975
127062
90539
132654
408480
493670
419897
53432
141795
264165
60368
473480
5634
253119
64236
85346
422987
28583
262389
111931
271291
13577
415079
132797
256502
76402
265607
11274
289667
398726
32021
302401
410650
369760...

output:

3090 193269 3028 186608 498475 64618 82114 231445 7541 329983 134623 235591 70401 18906 403427 280451 146897 355174 160090 144279 193430 332022 488244 228900 80781 84465 218682 27818 6035 368489 155673 440755 443926 241570 193717 143661 374105 56616 323329 95909 337798 20531 236329 28564 437244 4969...

result:

ok good job!

Test #3:

score: 0
Accepted
time: 730ms
memory: 132300kb

input:

500000 68287 68286 100000
273928
229768
65518
144983
311611
494773
489379
439644
467893
456131
430188
247387
485565
272285
474827
476962
338340
365804
344570
390867
390170
456217
43185
447057
385874
305750
107742
230530
259907
252254
280920
16831
45761
185191
117450
55891
175190
255615
35904
14855
2...

output:

242387 475865 321066 209201 462604 214253 196699 226268 117131 350699 80452 25767 119995 214529 357833 292947 225261 88518 406492 280325 288052 472421 212781 374357 131433 126129 146914 100104 462425 237524 61399 118483 69532 7167 205586 192148 457094 51756 22755 163842 266528 164794 33213 463264 24...

result:

ok good job!

Test #4:

score: 0
Accepted
time: 784ms
memory: 132392kb

input:

500000 63976 63975 100000
230132
63748
303785
13497
431672
370351
360004
412191
378555
409703
485802
218204
475692
27602
220794
398856
89157
166559
116145
350738
277404
196706
40307
118602
171802
378360
389092
485168
224465
383516
33147
322617
254917
274019
57283
272241
216098
421952
489927
75641
40...

output:

210552 497883 480811 452802 492417 418837 489998 482202 374848 289765 110680 44797 89513 378682 31352 85884 239398 471868 462423 126653 81678 479584 465851 494207 338718 467250 307848 66039 38694 318222 497955 274328 218770 211188 137818 464748 74827 307971 311906 435586 96725 263480 427209 316995 4...

result:

ok good job!

Test #5:

score: 0
Accepted
time: 774ms
memory: 132648kb

input:

500000 87673 87672 100000
151599
456749
347511
703
348209
260440
488627
416030
419890
408089
83617
120781
133411
374231
460689
211838
137587
252914
392401
321583
55161
335205
334340
4527
14086
142229
197076
17695
262896
258702
273353
51181
10968
366799
324067
299421
281975
7236
420627
92324
299845
1...

output:

51300 486608 447632 311856 176217 140269 41860 495622 336304 407115 326281 422501 237284 228965 459131 164231 210096 36000 18635 113069 424572 369989 278926 5557 285982 428570 246301 37534 439333 362562 335977 270335 150717 217178 479485 216679 181482 158511 101150 450818 65767 281479 254609 76742 3...

result:

ok good job!

Test #6:

score: 0
Accepted
time: 985ms
memory: 133604kb

input:

500000 77912 77911 100000
270576
129318
366297
25873
179787
473782
221947
331327
209469
412992
410608
286179
37554
355546
297085
420463
496948
223036
122019
151250
478469
468136
19073
318549
398897
364415
23730
407160
26064
436939
30150
336421
375149
131841
58480
259944
117641
414831
64311
336164
31...

output:

210887 450513 372367 243217 400064 17878 393825 463407 419374 324697 246607 415699 193455 464346 123412 360569 179912 398688 70886 255935 399564 225125 66793 171898 292203 303280 310037 168995 59490 80738 100068 33512 34314 337722 389758 398573 961 150261 487444 449590 366771 480658 469084 386039 24...

result:

ok good job!

Test #7:

score: 0
Accepted
time: 943ms
memory: 133380kb

input:

500000 77688 77687 100000
433011
472346
395389
187114
436024
138403
189990
398859
136147
195283
331183
46789
19828
335128
387768
442181
65556
72327
318927
462834
421288
227912
37067
387794
145879
258896
185861
356020
202881
490952
443694
95413
137215
137239
112863
481338
167802
304239
309781
391976
...

output:

176419 412347 156219 429048 311400 237666 376930 358105 172116 223728 347566 163517 24933 222554 62652 450905 111814 292285 381302 9722 20522 237978 362286 151412 329939 131882 35390 373863 273121 290480 204978 443479 492305 157831 85371 426614 320962 313401 260605 124678 401442 429465 35096 116573 ...

result:

ok good job!

Test #8:

score: 0
Accepted
time: 973ms
memory: 133536kb

input:

500000 70973 70972 100000
449081
8094
7358
89457
426121
454508
470543
485236
63347
441977
422774
88672
243638
499709
170209
157788
229166
106888
228931
289706
435222
496384
381579
323479
499140
1511
385050
44171
413854
248273
352221
305112
24289
277461
391744
395003
85800
396455
355110
186446
285096...

output:

449195 453335 431734 359470 262646 246698 118545 434345 308679 54000 429984 58520 150175 496485 320496 226752 157757 411306 471147 177546 84102 21718 405045 229436 365081 354126 287769 18847 159843 439439 472598 266529 209455 153471 162068 232574 113351 30464 210981 92825 30916 22707 141818 30364 25...

result:

ok good job!

Test #9:

score: 0
Accepted
time: 717ms
memory: 132040kb

input:

500000 66403 66402 100000
297237
432967
138046
88503
315699
372893
55309
335404
127581
165919
247543
254268
285147
289728
275281
44427
94393
302830
489861
429097
425153
11083
439096
414157
386411
152968
394984
46119
149177
369378
413029
198215
134317
366218
281170
465540
39702
367778
247925
64320
86...

output:

294428 473786 485825 431592 164281 145981 86316 174346 80301 113618 487665 195552 414429 388882 204209 80452 122276 419753 180024 193433 229168 88715 50776 213156 141400 324806 395048 381700 288273 304234 33047 458744 184179 366547 154339 335938 453780 354088 190817 228996 422056 344516 120896 33776...

result:

ok good job!

Test #10:

score: 0
Accepted
time: 748ms
memory: 132728kb

input:

500000 82328 82327 100000
280281
366446
183709
14447
442815
440473
121531
103568
472324
479656
337467
424742
474404
340302
269686
457628
230012
484228
422877
10759
156759
66102
130428
307888
123685
460634
235321
98667
93133
489886
479420
34961
352500
322001
129001
121871
135775
235639
100221
221760
...

output:

185494 481099 499156 453960 393401 420707 490583 300635 457841 400593 362860 43697 439965 396991 259790 76224 209515 171230 384576 55737 330347 157797 329818 259497 162271 198217 140016 472820 346759 384195 48170 465258 391317 424786 181091 362106 81702 55087 46106 446240 429146 381049 446694 111378...

result:

ok good job!

Test #11:

score: 0
Accepted
time: 717ms
memory: 132068kb

input:

500000 53948 53947 100000
287984
258934
272973
481182
131565
217198
34714
463056
337977
495727
310042
26372
320480
231799
249741
340990
365501
267377
460708
248843
285777
172137
492784
201463
213559
259528
461602
235849
398717
25475
241699
451061
188952
251790
83551
169967
335575
209367
55705
6381
2...

output:

490646 472144 368896 436727 185550 365962 268698 368492 3377 345731 3530 464046 140761 273689 201619 469799 394344 235126 314427 90759 257401 495673 56059 218201 415591 470103 255673 178092 331005 207934 77614 238596 214701 232320 472004 432899 63486 380625 274954 193159 466963 373914 221349 40706 2...

result:

ok good job!

Test #12:

score: 0
Accepted
time: 746ms
memory: 132812kb

input:

500000 77935 77934 100000
38748
422564
39441
105430
38474
225464
237519
121832
72613
477531
321661
29181
307418
314049
120252
261006
88761
17726
492112
460837
55199
354114
417097
133271
231933
436973
110894
478550
291976
50101
38774
316091
306160
121826
315769
361823
82990
188508
124574
13093
235123...

output:

423149 497074 411928 166528 27377 492052 442541 286098 257719 348936 496628 65473 327811 270751 427588 253567 209339 182074 3507 347804 482508 464097 368679 379219 262140 457347 414902 413100 249692 107916 365412 166083 109652 355124 313876 225009 139337 337423 39090 139799 20308 101393 120141 45641...

result:

ok good job!

Subtask #2:

score: 0
Wrong Answer

Test #13:

score: 0
Wrong Answer
time: 1172ms
memory: 138604kb

input:

500000 88721 177440 100000
30974
23891
211201
125199
180489
387190
218020
498838
230147
307989
484136
257785
353027
304420
311738
169842
334090
486070
126212
328609
174959
368840
238722
418092
488389
226349
427271
457322
332454
12958
197530
264474
355717
482774
221286
282148
216441
266659
213750
628...

output:

63742 263216 146169 50728 199716 469441 459156 322328 152164 66876 274063 180006 237497 208598 249207 359435 96669 110070 41714 147909 214779 59127 151892 216797 194356 199621 20899 418742 198323 158340 163745 123748 85656 172672 123919 47108 313725 12227 183377 183933 348552 102798 184923 290145 17...

result:

wrong answer wrong answer on the second integer of query #1: read 592617861 but expected 52989851

Subtask #3:

score: 0
Wrong Answer

Test #25:

score: 0
Wrong Answer
time: 773ms
memory: 125644kb

input:

500000 200 199 40000
76296
130139
291501
292412
139543
433345
372726
451574
18315
465578
324564
477223
237354
81532
65170
465332
342130
9670
193303
193680
129668
149532
268907
89969
398275
356210
324593
433492
482232
466692
135343
433758
102545
287283
432859
351864
305769
489532
101532
450535
295762...

output:

464387 27779 146694 443858 405500 46371 375328 183696 253669 95388 173896 183797 18073 431275 140576 468877 345574 227090 361228 17134 261985 60381 64649 124883 275006 345205 205047 166559 173438 437370 498046 158980 365732 106698 145138 342120 407307 83109 296453 316074 219468 97176 251586 177490 2...

result:

wrong answer wrong answer on the second integer of query #1: read -58187 but expected 47544

Subtask #4:

score: 0
Wrong Answer

Test #37:

score: 0
Wrong Answer
time: 2101ms
memory: 251000kb

input:

1000000 1000 999 100000
678746
439069
32542
85937
936926
284219
461661
203235
533462
940676
230275
621140
780674
254931
562355
229273
201341
493976
358955
963527
880412
91220
474599
160086
698841
591551
718276
844558
39859
765917
34722
401724
219774
443004
682244
545401
968419
968020
354030
411187
1...

output:

927453 237540 859419 982835 971518 506285 771618 939329 16802 700671 845162 359776 499849 958003 722555 893539 667107 399090 361260 56054 518738 929831 330952 261064 845434 378738 416383 813166 332967 155083 279300 603715 217430 73563 278581 71462 840056 191244 422478 38987 402361 21178 733103 92045...

result:

wrong answer wrong answer on the second integer of query #1: read 7356480 but expected 628652

Subtask #5:

score: 0
Wrong Answer

Test #49:

score: 0
Wrong Answer
time: 2402ms
memory: 264996kb

input:

1000000 91074 91073 100000
844855
360256
604500
520288
3402
603913
199722
732526
574997
429775
182518
190073
386932
693624
254661
333433
557929
350362
247817
201441
960948
519977
461212
493412
852908
455639
732827
432452
320916
223796
413293
969300
617038
438432
2369
51283
908991
374139
410798
19612...

output:

100266 524911 805244 861271 648132 338218 588017 846372 361674 257564 857806 809152 146144 655284 305021 895380 787314 337733 840423 783219 849918 564122 924389 456471 56411 922287 626528 573150 955857 663398 713622 677964 973127 106150 529384 890628 839820 734754 971452 312629 105748 707190 993032 ...

result:

wrong answer wrong answer on the second integer of query #1: read -1006994395 but expected 17552616