제출 #341949

#제출 시각아이디문제언어결과실행 시간메모리
341949_aniBirthday gift (IZhO18_treearray)C++17
56 / 100
4064 ms68972 KiB
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>
using namespace std;
const int l = 18;
set <int> v[2 * 100002];
set<int> p[2 * 100002];
int a[2 * 100002];
int up[2 * 100002][l + 1], tin[2 * 100002], tout[2 * 100002], t;
vector<int> g[2 * 100002];
void Dfs(int v, int p)
{
	tin[v] = ++t;
	up[v][0] = p;
	for (int i = 1; i <= l; i++)
		if (up[v][i - 1] != -1)
			up[v][i] = up[up[v][i - 1]][i - 1];
	for (int to : g[v])
		if (to != p)
			Dfs(to, v);
	tout[v] = t;
}
bool IsParent(int a, int b) {
	return tin[a] <= tin[b] && tout[a] >= tout[b];
}
int Lca(int a, int b)
{
	if (IsParent(a, b))return a;
	if (IsParent(b, a))return b;
	for (int i = l; i >= 0; --i)
		if (up[a][i] != -1 && !IsParent(up[a][i], b))
			a = up[a][i];
	return up[a][0];
}
int main()
{
	int n, m, q;
	cin >> n >> m >> q;
	for (int i = 0; i < n - 1; i++)
	{
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	for (int i = 1; i <= n; i++)
		for (int j = 0; j <= l; j++)
			up[i][j] = -1;
	Dfs(1, -1);
	for (int i = 0; i < m; i++)
	{
		cin >> a[i];
		v[a[i]].insert(i);
		if (i > 0)
			p[Lca(a[i], a[i - 1])].insert(i - 1);
	}
	while (q--)
	{
		int type;
		cin >> type;
		if (type == 1)
		{
			int pos, V;
			cin >> pos >> V;
			pos--;
			v[a[pos]].erase(pos);
			if (pos - 1 >= 0)
			{
				p[Lca(a[pos], a[pos - 1])].erase(pos - 1);
				p[Lca(V, a[pos - 1])].insert(pos - 1);
			}
			if (pos + 1 < m)
			{
				p[Lca(a[pos], a[pos + 1])].erase(pos);
				p[Lca(V, a[pos + 1])].insert(pos);
			}
			a[pos] = V;
			v[V].insert(pos);
		}
		else
		{
			bool ok = false;
			int l, r, V;
			cin >> l >> r >> V;
			l--;
			r--;
			for (auto x : p[V])
			{
				if (x >= l && x + 1 <= r)
				{
					cout << x + 1 << ' ' << x + 2 << '\n';
					ok = true;
					break;
				}
			}
			if (!ok)
				for (auto x : v[V])
					if (x >= l && x <= r)
					{
						cout << x + 1 << ' ' << x + 1 << '\n';
						ok = true;
						break;
					}
			if (!ok)
				cout << -1 << ' ' << -1 << '\n';
		}
	}
	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...