This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = int(5e5) + 1000;
const int K = 19;
vector<int> ch[N];
int jmp[K][N];
vector<int> st;
ll d[N], dis[N];
int k2;
void dfs(int u) {
if (k2 && u) {
st.push_back(u);
--k2;
}
for (int v : ch[u]) {
d[v] = d[u] + 1;
dfs(v);
}
}
void dfs2(int u) {
for (int v : ch[u]) {
dis[v] += dis[u];
dfs2(v);
}
}
void solve() {
int n, k, q, t;
cin >> n >> k >> q >> t;
for (int i = 0; i < 2; ++i) {
for (int j = 1; j <= n; ++j) {
int q;
cin >> q;
q += q < 0;
if (i == 0) {
ch[q].push_back(j);
jmp[0][j] = q;
}
}
}
k2 = k;
dfs(0);
for (int i : st) cout << i << ' ';
cout << '\n';
for (int i : st) if (jmp[0][i]) {
cout << "? " << i << ' ' << jmp[0][i] << endl;
int a[4];
for (int &j : a) cin >> j;
dis[i] = a[0];
}
dfs2(0);
for (int i = 1; i < K; ++i) {
for (int j = 0; j <= n; ++j) {
jmp[i][j] = jmp[i-1][jmp[i-1][j]];
}
}
cout << "!" << endl;
while (t--) {
int a, b;
cin >> a >> b;
ll base = dis[a] + dis[b];
if (a == b) {
cout << base - 2 * dis[a] << ' ' << base - 2 * dis[a] << endl;
continue;
}
if (d[a] < d[b]) swap(a, b);
for (int i = K-1; i >= 0; --i) {
if (d[jmp[i][a]] >= d[b]) a = jmp[i][a];
}
if (a == b) {
cout << base - 2 * dis[a] << ' ' << base - 2 * dis[a] << endl;
continue;
}
for (int i = K-1; i >= 0; --i) {
if (jmp[i][a] != jmp[i][b]) {
a = jmp[i][a];
b = jmp[i][b];
}
}
a = jmp[0][a];
cout << base - 2 * dis[a] << ' ' << base - 2 * dis[a] << endl;
}
}
int main() {
cin.tie(0) -> sync_with_stdio(0);
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |