| Task: | Company Queries II |
| Sender: | aalto26gh_043 |
| Submission time: | 2026-10-15 20:52:41 +0300 |
| Language: | C++ (C++20) |
| 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.66 s | details |
| #7 | ACCEPTED | 0.60 s | details |
| #8 | ACCEPTED | 0.64 s | details |
| #9 | ACCEPTED | 0.66 s | details |
| #10 | ACCEPTED | 0.62 s | details |
| #11 | ACCEPTED | 0.00 s | details |
| #12 | ACCEPTED | 0.64 s | details |
Compiler report
input/code.cpp: In function 'std::vector<std::vector<int> > buildSparseTable(std::vector<int>&)':
input/code.cpp:26:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for (int i=0;i<arr.size();i++){
| ~^~~~~~~~~~~
input/code.cpp:31:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for (int l=2; l<=arr.size()-1; l*=2){
| ~^~~~~~~~~~~~~~
input/code.cpp:34:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for (int start=1; start+l-1<=arr.size()-1;start++){
| ~~~~~~~~~^~~~~~~~~~~~~~Code
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
void dfs(vector<vector<int>> & g, vector<int> & depth, vector<int>& tour, vector<int>& last, int node, int d){
depth.push_back(d);
tour.push_back(node);
for (int adj: g[node]){
dfs(g, depth, tour, last, adj, d+1);
tour.push_back(node);
depth.push_back(d);
}
last[node]=tour.size()-1;
}
vector<vector<int>> buildSparseTable(vector<int> & arr){
vector<vector<int>> sparse;
vector<int> firstLevel(arr.size());
for (int i=0;i<arr.size();i++){
firstLevel[i]=i;
}
sparse.push_back(firstLevel);
int level=1;
for (int l=2; l<=arr.size()-1; l*=2){
int half=l/2;
vector<int> currLevel(arr.size());
for (int start=1; start+l-1<=arr.size()-1;start++){
int lBest=sparse[level-1][start];
int rBest=sparse[level-1][start+half];
if (arr[lBest]<=arr[rBest]){
currLevel[start]=lBest;
} else {
currLevel[start]=rBest;
}
}
sparse.push_back(currLevel);
level++;
}
return sparse;
}
int queryMinIndex(vector<int> & arr, vector<vector<int>> & sparse, int l, int r){
int level=__lg(r-l+1);
int length=pow(2, level);
int lBest=sparse[level][l];
int rBest=sparse[level][r-length+1];
if (arr[lBest]<=arr[rBest]){
return lBest;
} else {
return rBest;
}
}
int main(){
int n, q;
cin >> n >> q;
vector<int> arr(n+1);
vector<vector<int>> g(n+1,vector<int>(0));
for (int i=2;i<=n;i++){
int a;
cin >> a;
g[a].push_back(i);
}
vector<int> depth(1);
vector<int> tour(1);
vector<int> last(n+1);
dfs(g, depth, tour, last, 1, 0);
auto sparse = buildSparseTable(depth);
for (int i=0;i<q;i++){
int l, r;
cin >> l >> r;
cout << tour[queryMinIndex(depth, sparse, min(last[l],last[r]), max(last[l],last[r]))] << '\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 ... |
