QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#447779#4401. Prizezhaohaikun0 0ms0kbC++204.0kb2024-06-18 19:53:412024-06-18 19:53:42

Judging History

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

  • [2024-06-18 19:53:42]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2024-06-18 19:53:41]
  • 提交

answer

// MagicDark
#include <bits/stdc++.h>
#define debug cerr << "\033[32m[" << __LINE__ << "]\033[0m "
#define SZ(x) ((int) x.size() - 1)
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define DF(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> T& chkmax(T& x, T y) {return x = max(x, y);}
template <typename T> T& chkmin(T& x, T y) {return x = min(x, y);}
template <typename T> T& read(T &x) {
	x = 0; int f = 1; char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = - f;
	for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
	return x *= f;
}
const int N = 1e6 + 10;
int n, k, q, t;
struct Tree {
	int fa[N], rt, dfn[N], nfd[N], dfscnt, sz[N], son[N], dep[N], top[N], dis[N];
	vector <int> v[N];
	void dfs(int x) {
		dep[x] = dep[fa[x]] + 1;
		sz[x] = 1;
		for (int i: v[x]) {
			dfs(i);
			sz[x] += sz[i];
			if (sz[i] > sz[son[x]]) son[x] = i;
		}
		if (son[x]) v[x].erase(find(all(v[x]), son[x])), v[x].insert(v[x].begin(), son[x]);
	}
	void dfs2(int x, int tt) {
		top[x] = tt;
		nfd[dfn[x] = ++dfscnt] = x;
		for (int i: v[x]) dfs2(i, i == v[x].front() ? tt : i);
	}
	void init() {
		F(i, 1, n) {
			cin >> fa[i];
			if (!~fa[i]) fa[i] = 0, rt = i;
			else v[fa[i]].push_back(i);
		}
		dfs(rt), dfs2(rt, rt);
	}
	int lca(int x, int y) {
		while (top[x] != top[y]) {
			if (dep[top[x]] > dep[top[y]]) swap(x, y);
			y = fa[top[y]];
		}
		return dep[x] < dep[y] ? x : y;
	}
	int query(int x, int y) {
		return dis[x] - dis[lca(x, y)] * 2 + dis[y];
	}
} T1, T2;
int val[N], an[N];
vector <pair <int, int>> v[N], vv[N];
void dfs(int x, int g) {
	T2.dis[x] = g;
	// debug << x << " " << T2.dis[x] << endl;
	for (auto [i, j]: vv[x]) dfs(i, g + j);
}
signed main() {
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

	cin >> n >> k >> q >> t;
	T1.init(), T2.init();
	vector <int> g;
	F(i, 1, k) g.push_back(T1.nfd[i]);//, debug << T1.nfd[i] << endl;
	// debug << T1.rt << endl;
	sort(all(g), [&] (int x, int y) {
		return T2.dfn[x] < T2.dfn[y];
	});
	for (int i: g) cout << i << " "; cout << endl;
	F(i, 0, SZ(g) - 1) cout << "? " << g[i] << " " << g[i + 1] << '\n';
	cout << "!" << endl;
	// set <int> s;
	stack <int> s;
	s.push(g[0]);
	F(i, 0, SZ(g) - 1) {
		int a, b, c, d; cin >> a >> b >> c >> d;
		int k1 = T1.lca(g[i], g[i + 1]);
		int k2 = T2.lca(g[i], g[i + 1]);
		// debug << k1 << " " << k2 << endl;
		v[k1].emplace_back(g[i], a);
		v[g[i]].emplace_back(k1, - a);
		v[k1].emplace_back(g[i + 1], b);
		v[g[i + 1]].emplace_back(k1, - b);
		// if (k2 == )
		an[g[i + 1]] = k2;
		val[g[i + 1]] = d;
		int lst = 0;
		while (s.size() && T2.dep[s.top()] > T2.dep[k2]) c -= val[lst], lst = s.top(), s.pop();
		if (s.empty() || s.top() != k2) {
			assert(lst);
			if (s.size()) {
				an[k2] = s.top();
				val[k2] = val[lst] - c;
			}
			an[lst] = k2;
			val[lst] = c;
			s.emplace(k2);
		}
		s.push(g[i + 1]);
		// s[g[i + 1]] = d;
		// an[g[i + 1]] = k2;
		// if (!s[g[i]]) {
		// 	s[g[i]] = d;
		// 	an[g[i]] = d;
		// } else {
		// 	if (T2.dep[an[g[i]]] < T2.dep[g[i]]) {
		// 	}
		// }
	}
	queue <int> q;
	// int rt = T1.nfd[1];
	T1.dis[T1.rt] = 1;
	q.push(T1.rt);
	while (q.size()) {
		int x = q.front(); q.pop();
		// debug << x << " " << T1.dis[x] << endl;
		for (auto [i, j]: v[x])
			if (!T1.dis[i]) {
				// debug << i << " " << x << " " << j << endl;
				T1.dis[i] = T1.dis[x] + j;
				q.push(i);
			}
	}
	// for (int i: g) cout << T1.dis[i] << " "; debug << endl;
	while (s.size() > 1) s.pop();
	F(i, 1, n)
		if (an[i]) vv[an[i]].emplace_back(i, val[i]);
	dfs(s.top(), 0);
	F(i, 1, t) {
		int p, q; cin >> p >> q;
		// int k1 = T1.lca(p, q), k2 = T2.lca(p, q);
		// cout << T1.query(p, k1) << " " << T1.query(q, k1) << " " << T2.query(p, k2) << " " << T2.query()
		cout << T1.query(p, q) << " " << T2.query(p, q) << '\n';
	}
	cout << endl;
	return 0;
}
/* why?
*/

詳細信息

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:

299348 225578 286701 388703 273711 466172 478011 490391 462013 126494 92677 182472 13812 107732 303666 361862 256289 91025 389690 156797 268792 434419 208299 409874 319842 64913 385537 136511 498213 255392 208598 45196 97386 482069 290480 370649 225780 380585 84550 485237 301855 494683 414740 107270...

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:

12225 329473 124294 112780 478338 445039 249189 32330 65783 179054 497476 452979 319006 30813 48206 427935 466790 486377 109196 200837 164218 45188 487722 282259 229713 367076 188057 187010 232559 151913 348461 116954 20242 322713 185020 157495 443679 326708 325415 391214 266949 457474 3735 299220 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 737189 653885 840772 346403 780854 103601 49131 439139 486132 820231 177271 826206 982104 499097 409243 194435 293457 172618 662161 236859 473531 81188 533335 712368 462084 777243 239386 911529 829354 62098 492333 390487 523069 358162 163042 451543 653539 717744 885154 584533 11086 661366 952...

result:


Subtask #5:

score: 0
Skipped

Dependency #4:

0%