QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#73134#4401. Prize123456780 0ms0kbC++142.8kb2023-01-22 13:23:262023-01-22 13:23:27

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-22 13:23:27]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2023-01-22 13:23:26]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mp make_pair
#define inf 1e9
#define pii pair <int, int>
const int mod = 1e9 + 7;
inline int read () {
	int x = 0, f = 1;
	char ch = getchar ();
	while (ch < '0' || ch > '9') f = ((ch == '-') ? -1 : f), ch = getchar ();
	while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar ();
	return x * f;
}
inline void write (int x) {
	if (x < 0) x = -x, putchar ('-');
	if (x >= 10) write (x / 10);
	putchar (x % 10 + '0');
}
int n, k, Q, T;
struct Tree {
	int Fa[1000005], Rt;
	vector <int> G[1000005];
	int dep[1000005], siz[1000005], son[1000005], top[1000005];
	int dfn[1000005], seq[1000005], tot;
	void dfs(int x, int fa) {
		dfn[x] = ++tot, seq[tot] = x;
		dep[x] = dep[fa] + 1;
		siz[x] = 1;
		for(auto y : G[x]) {
			dfs(y, x);
			siz[x] += siz[y];
			if(!son[x] || siz[y] > siz[son[x]]) son[x] = y;
		}
	}
	void dfs2(int x) {
		int fa = Fa[x];
		if(son[fa] == x) top[x] = top[fa];
		else top[x] = x;
		if(son[x]) dfs2(son[x]);
		for(auto y : G[x]) if(y != son[x]) dfs2(y);
	}
	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]];
		}
		if(dep[x] > dep[y]) swap(x, y);
		return x;
	}
	void init() {
		for(int i = 1; i <= n; i++) {
			Fa[i] = read();
			if(Fa[i] == -1) Rt = i;
			else G[Fa[i]].push_back(i);
		}
		dfs(Rt, 0), dfs2(Rt);
	}
	vector <pii> V[1000005];
	void add(int x, int y) {
		int e = read();
		V[x].push_back(mp(y, e)); 
		V[y].push_back(mp(x, -e));
	}
	int vis[1000005], dis[1000005];
	void dfs3(int x, int e) {
		if(vis[x]) return ;
		vis[x] = 1, dis[x] = e;
		for(auto y : V[x]) dfs3(y.first, e + y.second);
	}
	int Dis(int x, int y) {
		int lca = LCA(x, y);
		return dis[x] + dis[y] - 2 * dis[lca];
	}
}T1, T2;
int a[1000005];
signed main () {
//	freopen (".in", "r", stdin);
//	freopen (".out", "w", stdout);
	n = read(), k = read(), Q = read(), T = read();
	T1.init(), T2.init();
	for(int i = 1; i <= k; i++) a[i] = T2.seq[i];
	sort(a + 1, a + 1 + k, [&] (int A, int B) {return T1.dfn[A] < T1.dfn[B];});
	
	for(int i = 1; i <= k; i++) write(a[i]), putchar(' ');
	putchar('\n'), fflush(stdout);
	
	for(int i = 1; i < k; i++) {
		write(a[i]), putchar(' '), write(a[i+1]), putchar('\n'), fflush(stdout);
		int lca1 = T1.LCA(a[i], a[i+1]), lca2 = T2.LCA(a[i], a[i+1]); 
		T1.add(lca1, a[i]), T1.add(lca1, a[i+1]), T2.add(lca2, a[i]), T2.add(lca2, a[i+1]);
	}
	puts("!"), fflush(stdout);
	
	for(int i = 1; i <= k; i++) T1.dfs3(a[i], 0);
	sort(a + 1, a + 1 + k, [&] (int A, int B) {return T2.dfn[A] < T2.dfn[B];});
	for(int i = 1; i <= k; i++) T2.dfs3(a[i], 0);
	
	while(T--) {
		int x = read(), y = read();
		write(T1.Dis(x, y)), putchar(' '), write(T2.Dis(x, y)), putchar('\n'), fflush(stdout);
	}
	return 0;
}
/*
*/

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Time Limit Exceeded

Test #1:

score: 0
Time Limit Exceeded

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:


Subtask #2:

score: 0
Time Limit Exceeded

Test #13:

score: 0
Time Limit Exceeded

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:


Subtask #3:

score: 0
Time Limit Exceeded

Test #25:

score: 0
Time Limit Exceeded

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:


Subtask #4:

score: 0
Time Limit Exceeded

Test #37:

score: 0
Time Limit Exceeded

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:


Subtask #5:

score: 0
Skipped

Dependency #4:

0%