답안 #876442

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
876442 2023-11-21T18:38:19 Z serifefedartar Birthday gift (IZhO18_treearray) C++17
0 / 100
8 ms 35524 KB
#include <bits/stdc++.h>
using namespace std;

#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 998244353;
const ll LOGN = 20; 
const ll MAXN = 2e5 + 100;

vector<vector<int>> graph;
vector<int> A;
int depth[MAXN], up[LOGN][MAXN];
set<int> single[MAXN], two[MAXN];
void dfs(int node, int parent) {
	for (auto u : graph[node]) {
		if (u == parent)
			continue;
		depth[u] = depth[node] + 1;
		up[0][u] = node;
		for (int i = 1; i < LOGN; i++)
			up[i][u] = up[i-1][up[i-1][u]];
		dfs(u, node);
	}
}

int find(int node, int k) {
	for (int i = 0; i < LOGN; i++) {
		if ((1<<i) & k)
			node = up[i][node];
	}
	return node;
}

int lca(int a, int b) {
	if (depth[b] > depth[a])
		swap(a, b);
	a = find(a, depth[a] - depth[b]);
	if (a == b)
		return a;

	for (int i = LOGN - 1; i >= 0; i--) {
		if (up[i][a] != up[i][b]) {
			a = up[i][a];
			b = up[i][b];
		}
	}
	return up[0][a];
}

int main() {
	fast
	int n, m, q, a, b;
	cin >> n >> m >> q;

	graph = vector<vector<int>>(n+1, vector<int>());
	A = vector<int>(n+1);
	for (int i = 1; i < n; i++) {
		cin >> a >> b;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	for (int i = 0; i < LOGN; i++)
		up[i][1] = 1;
	dfs(1, 1);
	for (int i = 1; i <= m; i++) {
		cin >> A[i];
		single[A[i]].insert(i);
	}
	for (int i = 2; i <= m; i++)
		two[lca(A[i], A[i-1])].insert(i-1);

	while (q--) {
		int type;
		cin >> type;
		if (type == 1) {
			int pos, v;
			cin >> pos >> v;
			single[A[pos]].erase(pos);
			if (pos > 1)
				two[lca(A[pos], A[pos-1])].erase(pos-1);
			if (pos < m)
				two[lca(A[pos], A[pos+1])].erase(pos);

			A[pos] = v;
			single[A[pos]].insert(pos);
			if (pos > 1)
				two[lca(A[pos], A[pos-1])].insert(pos-1);
			if (pos < m)
				two[lca(A[pos], A[pos+1])].insert(pos);
		} else {
			int x, y, low;
			cin >> x >> y >> low;

			auto it = single[low].lower_bound(x);
			auto it2 = two[low].lower_bound(x);
			if (it != single[low].end() && *it <= y)
				cout << *it << " " << *it + 1 << "\n";
			else if (it2 != two[low].end() && *it2 + 1 <= y)
				cout << *it2 << " " << *it2 + 1 << "\n";
			else
				cout << "-1 -1\n";
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 35420 KB n=5
2 Incorrect 6 ms 35524 KB Wrong output format.
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 35420 KB n=5
2 Incorrect 6 ms 35524 KB Wrong output format.
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 35420 KB n=5
2 Incorrect 6 ms 35524 KB Wrong output format.
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 35420 KB n=5
2 Incorrect 6 ms 35524 KB Wrong output format.
3 Halted 0 ms 0 KB -