Task: | Company Queries II |
Sender: | HFalke |
Submission time: | 2024-10-24 01:03:37 +0300 |
Language: | C++ (C++17) |
Status: | READY |
Result: | ACCEPTED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.00 s | details |
#2 | ACCEPTED | 0.00 s | details |
#3 | ACCEPTED | 0.00 s | details |
#4 | ACCEPTED | 0.00 s | details |
#5 | ACCEPTED | 0.00 s | details |
#6 | ACCEPTED | 0.30 s | details |
#7 | ACCEPTED | 0.15 s | details |
#8 | ACCEPTED | 0.22 s | details |
#9 | ACCEPTED | 0.35 s | details |
#10 | ACCEPTED | 0.30 s | details |
#11 | ACCEPTED | 0.00 s | details |
#12 | ACCEPTED | 0.41 s | details |
Code
#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = a; i < b; i++) #define PB push_back #define MP make_pair #define F first #define S second typedef long long ll; typedef vector<int> vi; typedef vector<long long> vl; typedef pair<int,int> pi; typedef pair<long long, long long> pl; const long long lmx = LLONG_MAX; const int imx = INT_MAX; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; int levels = 0; while (pow(2, levels) < n) levels++; vector<vector<int>> anc(n, vector<int>(levels+1, 0)); vector<int> boss(n); vector<int> level(n); anc[0][0] = 1; boss[0] = 1; level[0] = 0; REP(i, 1, n) { cin >> boss[i]; level[i] = level[boss[i] - 1] + 1; anc[i][0] = boss[i]; } REP(i, 1, levels + 1) { REP(j, 1, n) { if (anc[j][i - 1] != 0 && anc[anc[j][i - 1] - 1][i - 1] != 0) { anc[j][i] = anc[anc[j][i - 1] - 1][i - 1]; } else { anc[j][i] = 0; } } } vector<pi> queries(q); REP(i, 0, q) { cin >> queries[i].F >> queries[i].S; } vector<int> ans(q); REP(i, 0, q) { int emp1 = queries[i].F, emp2 = queries[i].S; int lvl1 = level[emp1 - 1], lvl2 = level[emp2 - 1]; int dif = abs(lvl1 - lvl2); if (lvl1 < lvl2) { for (int j = levels; j >= 0; j--) { if (dif >= (1 << j)) { emp2 = anc[emp2 - 1][j]; dif -= (1 << j); } } } else if (lvl2 < lvl1) { for (int j = levels; j >= 0; j--) { if (dif >= (1 << j)) { emp1 = anc[emp1 - 1][j]; dif -= (1 << j); } } } if (emp1 == emp2) { ans[i] = emp1; continue; } for (int j = levels; j >= 0; j--) { if (anc[emp1 - 1][j] != 0 && anc[emp1 - 1][j] != anc[emp2 - 1][j]) { emp1 = anc[emp1 - 1][j]; emp2 = anc[emp2 - 1][j]; } } ans[i] = anc[emp1 - 1][0]; } REP(i, 0, q) cout << ans[i] << "\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 ... |