Submission #382380

#TimeUsernameProblemLanguageResultExecution timeMemory
382380ritul_kr_singhBirthday gift (IZhO18_treearray)C++17
100 / 100
1166 ms101484 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp << " " <<
#define nl << "\n"

vector<vector<int>> g;
vector<array<int, 19>> p;
vector<int> t0, t1;
int dfsTimer = 0;

void dfs(int u, int parent){
	t0[u] = dfsTimer++;
	p[u][0] = parent;
	for(int i=1; i<19; ++i) p[u][i] = p[p[u][i-1]][i-1];
	for(int v : g[u]) if(v != parent) dfs(v, u);
	t1[u] = dfsTimer++;
}

bool isAncestor(int u, int v){
	return t0[u]<=t0[v] and t1[v]<=t1[u];
}

int lca(int u, int v){
	if(isAncestor(u, v)) return u;
	if(isAncestor(v, u)) return v;
	for(int i=18; i>=0; --i)
		while(u and !isAncestor(p[u][i], v)) u = p[u][i];
	return p[u][0];
}

signed main(){
	cin.tie(0)->sync_with_stdio(0);
	int n, m, q, x, y, type; cin >> n >> m >> q;
	g.resize(n), p.resize(n), t0.resize(n), t1.resize(n);
	for(int i=1; i<n; ++i){
		cin >> x >> y;
		--x, --y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	dfs(0, 0);
	// for(int i=0; i<n; ++i)
	// 	for(int j=i; j<n; ++j)
	// 		cout << i+1 sp j+1 sp lca(i, j)+1 nl;
	vector<int> a(m);
	vector<set<int>> pairs(n), pos(n);
	for(int i=0; i<m; ++i){
		cin >> a[i]; --a[i];
		pos[a[i]].insert(i);
		if(i) pairs[lca(a[i], a[i-1])].insert(i-1);
	}

	while(q--){
		cin >> type >> x >> y;
		--x, --y;
		if(type==1){
			if(x+1<m){
				pairs[lca(a[x], a[x+1])].erase(x);
				pairs[lca(y, a[x+1])].insert(x);
			}
			if(x){
				pairs[lca(a[x], a[x-1])].erase(x-1);
				pairs[lca(y, a[x-1])].insert(x-1);
			}
			pos[a[x]].erase(x);
			pos[y].insert(x);
			a[x] = y;
		}else{
			int v; cin >> v; --v;
			auto f = pos[v].lower_bound(x);
			if(f!=pos[v].end() and *f<=y){
				cout << *f+1 sp *f+1 nl;
				continue;
			}
			f = pairs[v].lower_bound(x);
			if(f!=pairs[v].end() and *f<y) cout << *f+1 sp *f+2 nl;
			else cout << -1 sp -1 nl;
		}
	}

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...