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>
#define all(i) i.begin(), i.end()
#define rall(i) i.rbegin(), i.rend()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
vector<vector<int>> g, pa;
vector<int> dep;
void dfs(int i, int p) {
for (int j : g[i]) if (j != p)
dep[j] = dep[i] + 1, pa[j][0] = i, dfs(j, i);
}
int lca(int i, int j) {
if (dep[i] < dep[j]) swap(i, j);
for (int ii = 11; ~ii; --ii) if (dep[pa[i][ii]] >= dep[j])
i = pa[i][ii];
for (int ii = 11; ~ii; --ii) if (pa[i][ii] != pa[j][ii])
i = pa[i][ii], j = pa[j][ii];
return i == j ? i : pa[i][0];
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
g.resize(n), pa.assign(n, vector<int>(12)), dep.resize(n);;
for (int i = 0; i < n - 1; ++i) {
int x, y;
cin >> x >> y;
--x, --y;
g[x].push_back(y), g[y].push_back(x);
}
dep[0] = 0, pa[0][0] = 0;
dfs(0, 0);
for (int i = 1; i < 12; ++i)
for (int j = 0; j < n; ++j)
pa[j][i] = pa[pa[j][i - 1]][i - 1];
int a[m];
for (int &i : a) cin >> i, --i;
while (q--) {
int ty, x, y, z;
cin >> ty >> x >> y;
if (ty == 1) {
a[x - 1] = y - 1;
continue;
}
cin >> z;
--x, --z;
int l = -1, now = -1;
bool fail = true;
for (int r = x; r < y; ++r) {
if (a[r] == z) {
cout << r + 1 << ' ' << r + 1 << '\n';
fail = false;
break;
}
if (now == -1) {
int tmp = lca(a[r], z);
if (tmp == z)
l = r, now = a[r];
} else {
int tmp = lca(now, a[r]);
if (tmp == z) {
cout << l + 1 << ' ' << r + 1 << '\n';
fail = false;
break;
}
if (lca(tmp, z) == z)
now = tmp;
else
l = now = -1;
}
}
if (fail) cout << "-1 -1\n";
}
}
# | 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... |