#include <bits/stdc++.h>
//#include "includeall.h"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define all(x) x.begin(), x.end()
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define FOR(i, x, n) for (ll i =x; i<=n; ++i)
#define RFOR(i, x, n) for (ll i =x; i>=n; --i)
#pragma GCC optimize("O3","unroll-loops")
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<pi, ll> pii;
typedef pair<pi, pi> piii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll n, m, q;
vector<ll> adj[200005];
ll dep[200005], twok[200005][20];
ll a[200005];
set<ll> one[200005], two[200005];
void dfs(ll x = 1, ll p = -1)
{
for (ll k=1; k<20; ++k)
{
if (twok[x][k-1]==-1) twok[x][k] = -1;
else twok[x][k] = twok[twok[x][k-1]][k-1];
}
for (ll u: adj[x]) if (u!=p)
{
dep[u] = dep[x] + 1;
twok[u][0] = x;
dfs(u, x);
}
}
ll lca(ll x, ll y)
{
if (x==y) return x;
if (dep[x] < dep[y]) swap(x, y);
for (ll k=0; k<20; ++k) if ((1LL << k)&(dep[x] - dep[y])) x = twok[x][k];
if (x==y) return x;
for (ll k=19; k>=0; --k)
{
if (twok[x][k]==twok[y][k]) continue;
x = twok[x][k], y = twok[y][k];
}
return twok[x][0];
}
int main()
{
cin >> n >> m >> q;
for (ll i=0; i<n-1; ++i)
{
ll u, v; cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
twok[1][0] = -1, dep[1] = 0;
dfs();
for (ll i=0; i<m; ++i) cin >> a[i];
for (ll i=0; i<m; ++i)
{
one[a[i]].insert(i);
if (i > 0) two[lca(a[i], a[i-1])].insert(i);
}
while (q--)
{
ll t;
cin >> t;
if (t==1)
{
ll x, y;
cin >> x >> y; x--;
one[a[x]].erase(one[a[x]].find(x));
if (x > 0) two[lca(a[x], a[x-1])].erase(two[lca(a[x], a[x-1])].find(x));
if (x + 1 < m) two[lca(a[x+1], a[x])].erase(two[lca(a[x + 1], a[x])].find(x + 1));
a[x] = y;
one[a[x]].insert(x);
if (x > 0) two[lca(a[x], a[x-1])].insert(x);
if (x + 1 < m) two[lca(a[x+1], a[x])].insert(x + 1);
}
else
{
ll l, r, v;
cin >> l >> r >> v; l--; r--;
auto it = one[v].lb(l), it2 = two[v].ub(l);
if (it!=one[v].end() && *it<= r) cout << *it +1 << ' ' << *it + 1 << endl;
else if (it2!=two[v].end() && *it2 <= r) cout << *it2 << ' ' << *it2 + 1 << endl;
else cout << "-1 -1\n";
}
}
return 0;
}
/*/
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
* /*/
# | 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... |