Submission details
Task:Company Queries II
Sender:ind1f
Submission time:2025-10-09 19:45:59 +0300
Language:C++ (C++17)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.01 sdetails
#2ACCEPTED0.01 sdetails
#3ACCEPTED0.01 sdetails
#4ACCEPTED0.01 sdetails
#5ACCEPTED0.01 sdetails
#6ACCEPTED0.21 sdetails
#7ACCEPTED0.18 sdetails
#8ACCEPTED0.24 sdetails
#9ACCEPTED0.21 sdetails
#10ACCEPTED0.21 sdetails
#11ACCEPTED0.01 sdetails
#12ACCEPTED0.22 sdetails

Code

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;
const int L = 20;

int n, q;
vector<int> g[N];
int fst[N], et[2 * N];
int lv[N];
int lg2[2 * N];
pair<int, int> sp[L][2 * N];
int time_euler;

void dfs(int u) {
  fst[u] = ++time_euler;
  et[time_euler] = u;
  for (int v : g[u]) {
    lv[v] = lv[u] + 1;
    dfs(v);
    et[++time_euler] = u;
  }
}

void build_lca() {
  lg2[1] = 0;
  for (int i = 2; i < 2 * N; i++) {
    lg2[i] = lg2[i / 2] + 1;
  }
  for (int j = 0; j < L; j++) {
    for (int i = 1; i + (1 << j) - 1 <= time_euler; i++) {
      sp[j][i] = (j == 0 ? pair<int, int>{lv[et[i]], et[i]} : min(sp[j - 1][i], sp[j - 1][i + (1 << (j - 1))]));
    }
  }
}

int lca(int u, int v) {
  if (fst[u] > fst[v]) {
    swap(u, v);
  }
  int w = lg2[fst[v] - fst[u] + 1];
  return min(sp[w][fst[u]], sp[w][fst[v] - (1 << w) + 1]).second;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> q;
  for (int i = 2; i <= n; i++) {
    int e;
    cin >> e;
    g[e].emplace_back(i);
  }
  dfs(1);
  build_lca();
  while (q--) {
    int a, b;
    cin >> a >> b;
    cout << lca(a, b) << '\n';
  }
  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: ACCEPTED

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
74862
8750
16237
72298
58111
...

Test 7

Verdict: ACCEPTED

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
1
1
1
1
1
...

Test 8

Verdict: ACCEPTED

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
1
2
2
2
1
...

Test 9

Verdict: ACCEPTED

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
2796
633
633
151
2690
...

Test 10

Verdict: ACCEPTED

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
365
73
103
365
216
...

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: ACCEPTED

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
27468
6353
27468
6353
6353
...