Submission #1227863

#TimeUsernameProblemLanguageResultExecution timeMemory
1227863PlayVoltzBirthday gift (IZhO18_treearray)C++20
100 / 100
1067 ms89192 KiB
#include <cstdio>
#include <stdio.h>
#include <stdbool.h>
#include <iostream>
#include <map>
#include <vector>
#include <climits>
#include <stack>
#include <string>
#include <queue>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <cctype>
#include <bitset>
#include <iomanip>
#include <cstring>
#include <numeric>
#include <cassert>
#include <random>
#include <chrono>
#include <fstream>
using namespace std;

#define int long long
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second

vector<int> depth;
vector<vector<int> > graph, twok;

void dfs(int node, int par, int d){
	depth[node]=d;
	twok[node][0]=par;
	for (int i=1; i<20; ++i)twok[node][i]=twok[twok[node][i-1]][i-1];
	for (auto num:graph[node])if (num!=par)dfs(num, node, d+1);
}

int lca(int a, int b){
	if (depth[a]<depth[b])swap(a, b);
	for (int i=0, k=depth[a]-depth[b]; i<20; ++i)if (k&(1<<i))a=twok[a][i];
	if (a==b)return a;
	for (int i=19; i>=0; --i)if (twok[a][i]!=twok[b][i])a=twok[a][i], b=twok[b][i];
	return twok[a][0];
}

int32_t main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n, m, q, a, b, c, t;
	cin>>n>>m>>q;
	vector<int> vect(m+1);
	depth.resize(n+1);
	graph.resize(n+1);
	twok.resize(n+1, vector<int>(20));
	for (int i=1; i<n; ++i){
		cin>>a>>b;
		graph[a].pb(b);
		graph[b].pb(a);
	}
	dfs(1, 1, 0);
	set<pii> one, two;
	for (int i=1; i<=m; ++i){
		cin>>vect[i];
		one.insert(mp(vect[i], i));
		if (i!=1)two.insert(mp(lca(vect[i], vect[i-1]), i-1));
	}	
	while (q--){
		cin>>t;
		if (t==1){
			cin>>a>>b;
			one.erase(mp(vect[a], a));
			if (a!=1)two.erase(mp(lca(vect[a], vect[a-1]), a-1));
			if (a!=m)two.erase(mp(lca(vect[a], vect[a+1]), a));
			vect[a]=b;
			one.insert(mp(vect[a], a));
			if (a!=1)two.insert(mp(lca(vect[a], vect[a-1]), a-1));
			if (a!=m)two.insert(mp(lca(vect[a], vect[a+1]), a));
		}
		else{
			cin>>a>>b>>c;
			auto it=one.lower_bound(mp(c, a));
			if (it!=one.end()&&it->fi==c&&it->se<=b){
				cout<<it->se<<" "<<it->se<<"\n";
				continue;
			}
			it = two.lower_bound(mp(c, a));
			if (it!=two.end()&&it->fi==c&&it->se<b)cout<<it->se<<" "<<it->se+1<<"\n";
			else cout<<"-1 -1\n";
		}
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...