이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define pii pair<int, int>
#define pll pair<lli, lli>
#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define test(x) cout << #x << ' ' << x << endl
#define printv(x) {\
for (auto a : x) cout << a << ' ';\
cout << endl;\
}
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
const int N = 200000, logN = 17;
vector <int> adj[N];
int in[N], out[N], jump[N][logN], _t;
void dfs(int v, int pa) {
in[v] = _t++;
jump[v][0] = pa;
for (int u : adj[v]) if (u != pa) {
dfs(u, v);
}
out[v] = _t++;
}
bool anc(int u, int v) {
return in[u] <= in[v] && out[u] >= out[v];
}
int lca(int u, int v) {
if (anc(u, v)) return u;
if (anc(v, u)) return v;
for (int i = logN - 1; ~i; --i) {
int k = jump[u][i];
if (~k && !anc(k, v)) u = k;
}
return jump[u][0];
}
int main () {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
for (int i = 0, u, v; i < n - 1; ++i) {
cin >> u >> v, --u, --v;
adj[u].pb(v);
adj[v].pb(u);
}
for (int i = 0; i < logN; ++i) for (int j = 0; j < n; ++j) jump[j][i] = -1;
dfs(0, -1);
for (int j = 1; j < logN; ++j) {
for (int i = 0; i < n; ++i) {
int k = jump[i][j - 1];
if (~k) jump[i][j] = jump[k][j - 1];
}
}
set <int> adjpair[n], vertex[n];
vector <int> a(m);
for (int i = 0; i < m; ++i) {
cin >> a[i], --a[i];
vertex[a[i]].insert(i);
if (i) adjpair[lca(a[i - 1], a[i])].insert(i - 1);
}
while (q--) {
int t, l, r, v;
cin >> t >> l >> r, --l;
if (t == 1) {
vertex[a[l]].erase(l);
if (l) adjpair[lca(a[l - 1], a[l])].erase(l - 1);
if (l + 1 < m) adjpair[lca(a[l], a[l + 1])].erase(l);
a[l] = --r;
vertex[a[l]].insert(l);
if (l) adjpair[lca(a[l - 1], a[l])].insert(l - 1);
if (l + 1 < m) adjpair[lca(a[l], a[l + 1])].insert(l);
} else {
cin >> v, --v;
pii ans = mp(-2, -2);
auto it = vertex[v].lower_bound(l);
if (it != vertex[v].end() && *it < r) {
ans = mp(*it, *it);
}
auto it2 = adjpair[v].lower_bound(l);
if (it2 != adjpair[v].end() && *it2 < r - 1) {
ans = mp(*it2, *it2 + 1);
}
cout << ans.X + 1 << ' ' << ans.Y + 1 << '\n';
}
}
}
/*
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... |