Submission details
Task:Company Queries II
Sender:Aurelien
Submission time:2025-10-17 19:08:16 +0300
Language:C++ (C++17)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#60.43 sdetails
#70.40 sdetails
#80.43 sdetails
#90.43 sdetails
#100.43 sdetails
#11ACCEPTED0.00 sdetails
#120.43 sdetails

Code

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

typedef long long ll;


int main() {
    ll n,q;
    cin >> n >> q;

    ll a,b;
    ll array[n+1];
    ll adj[n+1];
    for(ll i = 0; i<n-1; i++) {
        cin >> a;
        array[i+2] = array[a]+1;
        adj[i+2] = a;
    }
    adj[1] = 1;

    ll ancestor[n+1][n] = {0};

    for(ll x = 1; x<=n; x++) {
        ll k = 1;
        while(k<n) {
            if(k == 1) {
                ancestor[x][k] = adj[x];
            } else {
                ancestor[x][k] = ancestor[ancestor[x][k/2]][k/2];
            }
            k *= 2;
        }
    }


    ll outputs[q];
    ll ances_pairs[n+1][n+1] = {0};
    for(ll i = 0; i<q; i++) {
        cin >> a >> b;
        ll k = 0;
        if(array[a] != array[b]) {
            if(array[a] < array[b]) swap(a,b);
            k = array[a] - array[b];
        }

        vector<ll> bin;
        while(k > 0) {
            bin.push_back(k % 2);
            k = k / 2;
        }

        for(long unsigned int j = 0; j<bin.size(); j++) {
            if(bin[j] != 0) {
                a = ancestor[a][(ll)pow(2,j)];
            }
            
        }


        ll p_a = a;
        ll p_b = b;
        while(a != b) {
            if(ances_pairs[a][b] != 0) {
                //cout << "pair";
                b = ances_pairs[a][b];
                break;
            }
            a = ancestor[a][1];
            b = ancestor[b][1];
        }
        outputs[i] = b;
        ances_pairs[p_a][p_b] = b;
        
    }

    for(ll i = 0; i<q; i++) {
        cout << outputs[i] << endl;
    }

}

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)