Task: | Company Queries II |
Sender: | esya_rae |
Submission time: | 2024-10-23 23:18:21 +0300 |
Language: | C++ (C++11) |
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.60 s | details |
#7 | ACCEPTED | 0.56 s | details |
#8 | ACCEPTED | 0.64 s | details |
#9 | ACCEPTED | 0.60 s | details |
#10 | ACCEPTED | 0.61 s | details |
#11 | ACCEPTED | 0.00 s | details |
#12 | ACCEPTED | 0.61 s | details |
Code
#include <iostream> #include <cmath> #include <vector> #include <tuple> using namespace std; // #define int long long int #include <limits> const int INF = std::numeric_limits<int>::max(); vector<int> ni, ds, r; vector<vector<int>> g; vector<pair<int, int>> min_tree; pair<int, int> res; int n, m, k; void dfs(int v, int d) { ni.push_back(v); ds.push_back(d); if (r[v] == -1) { r[v] = ni.size() - 1; } for (int w: g[v]) { dfs(w, d + 1); ni.push_back(v); ds.push_back(d); } } void build_tree_min() { for (int i = 0; i < m; i++) { min_tree[k + i] = {ds[i], i}; } for (int i = k - 1; i > 0; i--) { min_tree[i] = min(min_tree[2 * i], min_tree[2 * i + 1]); } } pair<int, int> answer_min(int a, int b) { res = {INF, -1}; a += k; b += k; while (a <= b) { if (a % 2 == 1) { res = min(res, min_tree[a]); a++; } if (b % 2 == 0) { res = min(res, min_tree[b]); b--; } a /= 2; b /= 2; } return res; } int main() { int q; cin >> n >> q; g.resize(n); r.resize(n, -1); int v; for (int i = 0; i < n - 1; ++i){ cin >> v; g[v - 1].push_back(i + 1); } dfs(0, 0); m = ni.size(); k = pow(2, ceil(log2(m))); min_tree.resize(2 * k, {INF, -1}); build_tree_min(); int a, b; for (int i = 0; i < q; i++) { cin >> a >> b; a--; b--; res = answer_min(min(r[a], r[b]), max(r[a], r[b])); cout << ni[res.second] + 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 ... Truncated |
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 ... Truncated |
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 ... Truncated |
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 ... Truncated |
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 ... Truncated |
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 ... Truncated |