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][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+1);
for (int i = 1; i <= M; i++) cin >> A[i];
dfs(1,1);
for (int i = 1; i <= M; i++) t[A[i]].insert(i);
for (int i = 1; i <= M-1; i++) tnt[lca(A[i], A[i+1])].insert(i);
while (Q--) {
int typ; cin >> typ; --typ;
if (typ) {
int l, r, v; cin >> l >> r >> v;
auto itr1 = t[v].lower_bound(l);
auto itr2 = tnt[v].lower_bound(l);
if (itr1 != t[v].end() && *itr1 <= r) {
cout << *itr1 << ' ' << *itr1 << '\n';
} else if (itr2 != tnt[v].end() && *itr2 + 1 <= r) {
cout << *itr2 << ' ' << *itr2 + 1 << '\n';
} else cout << "-1 -1\n";
} else {
int i, v; cin >> i >> v;
t[A[i]].erase(i); t[v].insert(i);
if (i + 1 <= M) {
tnt[lca(A[i], A[i+1])].erase(i);
tnt[lca(v, A[i+1])].insert(i);
}
if (i - 1 >= 1) {
tnt[lca(A[i-1], A[i])].erase(i-1);
tnt[lca(A[i-1], v)].insert(i-1);
}
A[i] = v;
}
}
}
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... |