Submission #379225

#TimeUsernameProblemLanguageResultExecution timeMemory
3792258e7Birthday gift (IZhO18_treearray)C++14
0 / 100
19 ms24044 KiB
//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <set>
#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];
set<int> pos[maxn], pos2[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];
		pos[a[i]].insert(i);
		if (i > 1) {
			b[i - 1] = lca(a[i - 1], a[i]);
			pos2[b[i - 1]].insert(i-1);
		}
	}
	for (int i = 1;i <= n;i++) pos[i].insert(n + 1), pos2[i].insert(n + 1);

	for (int i = 0;i < q;i++) {
		int type;
		cin >> type;
		if (type == 1) {
			int p, v;
			cin >> p >> v;
			if (p > 1) {
				//sb[pos - 1]->modify(1, n + 1, b[i], -1, 0);
				pos2[b[p - 1]].erase(pos2[b[p - 1]].find(p-1));
				b[p - 1] = lca(a[p - 1], v);
				pos2[b[p - 1]].insert(p-1);
			}
			if (p < n) {
				pos2[b[p]].erase(pos2[b[p]].find(p));
				b[p] = lca(v, a[p + 1]);
				pos2[b[p]].insert(p);
			}
			pos[a[p]].erase(pos[a[p]].find(p));
			a[p] = v;
			pos[a[p]].insert(p);
		} else {
			int l, r, v;
			cin >> l >> r >> v;
			int ai = *pos[v].lower_bound(l);
			if (ai <= r) cout << ai << " " << ai << "\n";
			else {
				int bi = *pos2[v].lower_bound(l);
				//cout << bi << endl;
				if (bi < r) cout << bi << " " << bi + 1 << "\n";
				else {
					cout << -1 << " " << -1 << "\n";
				}
			}
		}
	}
}
/*
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...