이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<ll,ll>
#define pb push_back
#define fi first
#define se second
const ll N = 2e5+5;
const ll LOG = 20;
ll n,m,q,u,v;
ll l,r,mid,ans1,ans2,val;
ll var,x,y,upd,event;
bool ans;
ll depth[N],a[N];
ll anc[N][LOG];
vector<ll> adj[N];
void dfs(ll x, ll y) {
	anc[x][0] = y;
	depth[x] = depth[y] + 1;
	for(int i=1; i<LOG; i++) {
		anc[x][i] = anc[anc[x][i-1]][i-1];
	}
	for(auto i : adj[x]) {
		if(i == y) continue;
		dfs(i,x);
	}
}
ll lca(ll x, ll y) {
	if(depth[x] < depth[y]) swap(x,y);
	for(int i=LOG-1; i>=0; i--) {
		if(depth[anc[x][i]] >= depth[y]) {
			x = anc[x][i];
		}
	}
	if(x == y) return x;
	for(int i=LOG-1; i>=0; i--) {
		if(anc[x][i] != anc[y][i]) {
			x = anc[x][i];
			y = anc[y][i];
		}
	}
	return anc[x][0];
}
ll check(ll x, ll y) {
	ll temp = a[x];
	for(int i=x+1; i<=y; i++) {
		temp = lca(temp,a[i]);
	}
	return temp;
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m >> q;
	for(int i=1; i<n; i++) {
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	dfs(1,0);
	for(int i=1; i<=m; i++) {
		cin >> a[i];
	}
	while(q--) {
		cin >> event;
		if(event == 1) {
			cin >> upd >> val;
			a[upd] = val;
		}
		else {
			ans = false;
			cin >> x >> y >> var;
			ans1 = -1; ans2 = -1;
			for(int i=x; i<=y; i++) {
				l = i;
				r = y;
				while(l <= r) {
					mid = (l+r)/2;
					ll ret = check(i,mid);
					if(depth[ret] < depth[var]) {
						r = mid - 1;
					}
					else if(depth[ret] > depth[var]) {
						l = mid + 1;
					}
					else if(ret == var) {
						ans1 = i;
						ans2 = mid;
						ans = true;
						break;
					}
					else break;
				}
				if(ans) break;
			}
			cout << ans1 << " " << ans2 << 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... |