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;
#define int int64_t
#define ld long double
#define ar array
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7;
const int INF = 1e15+5;
const int MXN = 2e5+9;
vector<int> g[MXN];
int up[20][MXN], dep[MXN];
set<int> tnt[MXN], t[MXN];
void dfs(int u, int pu) {
up[0][u] = pu;
for (int i = 1; i < 20; i++)
up[i][u] = up[i-1][up[i-1][u]];
for (int v : g[u]) if (v != pu) {
dep[v] = dep[u] + 1; dfs(v, u);
}
}
int lca(int u, int v) {
if (dep[v] > dep[u]) swap(v, u);
for (int i = 19; i >= 0; --i)
if (((dep[u]-dep[v]) >> i)&1)
u = up[i][v];
if (u == v) return u;
for (int i = 19; i >= 0; --i)
if (up[i][u] != up[i][v])
u = up[i][u], v = up[i][v];
return up[0][u];
}
void solve() {
int N, M, Q; cin >> N >> M >> Q;
for (int i = 0; i < N-1; i++) {
int a, b; cin >> a >> b;
g[--a].push_back(--b);
g[b].push_back(a);
}
vector<int> A(M);
for (int &i : A) cin >> i, --i;
dfs(0,0);
for (int i = 0; i < M; i++) t[A[i]].insert(i);
for (int i = 0; i < M-1; i++) tnt[lca(A[i], A[i+1])].insert(i);
for (int i = 0; i < N; i++)
t[i].insert(M+1), tnt[i].insert(M+1);
while (Q--) {
int typ; cin >> typ; --typ;
if (typ) {
int l, r, v; cin >> l >> r >> v;
--l, --r, --v;
int ans = *t[v].lower_bound(l);
if (ans <= r) {
cout << ans+1 << ' ' << ans+1 << '\n';
continue;
}
ans = *tnt[v].lower_bound(l);
if (ans + 1 <= r) {
cout << ans+1 << ' ' << ans+2 << '\n';
continue;
}
cout << "-1 -1\n";
} else {
int pos, v; cin >> pos >> v;
--pos, --v;
t[A[pos]].erase(t[A[pos]].find(pos));
if (pos + 1 < M) {
int omk = lca(A[pos], A[pos+1]);
tnt[omk].erase(tnt[omk].find(pos));
}
if (pos - 1 >= 0) {
int omk = lca(A[pos-1], A[pos]);
tnt[omk].erase(tnt[omk].find(pos-1));
}
A[pos] = v;
t[A[pos]].insert(pos);
if (pos + 1 < M)
tnt[lca(A[pos], A[pos+1])].insert(pos);
if (pos - 1 >= 0)
tnt[lca(A[pos-1], A[pos])].insert(pos-1);
}
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t = 1; //cin >> t;
while (t--) solve();
}
# | 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... |