| Task: | Company Queries II |
| Sender: | tjaa |
| Submission time: | 2025-10-22 13:17:36 +0300 |
| Language: | C++ (C++20) |
| Status: | READY |
| Result: | RUNTIME ERROR |
| test | verdict | time | |
|---|---|---|---|
| #1 | RUNTIME ERROR | 0.02 s | details |
| #2 | RUNTIME ERROR | 0.02 s | details |
| #3 | RUNTIME ERROR | 0.02 s | details |
| #4 | RUNTIME ERROR | 0.02 s | details |
| #5 | RUNTIME ERROR | 0.02 s | details |
| #6 | RUNTIME ERROR | 0.13 s | details |
| #7 | RUNTIME ERROR | 0.07 s | details |
| #8 | RUNTIME ERROR | 0.14 s | details |
| #9 | RUNTIME ERROR | 0.11 s | details |
| #10 | RUNTIME ERROR | 0.11 s | details |
| #11 | RUNTIME ERROR | 0.02 s | details |
| #12 | RUNTIME ERROR | 0.12 s | details |
Code
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+1;
int vis[N];
int first[N];
vector<vector<int>> adj(N);
const int eN = 2*N - 1;
int tot[eN];
int depth[eN];
void dfs(int n, int& i, int d=1) {
vis[n] = 1;
if(!first[n])
first[n] = i;
depth[i]=d;
tot[i++] = n;
for(auto e : adj[n]) {
if(!vis[e]) {
dfs(e, i, d+1);
depth[i]=d;
tot[i++]=n;
}
}
}
const int K = 20;
int st[K+1][eN];
void buildST(int* s, int len) {
copy(s, s+len, st[0]);
for (int i = 1; i <= K; i++)
for (int j = 0; j + (1<<i) <= N; j++) {
int a = st[i-1][j];
int b = st[i-1][j + (1<<(i-1))];
st[i][j] = depth[a] < depth[b] ? a : b;
}
}
int queryST(int l, int r) {
int i = bit_width(r-l+1ul) - 1;
int a = st[i][l];
int b = st[i][r-(1<<1)+1];
int mindx = depth[a] < depth[b] ? a : b;
return tot[mindx];
}
int main () {
int n, q;
cin >> n >> q;
for(int i = 2; i <= n; i++) {
int e; cin >> e;
adj[i].push_back(e);
adj[e].push_back(i);
}
int i = 0;
dfs(1, i);
buildST(tot, i);
while(q--) {
int a, b;
cin >> a >> b;
cout << queryST(first[a], first[b]) << '\n';
}
return 0;
}
Test details
Test 1
Verdict: RUNTIME ERROR
| 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 |
|---|
| 7 9 |
Test 2
Verdict: RUNTIME ERROR
| 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 |
Test 3
Verdict: RUNTIME ERROR
| 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 2 |
Test 4
Verdict: RUNTIME ERROR
| 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 |
|---|
| (empty) |
Test 5
Verdict: RUNTIME ERROR
| 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 |
|---|
| (empty) |
Test 6
Verdict: RUNTIME ERROR
| 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: RUNTIME ERROR
| 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 |
Test 8
Verdict: RUNTIME ERROR
| 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 |
Test 9
Verdict: RUNTIME ERROR
| 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: RUNTIME ERROR
| 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: RUNTIME ERROR
| input |
|---|
| 2 4 1 1 1 1 2 2 1 ... |
| correct output |
|---|
| 1 1 1 2 |
| user output |
|---|
| 1 1 |
Test 12
Verdict: RUNTIME ERROR
| 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 |
|---|
| 1 |
