제출 #338828

#제출 시각아이디문제언어결과실행 시간메모리
338828sumit_kk10Birthday gift (IZhO18_treearray)C++14
12 / 100
206 ms492 KiB
#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define ll long long int
#define ld long double
using namespace std;
const int N = 500 + 5;
const int MOD = 1e9 + 7;
int n, m, q;
vector<int> graph[N];
vector<int> seq;
ll dp[N][(ll) log2(N)], lvl[N];

void dfs(int source, int par, int level){
	dp[source][0] = par;
	lvl[source] = level;
	for(auto k : graph[source])
		if(k != par)
			dfs(k, source, level + 1);
}

void init(){
	dfs(1, -1, 0);
	for(int i = 1; i <= (log2(n)); ++i)
		for(int j = 1; j <= n; ++j)
			if(dp[j][i - 1] != -1)
				dp[j][i] = dp[dp[j][i - 1]][i - 1];
}

ll LCA(int u, int v){
	if(lvl[u] > lvl[v]) swap(u, v);
	ll d = lvl[v] - lvl[u];
	while(d){
		int jump = log2(d);
		v = dp[v][jump];
		d -= pow(2, jump);
	}
	if(v == u) 
		return v;
	for(int i = log2(n); i >= 0; --i){
		if(dp[v][i] != -1 && dp[v][i] != dp[u][i]){
			u = dp[u][i];
			v = dp[v][i];
		}
	}
	return dp[v][0];
}

void solve(){
	cin >> n >> m >> q;
	for(int i = 0; i < n - 1; ++i){
		int u, v;
		cin >> u >> v;
		graph[u].push_back(v);
		graph[v].push_back(u);
	}
	for(int i = 0; i < m; ++i){
		int x;
		cin >> x;
		seq.push_back(x);
	}
	init();
	while(q--){
		int query;
		cin >> query;
		if(query == 1){
			int x, val;
			cin >> x >> val;
			--x;
			seq[x] = val;
		}
		else{
			int l, r, v;
			cin >> l >> r >> v;
			--l, --r;
			bool ok = 0;
			for(int i = l; i <= r; ++i){
				int lca = seq[i];
				for(int j = i; j <= r; ++j){
					lca = LCA(lca, seq[j]);
					if(lca == v){
						cout << i + 1 << ' ' << j + 1 << '\n'; 
						ok = 1;
						break;
					}
				}
				if(ok)
					break;
			}
			if(!ok)
				cout << "-1 -1\n";
		}
	}
}

int main(){
	fast;
	int t = 1;
	// cin >> t;
	while(t--)
		solve();
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...