#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e18;
const int M = 2e5 + 1;
const int LOG = 20;
int n , m , q , a[M] , up[LOG][M] , dep[M];
vector < int > adj[M];
void dfs(int v , int p)
{
up[0][v] = p;
for(int i = 1;i < LOG;i++) up[i][v] = up[i - 1][up[i - 1][v]];
for(int u : adj[v]){
if(u == p) continue;
dep[u] = dep[v] + 1;
dfs(u , v);
}
}
int LCA(int a , int b)
{
if(dep[a] < dep[b]) swap(a , b);
int dif = dep[a] - dep[b];
for(int i = 0;i < LOG;i++)
{
if((1 << i) & dif)
{
a = up[i][a];
}
}
if(a == b) return a;
for(int i = LOG - 1;i >= 0;i--)
{
if(up[i][a] != up[i][b])
{
a = up[i][a];
b = up[i][b];
}
}
return up[0][a];
}
signed main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
cin >> n >> m >> q;
for(int i = 2;i <= n;i++)
{
int u , v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i = 1;i <= m;i++) cin >> a[i];
dfs(1 , -1);
while(q--)
{
int t;
cin >> t;
if(t == 1)
{
int pos , v;
cin >> pos >> v;
a[pos] = v;
}
else
{
int l , r , v;
cin >> l >> r >> v;
vector < int > p(m + 1 , 0);
for(int i = l;i <= r;i++) p[i] = (LCA(v , a[i]) == v);
for(int i = 2;i <= m;i++) p[i] += p[i - 1];
bool can = 0;
for(int i = l;i <= r;i++)
{
for(int j = i;j <= r;j++)
{
if(p[j] - p[i - 1] == j - i + 1 && LCA(a[i] , a[j]) == v)
{
cout << i << ' ' << j << endl;
can = 1;
break;
}
}
if(can) break;
}
if(!can)
{
cout << -1 << ' ' << -1 << endl;
}
}
}
}
# | 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... |