This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#define ll long long
#define maxn 200005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
vector<int> adj[maxn];
int a[maxn], lef[maxn], rig[maxn], ord[maxn], dep[maxn], anc[18][maxn];
bool poss[maxn];
int c = 0;
void dfs(int n, int par, int d) {
anc[0][n] = par;
dep[n] = d;
lef[n] = c;
ord[c++] = n;
for (int v:adj[n]) {
if (v != par) {
dfs(v, n, d + 1);
}
}
rig[n] = c;
}
int lca(int a, int b) {
if (dep[a] < dep[b]) swap(a, b);
int hdif = dep[a] - dep[b], cnt = 0;
while (hdif) {
if (hdif & 1) a = anc[cnt][a];
cnt++, hdif >>= 1;
}
if (a == b) return a;
for (int i = 17;i >= 0;i--) {
if (anc[i][a] != anc[i][b]) {
a = anc[i][a], b = anc[i][b];
}
}
return anc[0][a];
}
int main() {
io
int n, m, q;
cin >> n >> m >> q;
for (int i = 0;i < n - 1;i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 0, 0);
for (int i = 1;i < 18;i++) {
for (int j = 1;j <= n;j++) anc[i][j] = anc[i - 1][anc[i - 1][j]];
}
for (int i = 1;i <= m;i++) {
cin >> a[i];
a[i] = lef[a[i]];
}
for (int i = 0;i < q;i++) {
int type;
cin >> type;
if (type == 1) {
int pos, v;
cin >> pos >> v;
a[pos] = lef[v];
} else {
int l, r, v;
cin >> l >> r >> v;
for (int j = l;j <= r;j++) {
poss[j] = (lef[v] <= a[j] && a[j] < rig[v]);
}
int ax = -1, ay = 1;
for (int j = l;j < r;j++) {
if (ord[a[j]] == v) {
ax = j, ay = j;
break;
}
if (poss[j] && poss[j + 1]) {
if (lca(ord[a[j]], ord[a[j + 1]]) == v) {
ax = j, ay = j + 1;
break;
}
}
}
cout << ax << " " << ay << endl;
}
}
}
/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5
2 3 4 5
2 1 3 1
*/
# | 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... |