CSES - Aalto Competitive Programming 2024 - wk7 Homework - Results
Submission details
Task:Company Queries II
Sender:hungdojan
Submission time:2024-10-19 13:04:55 +0300
Language:C++ (C++17)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.57 sdetails
#7ACCEPTED0.53 sdetails
#8ACCEPTED0.61 sdetails
#9ACCEPTED0.57 sdetails
#10ACCEPTED0.57 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.58 sdetails

Code

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

#define I_2D(row, col, width) ((row) * (width) + (col))
#define PRINT_ARR(arr, n)                                                      \
  do {                                                                         \
    for (int i = 0; i < n; i++) {                                              \
      cout << arr[i] << " ";                                                   \
    }                                                                          \
    cout << "\n";                                                              \
  } while (0)
#define PRINT_VEC_ARR(v, n)                                                    \
  do {                                                                         \
    for (int i = 0; i < n; i++) {                                              \
      cout << i << ": ";                                                       \
      for (auto item : v[i]) {                                                 \
        cout << item << " ";                                                   \
      }                                                                        \
      cout << endl;                                                            \
    }                                                                          \
  } while (0)
#define min_pp(a, b) (a.second < b.second ? a : b)

typedef long long ll;
typedef pair<ll, ll> pp;
#define LOG2 20
#define MAXN (int)1e6
pp min_acc[LOG2][MAXN];

void traverse(ll *i, ll id, ll d, vector<ll> *edges, unordered_map<ll, ll> &index) {
  min_acc[0][*i] = {id, d};
  index[id] = *i;

  if (!edges[id].empty()) {
    for (auto n : edges[id]) {
      (*i)++;
      traverse(i, n, d + 1, edges, index);
      (*i)++;
      min_acc[0][*i] = {id, d};
    }
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, m;
  cin >> n >> m;

  ll parent[n];
  vector<ll> edges[n];
  int size = 2 * n - 1;
  unordered_map<ll, ll> index;

  for (int i = 1; i < n; i++) {
    cin >> parent[i];
    edges[--parent[i]].push_back(i);
  }


  ll i = 0;
  traverse(&i, 0, 1, edges, index);
  for (int i = 1; i < LOG2; i++) {
    for (int j = 0; j + (1 << i) <= size; j++) {
      min_acc[i][j] =
          min_pp(min_acc[i - 1][j], min_acc[i - 1][j + (1 << (i - 1))]);
    }
  }

  int a, b, A, B,k;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--, b--;
    A = index[a];
    B = index[b];
    if (A > B) {
      int tmp = A;
      A = B;
      B = tmp;
    }
    k = (int)log2(B - A + 1);
    cout << min_pp(min_acc[k][A], min_acc[k][B - (1 << k) + 1]).first + 1
         << 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: 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
...