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>
// #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx2")
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
#define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define pb push_back
#define ss second
#define ff first
void err(istringstream *iss){}template<typename T,typename ...Args> void err(istringstream *iss,const T &_val, const Args&...args){string _name;*iss>>_name;if(_name.back()==',')_name.pop_back();cerr<<_name<<" = "<<_val<<", ",err(iss,args...);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7, maxn = 2e5 + 5, lg = 22, lng = 26, inf = ll(1e9) + 5;
ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll);return k*k%md*(b&1ll?a:1)%md;}
vector<int> adj[maxn];
int par[maxn][lg], h[maxn];
void dfs(int r, int p){
par[r][0] = p;
rep(i,1,lg) par[r][i] = par[par[r][i-1]][i-1];
for(int c: adj[r]) if(c != p) h[c] = h[r]+1, dfs(c, r);
}
int lca(int u, int v){
if(h[u] > h[v]) swap(u, v);
per(i,lg-1,0) if(h[u] <= h[par[v][i]]) v = par[v][i];
if(u == v) return u;
per(i,lg-1,0) if(par[u][i] != par[v][i]) u = par[u][i], v = par[v][i];
return par[u][0];
}
set<int> pos[maxn], bt[maxn];
int main(){
cin.tie(0) -> sync_with_stdio(0);
int n, m, q; cin >> n >> m >> q;
rep(i,1,n){
int u, v; cin >> u >> v; u--, v--;
adj[u].pb(v), adj[v].pb(u);
} dfs(0, 0);
vector<int> a(m);
rep(i,0,m){
cin >> a[i]; a[i]--;
pos[a[i]].insert(i);
if(i) bt[lca(a[i-1], a[i])].insert(i-1);
}
while(q--){
int t; cin >> t;
if(t&1){
int p, v; cin >> p >> v; p--, v--;
pos[a[p]].erase(p);
if(p) bt[lca(a[p], a[p-1])].erase(p-1);
if(p < m-1) bt[lca(a[p], a[p+1])].erase(p);
a[p] = v;
pos[a[p]].insert(p);
if(p) bt[lca(a[p], a[p-1])].insert(p-1);
if(p < m-1) bt[lca(a[p], a[p+1])].insert(p);
}else{
int l, r, x; cin >> l >> r >> x; l--, r--, x--;
auto it = pos[x].lower_bound(l);
if(it != end(pos[x]) && *it <= r) cout << *it + 1 << ' ' << *it + 1 << '\n';
else{
auto it = bt[x].lower_bound(l);
if(it != end(bt[x]) && *it < r) cout << *it + 1 << ' ' << *it + 2 << '\n';
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... |