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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (int i = l; i < r; i++)
#define ForE(i, l, r) for (int i = l; i <= r; i++)
#define FordE(i, l, r) for (int i = l; i >= r; i--)
#define Fora(v, a) for (auto v: a)
#define bend(a) a.begin(), a.end()
#define isz(a) ((signed)a.size())
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
using vi = vector <int>;
using vpii = vector <pii>;
using vvi = vector <vi>;
const int N = 2e5 + 5, M = __lg(N) + 2;
int n, m, q;
vi adj[N];
int a[N];
int h[N];
int ctrtime, tin[N], tout[N];
int lift[N][M];
void dfs_tour(int u, int p){
tin[u] = tout[u] = ++ctrtime;
Fora(&v, adj[u]){
if (v == p){
continue;
}
h[v] = h[u] + 1;
dfs_tour(v, u);
tout[u] = ++ctrtime;
}
}
void dfs_lift(int u, int p){
lift[u][0] = p;
For(i, 1, M){
lift[u][i] = lift[lift[u][i - 1]][i - 1];
}
Fora(&v, adj[u]){
if (v == p){
continue;
}
dfs_lift(v, u);
}
}
int lca(int u, int v){
if (h[u] < h[v]){
swap(u, v);
}
FordE(i, M - 1, 0){
if (h[u] - (1 << i) >= h[v]){
u = lift[u][i];
}
}
if (u == v){
return u;
}
FordE(i, M - 1, 0){
if (lift[u][i] != lift[v][i]){
u = lift[u][i];
v = lift[v][i];
}
}
return lift[u][0];
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen(".inp", "r", stdin);
// freopen(".out", "w", stdout);
cin >> n >> m >> q;
For(i, 1, n){
int u, v; cin >> u >> v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
ForE(i, 1, m){
cin >> a[i];
}
dfs_tour(1, 1);
dfs_lift(1, 1);
while (q--){
int que; cin >> que;
if (que == 1){
int pos, u; cin >> pos >> u;
a[pos] = u;
}
else{
int l, r, u; cin >> l >> r >> u;
int ansx = -1, ansy = -1;
int v = -1, x = -1;
ForE(y, l, r){
if (!(tin[u] <= tin[a[y]] and tout[a[y]] <= tout[u])){
v = -1;
x = -1;
continue;
}
if (v == -1){
v = a[y];
x = y;
}
else{
v = lca(v, a[y]);
}
if (v == u){
ansx = x; ansy = y;
}
}
cout << ansx << ' ' << ansy << endl;
}
}
}
/*
==================================================+
INPUT: |
--------------------------------------------------|
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
--------------------------------------------------|
==================================================+
OUTPUT: |
--------------------------------------------------|
1 3
3 3
-1 -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... |