#include<bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define all(x) x.begin(), x.end()
#define int long long
#define pq priority_queue
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define pp pop_back
#define F first
#define S second
using namespace std;
int n, m, q;
int a[200001];
int lvl[200001];
int J[200001][20];
set<int> s[200001];
set<int> o[200001];
vector<int> d[200001];
void dfs(int s, int p)
{
lvl[s] = lvl[p] + 1;
J[s][0] = p;
for (int i = 1; i < 18; i++) J[s][i] = J[J[s][i-1]][i-1];
for (auto i: d[s]) if (i != p) dfs(i, s);
}
int lca(int u, int v)
{
if (lvl[u] > lvl[v]) swap(u, v);
for (int i = 17; i >= 0; i--) if (lvl[J[v][i]] >= lvl[u]) v = J[v][i];
if (u == v) return u;
for (int i = 17; i >= 0; i--) if (J[u][i] != J[v][i]) u = J[u][i], v = J[v][i];
return J[u][0];
}
void update(int x)
{
if (x > 1) s[lca(a[x], a[x-1])].insert(x-1);
if (x < m) s[lca(a[x], a[x+1])].insert(x);
o[a[x]].insert(x);
}
void remove(int x)
{
if (x > 1) s[lca(a[x], a[x-1])].erase(x-1);
if (x < m) s[lca(a[x], a[x+1])].erase(x);
o[a[x]].erase(x);
}
void answer(int l, int r, int v)
{
auto it1 = o[v].lb(l);
auto it2 = s[v].lb(l);
if (it1 != o[v].end() && (*it1) <= r) cout << *it1 << ' ' << *it1 << '\n';
else if (it2 != s[v].end() && (*it2) < r) cout << *it2 << ' ' << *it2+1 << '\n';
else cout << -1 << ' ' << -1 << '\n';
}
void solve ()
{
cin >> n >> m >> q;
for (int i = 0; i < n-1; i++)
{
int u, v; cin >> u >> v;
d[u].pb(v); d[v].pb(u);
}
dfs(1, 1);
for (int i = 1; i <= m; i++) {cin >> a[i]; update(i);}
while (q--)
{
int t; cin >> t;
if (t == 1)
{
int x, v; cin >> x >> v;
remove(x);
a[x] = v;
update(x);
}
else
{
int l, r, v;
cin >> l >> r >> v;
answer(l, r, v);
}
}
}
signed main() {IOS solve(); return 0;}
# | 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... |