Task: | Company Queries II |
Sender: | snude |
Submission time: | 2024-10-20 13:34:26 +0300 |
Language: | C++ (C++11) |
Status: | READY |
Result: | RUNTIME ERROR |
test | verdict | time | |
---|---|---|---|
#1 | RUNTIME ERROR | 0.00 s | details |
#2 | ACCEPTED | 0.00 s | details |
#3 | ACCEPTED | 0.00 s | details |
#4 | WRONG ANSWER | 0.00 s | details |
#5 | WRONG ANSWER | 0.00 s | details |
#6 | RUNTIME ERROR | 0.11 s | details |
#7 | ACCEPTED | 0.56 s | details |
#8 | WRONG ANSWER | 0.64 s | details |
#9 | WRONG ANSWER | 0.88 s | details |
#10 | WRONG ANSWER | 0.87 s | details |
#11 | ACCEPTED | 0.00 s | details |
#12 | WRONG ANSWER | 0.74 s | details |
Code
#include <algorithm>#include <cmath>#include <cstdio>#include <cstdlib>#include <iostream>#include <vector>#include <bits/stdc++.h>using namespace std;using ll = long long;// Debug printing#ifdef DEBUG#define deb(fmt, args...) printf("DEBUG: %d: " fmt, __LINE__, ##args)#else#define deb(fmt, args...)#endifvoid print_array(vector<int> in, const string title = "Vector"){cout << title << " [\n";for (const auto &el : in) {cout << el << " ";}cout << "\n] END\n";}void print_matrix(vector<vector<int> > in, const string title = "Matrix"){cout << title << " [\n";for (unsigned int i = 0; i < in.size(); i++) {cout << "row " << i << ": ";for (unsigned int j = 0; j < in[i].size(); j++) {cout << in[i][j] << " ";}cout << "\n";}cout << "] END\n";}void dfs(int s, vector<bool> &visited, vector<vector<int> > &adj, int depth,vector<int> &depths){if (visited[s]) {return;};visited[s] = true;depths[s] = depth;// traversal.push_back({ s, depth });for (auto u : adj[s]) {dfs(u, visited, adj, depth + 1, depths);// traversal.push_back({ s, depth });}}int bit_ceil(int n) {int res = 1;while (res < n) res <<= 1;return res >>= 1;}int get_ancestor(int in, int d, vector<vector<int> > &ancestors){int depth = d;int next = in;int row = 0;// while (depth > 0) {// row = bit_ceil(d);// next = ancestors[row][next];// depth -= row;// }// return next;while (depth > 0) {if (depth % 2 == 1)next = ancestors[row][next- 1];row++;depth >>= 1;}return next;}int main(int argc, char *argv[]){// Read the input parametersint n, q;cin >> n >> q;// Read values from one linevector<int> boss(n + 1);vector<vector<int> > kids(n + 1, vector<int>());vector<vector<int> > ancestor(log2(n), vector<int>(n + 1));int tmp;for (int i = 1; i < n; i++) {cin >> tmp;boss[i] = tmp;ancestor[0][i] = tmp;kids[tmp].push_back(i + 1);}boss[1] = 1;boss[0] = 1;ancestor[0][1] = 1;ancestor[0][0] = 1;// print_matrix(kids);vector<bool> visited(n, false);vector<int> traversal;vector<int> depths(n + 1);dfs(1, visited, kids, 0, depths);for (unsigned int row = 1; row < ancestor.size(); row++) {for (int j = 1; j < n + 1; j++) {ancestor[row][j] =ancestor[row - 1][ancestor[row - 1][j]];}}// print_matrix(ancestor, "ancestor");// print_array(depths, "depths");int l, r;for (int i = 0; i < q; i++) {cin >> l >> r;// l--;// r--;// l = first[l];// r = first[r];deb("väli (%d, %d)\n", l, r);// increase nodes to same depthdeb("r: %d, r: %d, l: %d\n", r, depths[r], depths[l]);if (depths[l] > depths[r]) {l = get_ancestor(l, depths[l] - depths[r], ancestor);} else {// deb("r: %d, k: %d", r, dephts[r] - dephts[l]);deb("r: %d, k: %d", r, depths[r] - depths[l]);r = get_ancestor(r, depths[r] - depths[l], ancestor);}deb("r: %d, l: %d\n", r, l);// start traversing top downif (l != r) {for (int row = ancestor.size() - 1; row >= 0; row--) {// if ancestor is different, update positions to get closerdeb("l: %d, r: %d, row: %d\n", l, r, row);if (ancestor[row][l] != ancestor[row][r]) {deb("l: %d, r: %d\n", l, r);l = ancestor[row][l];r = ancestor[row][r];}}l = ancestor[0][l];}cout << l << "\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 |
---|
6 8 7 4 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: WRONG ANSWER
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 1 3 3 9 ... |
Test 5
Verdict: WRONG ANSWER
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 |
---|
2 1 1 2 5 ... |
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 |
---|
167529 8750 |
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: WRONG ANSWER
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 1 2 1 2 ... Truncated |
Test 9
Verdict: WRONG ANSWER
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 |
---|
33594 181357 4232 126194 78224 ... Truncated |
Test 10
Verdict: WRONG ANSWER
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 |
---|
72597 48543 1631 51757 50396 ... 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: WRONG ANSWER
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 1 1 1 1 ... Truncated |