Submission details
Task:Company Queries II
Sender:usvafe
Submission time:2025-10-20 12:56:20 +0300
Language:C++ (C++17)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.01 sdetails
#2ACCEPTED0.01 sdetails
#3ACCEPTED0.01 sdetails
#4ACCEPTED0.01 sdetails
#5ACCEPTED0.01 sdetails
#6--details
#7--details
#8--details
#9--details
#10--details
#11ACCEPTED0.01 sdetails
#12--details

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:38:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |         while (nn <= sn.size()) {
      |                ~~~^~~~~~~~~~~~
input/code.cpp:40:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |                 for (int i=0; i<sn.size()-nn; i++) {
      |                               ~^~~~~~~~~~~~~

Code

#include <bits/stdc++.h>
using namespace std;

int main() {
	vector<int> c[(int)2e5+1];
	int par[(int)2e5+1];
	par[1] = 0;
	int n, q;
	cin >> n >> q;
	for (int i=2; i<=n; i++) {
		int b;
		cin >> b;
		par[i] = b;
		c[b].push_back(i);
	}
	vector<bool> vis((int)2e5+1);
	stack<int> s;
	vector<int> sn;
	vector<int> depth((int)2e5+1);
	vector<int> fo((int)2e5+1);
	s.push(1);
	while (!s.empty()) {
		int b = s.top(); s.pop();
		if (par[b]) {
			depth[b] = depth[par[b]] + 1;
		}
		fo[b] = sn.size();
		sn.push_back(b);
		if (vis[b]) continue;
		vis[b] = true;
		for (int ch : c[b]) {
			s.push(b);
			s.push(ch);
		}
	}
	map<pair<int,int>,pair<int, int>> mrq;
	int nn = 1;
	while (nn <= sn.size()) {
		nn -= 1;
		for (int i=0; i<sn.size()-nn; i++) {
			if (nn == 0) {
				mrq[{i,i}] = {depth[sn[i]], sn[i]};
				continue;
			}
			mrq[{i, i+nn}] = min(mrq[{i, i+nn/2}], mrq[{i+nn-nn/2, i+nn}]);
		}
		nn = (nn+1) * 2;
	}

	for (int i=0; i<q; i++) {
		int a, b;
		cin >> a >> b;
		a = fo[a];
		b = fo[b];
		int mn = min(a, b);
		int mx = max(a, b);
		int nn = 1;
		for (; nn*2 < (mx-mn)+1; nn*=2);
		int out = min(mrq[{mn, mn+nn-1}], mrq[{mx-nn+1, mx}]).second;
		cout << out << endl;
	}

	return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
10 10
1 2 3 4 5 6 7 8 9
6 9
8 10
10 3
...

correct output
6
8
3
1
8
...

user output
6
8
3
1
8
...

Test 2

Verdict: ACCEPTED

input
10 10
1 1 1 1 1 1 1 1 1
1 7
3 4
4 1
...

correct output
1
1
1
1
1
...

user output
1
1
1
1
1
...

Test 3

Verdict: ACCEPTED

input
10 10
1 1 1 1 2 3 4 4 1
1 8
2 7
8 3
...

correct output
1
1
1
1
1
...

user output
1
1
1
1
1
...

Test 4

Verdict: ACCEPTED

input
10 10
1 1 3 1 2 2 5 3 9
7 2
7 6
3 9
...

correct output
2
2
3
1
1
...

user output
2
2
3
1
1
...

Test 5

Verdict: ACCEPTED

input
10 10
1 2 3 2 5 3 2 2 4
6 1
1 3
1 9
...

correct output
1
1
1
2
2
...

user output
1
1
1
2
2
...

Test 6

Verdict:

input
200000 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
74862
8750
16237
72298
58111
...

user output
(empty)

Test 7

Verdict:

input
200000 200000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
1
1
1
1
1
...

user output
(empty)

Test 8

Verdict:

input
200000 200000
1 2 1 2 3 2 1 6 3 1 10 12 13 4...

correct output
1
2
2
2
1
...

user output
(empty)

Test 9

Verdict:

input
200000 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
2796
633
633
151
2690
...

user output
(empty)

Test 10

Verdict:

input
200000 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
365
73
103
365
216
...

user output
(empty)

Test 11

Verdict: ACCEPTED

input
2 4
1
1 1
1 2
2 1
...

correct output
1
1
1
2

user output
1
1
1
2

Test 12

Verdict:

input
200000 200000
1 1 2 3 4 5 6 7 8 9 10 11 12 1...

correct output
27468
6353
27468
6353
6353
...

user output
(empty)