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]; pii sptable[M][N];
void dfs_tour(int u, int p){
tin[u] = tout[u] = ++ctrtime; sptable[0][ctrtime] = {h[u], u};
Fora(&v, adj[u]){
if (v == p){
continue;
}
h[v] = h[u] + 1;
dfs_tour(v, u);
tout[u] = ++ctrtime; sptable[0][ctrtime] = {h[u], u};
}
}
int lca(int u, int v){
int l = min(tin[u], tin[v]), r = max(tout[u], tout[v]), z = __lg(r - l + 1);
return min(sptable[z][l], sptable[z][r - (1 << z) + 1]).se;
}
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);
For(j, 1, M){
ForE(i, 1, ctrtime - (1 << j) + 1){
sptable[j][i] = min(sptable[j - 1][i], sptable[j - 1][i + (1 << (j - 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;
ForE(x, l, r){
int v = a[x];
if (v == u){
ansx = x;
ansy = x;
}
ForE(y, x + 1, r){
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... |