답안 #1109220

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1109220 2024-11-06T08:19:03 Z stdfloat Birthday gift (IZhO18_treearray) C++17
0 / 100
1 ms 508 KB
#include <bits/stdc++.h>
using namespace std;

vector<int> d;

vector<vector<int>> E, sp;

void dfs(int x, int p = -1) {
	sp[x][0] = p;
	if (p != -1) d[x] = d[p] + 1;
	for (auto i : E[x])
		if (i != p) dfs(i, x);
}

int lca(int x, int y) {
	if (d[x] < d[y]) swap(x, y);

	for (int i = 19; i >= 0; i--)
		if (d[x] - (1 << i) >= d[y]) x = sp[x][i];

	if (x == y) return x;

	for (int i = 19; i >= 0; i--) {
		if (sp[x][i] != sp[y][i]) {
			x = sp[x][i]; y = sp[y][i];
		}
	}

	return sp[x][0];
}

int main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);

	int n, m, q;
	cin >> n >> m >> q;

	E.assign(n, {});
	for (int i = 1; i < n; i++) {
		int x, y;
		cin >> x >> y; x--; y--;

		E[x].push_back(y);
		E[y].push_back(x);
	}

	d.assign(n, 0);
	sp.assign(n, vector<int>(20, -1));
	dfs(0);

	for (int i = 1; i < 20; i++) {
		for (int j = 0; j < n; j++)
			if (sp[j][i - 1] != -1) sp[j][i] = sp[sp[j][i - 1]][i - 1];
	}

	vector<int> a(m);
	for (auto &i : a) {
		cin >> i; i--;
	}

	vector<set<pair<int, int>>> s(n);
	for (int i = 0; i + 1 < m; i++) {
		s[a[i]].insert({i, i});
		s[lca(a[i], a[i + 1])].insert({i, i + 1});
	}

	while (q--) {
		int tp;
		cin >> tp;

		if (tp == 1) {
			int ps, v;
			cin >> ps >> v; ps--; v--;
		
			s[a[ps]].erase({ps, ps});
			if (ps) s[lca(a[ps - 1], a[ps])].erase({ps - 1, ps});
			if (ps + 1 < m) s[lca(a[ps], a[ps + 1])].erase({ps, ps + 1});

			a[ps] = v;

			s[a[ps]].insert({ps, ps});
			if (ps) s[lca(a[ps - 1], a[ps])].insert({ps - 1, ps});
			if (ps + 1 < m) s[lca(a[ps], a[ps + 1])].insert({ps, ps + 1});
		}
		else {
			int l, r, v;
			cin >> l >> r >> v; l--; r--; v--;

			auto t = s[v].lower_bound({l, l});
			if (t == s[v].end() || (*t).second > r) cout << "-1 -1\n";
			else cout << (*t).first + 1 << ' ' << (*t).second + 1 << '\n';
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB n=5
2 Correct 1 ms 504 KB n=100
3 Correct 1 ms 508 KB n=100
4 Correct 1 ms 336 KB n=100
5 Correct 1 ms 336 KB n=100
6 Correct 1 ms 336 KB n=100
7 Correct 1 ms 336 KB n=100
8 Correct 1 ms 336 KB n=100
9 Correct 1 ms 336 KB n=100
10 Correct 1 ms 336 KB n=100
11 Correct 1 ms 336 KB n=100
12 Correct 1 ms 336 KB n=100
13 Correct 1 ms 336 KB n=100
14 Correct 1 ms 336 KB n=100
15 Correct 1 ms 336 KB n=100
16 Correct 1 ms 336 KB n=100
17 Incorrect 1 ms 336 KB Jury has the answer but participant has not
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB n=5
2 Correct 1 ms 504 KB n=100
3 Correct 1 ms 508 KB n=100
4 Correct 1 ms 336 KB n=100
5 Correct 1 ms 336 KB n=100
6 Correct 1 ms 336 KB n=100
7 Correct 1 ms 336 KB n=100
8 Correct 1 ms 336 KB n=100
9 Correct 1 ms 336 KB n=100
10 Correct 1 ms 336 KB n=100
11 Correct 1 ms 336 KB n=100
12 Correct 1 ms 336 KB n=100
13 Correct 1 ms 336 KB n=100
14 Correct 1 ms 336 KB n=100
15 Correct 1 ms 336 KB n=100
16 Correct 1 ms 336 KB n=100
17 Incorrect 1 ms 336 KB Jury has the answer but participant has not
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB n=5
2 Correct 1 ms 504 KB n=100
3 Correct 1 ms 508 KB n=100
4 Correct 1 ms 336 KB n=100
5 Correct 1 ms 336 KB n=100
6 Correct 1 ms 336 KB n=100
7 Correct 1 ms 336 KB n=100
8 Correct 1 ms 336 KB n=100
9 Correct 1 ms 336 KB n=100
10 Correct 1 ms 336 KB n=100
11 Correct 1 ms 336 KB n=100
12 Correct 1 ms 336 KB n=100
13 Correct 1 ms 336 KB n=100
14 Correct 1 ms 336 KB n=100
15 Correct 1 ms 336 KB n=100
16 Correct 1 ms 336 KB n=100
17 Incorrect 1 ms 336 KB Jury has the answer but participant has not
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB n=5
2 Correct 1 ms 504 KB n=100
3 Correct 1 ms 508 KB n=100
4 Correct 1 ms 336 KB n=100
5 Correct 1 ms 336 KB n=100
6 Correct 1 ms 336 KB n=100
7 Correct 1 ms 336 KB n=100
8 Correct 1 ms 336 KB n=100
9 Correct 1 ms 336 KB n=100
10 Correct 1 ms 336 KB n=100
11 Correct 1 ms 336 KB n=100
12 Correct 1 ms 336 KB n=100
13 Correct 1 ms 336 KB n=100
14 Correct 1 ms 336 KB n=100
15 Correct 1 ms 336 KB n=100
16 Correct 1 ms 336 KB n=100
17 Incorrect 1 ms 336 KB Jury has the answer but participant has not
18 Halted 0 ms 0 KB -