Submission #379010

#TimeUsernameProblemLanguageResultExecution timeMemory
3790108e7Birthday gift (IZhO18_treearray)C++14
0 / 100
4 ms5100 KiB
//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#define ll long long
#define maxn 200005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
vector<int> adj[maxn];
int a[maxn], b[maxn], lef[maxn], rig[maxn], ord[maxn], dep[maxn], anc[18][maxn];

int c = 0;
void dfs(int n, int par, int d) {
	anc[0][n] = par;
	dep[n] = d;
	lef[n] = c;
	ord[c++] = n;
	for (int v:adj[n]) {
		if (v != par) {
			dfs(v, n, d + 1);
		}
	}
	rig[n] = c;
}


int lca(int a, int b) {
	if (dep[a] < dep[b]) swap(a, b);
	int hdif = dep[a] - dep[b], cnt = 0;
	while (hdif) {
		if (hdif & 1) a = anc[cnt][a];
		cnt++, hdif >>= 1;
	}
	if (a == b) return a;
	for (int i = 17;i >= 0;i--) {
		if (anc[i][a] != anc[i][b]) {
			a = anc[i][a], b = anc[i][b];
		}
	}
	return anc[0][a];
}
int main() {
	io
	int n, m, q;
	cin >> n >> m >> q;
	for (int i = 0;i < n - 1;i++) {
		int u, v;
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	dfs(1, 0, 0);
	for (int i = 1;i < 18;i++) {
		for (int j = 1;j <= n;j++) anc[i][j] = anc[i - 1][anc[i - 1][j]];
	}

	for (int i = 1;i <= m;i++) {
		cin >> a[i];
		//a[i] = lef[a[i]];
	}

	for (int i = 0;i < q;i++) {
		int type;
		cin >> type;
		if (type == 1) {
			int pos, v;
			cin >> pos >> v;
			if (pos) b[pos - 1] = lca(a[pos - 1], v);
			if (pos < n) b[pos] = lca(v, a[pos + 1]);
			a[pos] = v;
		} else {
			int l, r, v;
			cin >> l >> r >> v;
			int ax = -1, ay = 1;
			for (int j = l;j <= r;j++) {
				if (a[j] == v) {
					ax = j, ay = j;
					break;
				}
				if (j < r && b[j] == v) {
					ax = j, ay = j + 1;
					break;
				}
			}
			cout << ax << " " << ay << endl;
		}
	}
}
/*
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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...