제출 #41563

#제출 시각아이디문제언어결과실행 시간메모리
41563aomeBirthday gift (IZhO18_treearray)C++14
100 / 100
3357 ms113572 KiB
#include <bits/stdc++.h>
using namespace std;

const int N = 200005;

int n, m, q;
int a[N];
int high[N];
int p[20][N];
vector<int> G[N];
set<int> s[2][N];

int lca(int u, int v) {
	if (high[u] > high[v]) swap(u, v);
	for (int i = 18; i >= 0; --i) {
		if (high[p[i][v]] >= high[u]) v = p[i][v];
	}
	for (int i = 18; i >= 0; --i) {
		if (p[i][v] != p[i][u]) v = p[i][v], u = p[i][u];
	}
	return (u == v) ? u : p[0][u];
}

void dfs(int u) {
	for (auto v : G[u]) {
		if (p[0][u] == v) continue;
		p[0][v] = u, high[v] = high[u] + 1, dfs(v);
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin >> n >> m >> q;
	for (int i = 1; i < n; ++i) {
		int u, v; cin >> u >> v;
		G[u].push_back(v), G[v].push_back(u);
	}
	p[0][1] = 1, dfs(1);
	for (int i = 1; i <= 18; ++i) {
		for (int j = 1; j <= n; ++j) {
			p[i][j] = p[i - 1][p[i - 1][j]];
		}
	}
	for (int i = 1; i <= m; ++i) cin >> a[i];
	for (int i = 1; i <= m; ++i) s[0][a[i]].insert(i);
	for (int i = 1; i < m; ++i) s[1][lca(a[i], a[i + 1])].insert(i);
	while (q--) {
		int type; cin >> type;
		if (type == 1) {
			int pos, v; cin >> pos >> v;
			s[0][a[pos]].erase(pos);
			s[0][v].insert(pos);
			if (pos > 1) {
				s[1][lca(a[pos - 1], a[pos])].erase(pos - 1);
				s[1][lca(a[pos - 1], v)].insert(pos - 1);
			}
			if (pos < m) {
				s[1][lca(a[pos], a[pos + 1])].erase(pos);
				s[1][lca(v, a[pos + 1])].insert(pos);
			}
			a[pos] = v;
		}
		else {
			int l, r, v; cin >> l >> r >> v;
			set<int> :: iterator it;
			it = s[0][v].lower_bound(l);
			if (it == s[0][v].end() || *it > r) {
				it = s[1][v].lower_bound(l);
				if (it != s[1][v].end() && *it < r) {
					cout << *it << ' ' << *it + 1 << '\n'; continue;
				}
			}
			else {
				cout << *it << ' ' << *it << '\n'; continue;
			}
			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...