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> d;
void dfs(int i, int p) {
for (int j : g[i]) if (j != p)
d[j] = d[i] + 1, pa[j][0] = i, dfs(j, i);
}
int lca(int i, int j) {
if (d[i] < d[j]) swap(i, j);
for (int ii = 19; ~ii; --ii) if (d[pa[i][ii]] >= d[j])
i = pa[i][ii];
for (int ii = 19; ~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>(20)), d.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);
}
pa[0][0] = d[0] = 0;
dfs(0, 0);
for (int i = 1; i < 20; ++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;
set<int> s[n], s1[n];
for (int i = 1; i < m; ++i) s[lca(a[i], a[i - 1])].insert(i - 1);
for (int i = 0; i < m; ++i) s1[a[i]].insert(i);
while (q--) {
int ty, x, y, z;
cin >> ty >> x >> y;
--x, --y;
if (ty == 1) {
if (x) s[lca(a[x], a[x - 1])].erase(x - 1);
if (x < m - 1) s[lca(a[x], a[x + 1])].erase(x);
s1[a[x]].erase(x);
a[x] = y;
if (x) s[lca(a[x], a[x - 1])].insert(x - 1);
if (x < m - 1) s[lca(a[x], a[x + 1])].insert(x);
s1[a[x]].insert(x);
}
else {
cin >> z;
--z;
auto it = s1[z].lower_bound(x);
if (it != s1[z].end() && *it <= y) {
cout << *it + 1 << ' ' << *it + 1 << '\n';
continue;
}
it = s[z].lower_bound(x);
if (it != s[z].lower_bound(y))
cout << "-1 -1\n";
else cout << *it + 1 << ' ' << *it + 2 << '\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... |