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;
const int mxN = 2e5, LOG = 18;
vector<int> g[mxN];
int up[LOG][mxN], dep[mxN], _m;
void dfs(int s, int e) {
for (auto u : g[s]) {
if (u == e) continue;
up[0][u] = s;
dep[u] = dep[s] + 1;
dfs(u, s);
}
}
void jump(int &s, int k) {
for (int i = 0; i < LOG; ++i) {
if ((1 << i) & k) {
s = up[i][s];
}
}
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
jump(u, dep[u] - dep[v]);
if (u == v) return u;
for (int i = LOG-1; i >= 0; --i) {
if (up[i][u] != up[i][v]) {
u = up[i][u], v = up[i][v];
}
}
return up[0][u];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q; _m = m-1;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
u--, v--;
g[u].push_back(v), g[v].push_back(u);
}
dfs(0, -1);
for (int i = 1; i < LOG; ++i) {
for (int j = 0; j < n; ++j) {
up[i][j] = up[i-1][up[i-1][j]];
}
}
int a[m];
multiset<int> ms[n], mms[n];
vector<int> val(m);
for (int i = 0; i < n; ++i) {
ms[i].insert(1e9);
mms[i].insert(1e9);
}
for (int i = 0; i < m; ++i) {
cin >> a[i];
a[i]--;
if (i) {
val[i-1] = lca(a[i-1], a[i]);
mms[val[i-1]].insert(i-1);
}
ms[a[i]].insert(i);
}
while (q--) {
int t;
cin >> t;
if (t == 1) {
int l, u;
cin >> l >> u;
l--, u--;
if (l != m-1) {
int x = lca(u, a[l+1]);
mms[val[l]].erase(l);
mms[x].insert(l);
val[l] = x;
}
if (l) {
int x = lca(u, a[l-1]);
mms[val[l-1]].erase(l-1);
mms[x].insert(l-1);
val[l-1] = x;
}
ms[a[l]].erase(ms[a[l]].find(l));
ms[u].insert(l);
a[l] = u;
}
if (t == 2) {
int l, r, v;
cin >> l >> r >> v;
l--, r--, v--;
if (*ms[v].lower_bound(l) <= r) {
cout << 1+*ms[v].lower_bound(l) << ' ' << 1+*ms[v].lower_bound(l) << '\n';
}
else if (*mms[v].lower_bound(l) <= r-1) {
cout << 1 + *mms[v].lower_bound(l) << ' ' << 2+*mms[v].lower_bound(l) << '\n';
}
else {
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... |