제출 #503293

#제출 시각아이디문제언어결과실행 시간메모리
503293NachoLibreBirthday gift (IZhO18_treearray)C++17
100 / 100
975 ms80580 KiB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;

const int N = 200005, M = 18;
vector<int> v[N];
set<int> s[N], o[N];
int a[N], b[N], d[N], up[N][M];

void dfsu(int x = 1, int y = 0) {
	up[x][0] = y;
	d[x] = d[y] + 1;
	for(int z : v[x]) {
		if(z == y) continue;
		dfsu(z, x);
	}
}

int L(int x, int y) {
	if(!x || !y) return 0;
	if(d[x] < d[y]) swap(x, y);
	for(int i = M - 1; i >= 0; --i) {
		if(d[x] - (1 << i) >= d[y]) {
			x = up[x][i];
		}
	}
	if(x == y) return x;
	for(int i = M - 1; i >= 0; --i) {
		if(up[x][i] ^ up[y][i]) {
			x = up[x][i];
			y = up[y][i];
		}
	}
	return up[x][0];
}

void _set(int i, int x) {
	if(b[i - 1]) s[b[i - 1]].erase(i - 1);
	if(b[i]) s[b[i]].erase(i);
	if(a[i]) o[a[i]].erase(i);
	a[i] = x;
	if(a[i]) o[a[i]].insert(i);
	b[i - 1] = L(a[i - 1], a[i]);
	b[i] = L(a[i], a[i + 1]);
	if(b[i - 1]) s[b[i - 1]].insert(i - 1);
	if(b[i]) s[b[i]].insert(i);
}

void answer(int l, int r, int x) {
	auto it = o[x].lower_bound(l);
	if(it != o[x].end() && *it <= r) {
		cout << *it << " " << *it << "\n";
		return;
	}
	it = s[x].lower_bound(l);
	if(it != s[x].end() && *it <= r - 1) {
		cout << *it << " " << *it + 1 << "\n";
		return;
	}
	cout << "-1 -1\n";
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	#ifdef x
	freopen("x.txt", "r", stdin);
	#endif
	int n, m, q;
	cin >> n >> m >> q;
	for(int i = 1; i < n; ++i) {
		int x, y;
		cin >> x >> y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	dfsu();
	for(int i = 1; i < M; ++i) {
		for(int j = 1; j <= n; ++j) {
			up[j][i] = up[up[j][i - 1]][i - 1];
		}
	}
	for(int i = 1; i <= m; ++i) {
		int x;
		cin >> x;
		_set(i, x);
	}
	for(int i = 1; i <= q; ++i) {
		int t, x, y;
		cin >> t >> x >> y;
		if(t == 1) {
			_set(x, y);
		} else {
			int z;
			cin >> z;
			answer(x, y, z);
		}
	}
	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...