답안 #760338

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
760338 2023-06-17T13:01:24 Z NK_ Birthday gift (IZhO18_treearray) C++17
100 / 100
756 ms 75644 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'
#define pb push_back

#define f first
#define s second
#define mp make_pair

using pi = pair<int, int>;

const int nax = 2e5+5;
const int LG = 18;

template<class T> using V = vector<T>;

int up[nax][LG];
int dep[nax];

int jmp(int u, int d) {
	for(int i = 0; i < LG; i++) if ((d >> i) & 1) {
		u = up[u][i];
	}
	return u;
}

int lca(int a, int b) {
	if (dep[a] < dep[b]) swap(a, b);

	a = jmp(a, dep[a] - dep[b]);
	if (a == b) return a;

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

	return up[a][0];
}

int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	int N, M, Q; cin >> N >> M >> Q;

	V<V<int>> adj(N);
	for(int _ = 0; _ < N-1; _++) {
		int u, v; cin >> u >> v; --u, --v;
		adj[u].pb(v);
		adj[v].pb(u);
	}

	function<void(int, int)> dfs = [&](int u, int p) {
		up[u][0] = p;
		for(int i = 1; i < LG; i++) up[u][i] = up[up[u][i-1]][i-1];

		for(auto v : adj[u]) if (v != p) {
			dep[v] = dep[u] + 1; dfs(v, u);
		}
	};

	dep[0] = 0; dfs(0, 0);

	V<set<pi>> X(N);
	V<int> A(M), LCA(M-1); for(auto& x : A) { cin >> x; --x; }

	for(int i = 0; i < M; i++) {
		if (i+1 < M) X[(LCA[i] = lca(A[i], A[i+1]))].insert(mp(i, i+1));
		X[A[i]].insert(mp(i, i));
	}

	while(Q--) {
		int t; cin >> t;
		if (t == 1) {
			int i, x; cin >> i >> x; --i, --x;
			if (i-1 >= 0) X[LCA[i-1]].erase(mp(i-1, i));
			if (i+1 < M) X[LCA[i]].erase(mp(i, i+1));
			X[A[i]].erase(mp(i, i));

			A[i] = x;

			X[A[i]].insert(mp(i, i));
			if (i-1 >= 0) X[(LCA[i-1] = lca(A[i-1], A[i]))].insert(mp(i-1, i));
			if (i+1 < M) X[(LCA[i] = lca(A[i], A[i+1]))].insert(mp(i, i+1));
		} else {
			int l, r, x; cin >> l >> r >> x; --l, --r, --x;
			// --r; // BECAUSE NEED TO FIND i SUCH THAT l ≤ i < r

			// for(auto v : X[x]) cout << "(" << v.f << ", " << v.s  << ")" << nl;
			// cout << nl;

			auto it = X[x].lower_bound(mp(l, -1));
			if (it == end(X[x])) cout << -1 << " " << -1 << nl;
			else {
				int L, R; tie(L, R) = *it;
				if (R > r) L = R = -1;
				else ++L, ++R;
				cout << L << " " << R << nl;
			}
		}
	}




    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 0 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 0 ms 340 KB n=100
9 Correct 1 ms 340 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 0 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 0 ms 340 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 0 ms 340 KB n=100
23 Correct 0 ms 340 KB n=100
24 Correct 0 ms 340 KB n=100
25 Correct 0 ms 340 KB n=100
26 Correct 0 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 0 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 0 ms 340 KB n=100
9 Correct 1 ms 340 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 0 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 0 ms 340 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 0 ms 340 KB n=100
23 Correct 0 ms 340 KB n=100
24 Correct 0 ms 340 KB n=100
25 Correct 0 ms 340 KB n=100
26 Correct 0 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 1 ms 468 KB n=500
29 Correct 1 ms 468 KB n=500
30 Correct 1 ms 468 KB n=500
31 Correct 1 ms 468 KB n=500
32 Correct 1 ms 468 KB n=500
33 Correct 1 ms 468 KB n=500
34 Correct 2 ms 468 KB n=500
35 Correct 1 ms 468 KB n=500
36 Correct 1 ms 468 KB n=500
37 Correct 1 ms 468 KB n=500
38 Correct 1 ms 468 KB n=500
39 Correct 1 ms 468 KB n=500
40 Correct 1 ms 468 KB n=500
41 Correct 1 ms 468 KB n=500
42 Correct 1 ms 468 KB n=500
43 Correct 1 ms 468 KB n=500
44 Correct 1 ms 468 KB n=500
45 Correct 1 ms 468 KB n=500
46 Correct 1 ms 468 KB n=500
47 Correct 1 ms 468 KB n=500
48 Correct 1 ms 468 KB n=500
49 Correct 1 ms 468 KB n=500
50 Correct 2 ms 468 KB n=500
51 Correct 1 ms 468 KB n=500
52 Correct 1 ms 468 KB n=500
53 Correct 1 ms 468 KB n=500
54 Correct 1 ms 468 KB n=500
55 Correct 1 ms 340 KB n=278
56 Correct 1 ms 468 KB n=500
57 Correct 1 ms 468 KB n=500
58 Correct 1 ms 468 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 0 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 0 ms 340 KB n=100
9 Correct 1 ms 340 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 0 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 0 ms 340 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 0 ms 340 KB n=100
23 Correct 0 ms 340 KB n=100
24 Correct 0 ms 340 KB n=100
25 Correct 0 ms 340 KB n=100
26 Correct 0 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 1 ms 468 KB n=500
29 Correct 1 ms 468 KB n=500
30 Correct 1 ms 468 KB n=500
31 Correct 1 ms 468 KB n=500
32 Correct 1 ms 468 KB n=500
33 Correct 1 ms 468 KB n=500
34 Correct 2 ms 468 KB n=500
35 Correct 1 ms 468 KB n=500
36 Correct 1 ms 468 KB n=500
37 Correct 1 ms 468 KB n=500
38 Correct 1 ms 468 KB n=500
39 Correct 1 ms 468 KB n=500
40 Correct 1 ms 468 KB n=500
41 Correct 1 ms 468 KB n=500
42 Correct 1 ms 468 KB n=500
43 Correct 1 ms 468 KB n=500
44 Correct 1 ms 468 KB n=500
45 Correct 1 ms 468 KB n=500
46 Correct 1 ms 468 KB n=500
47 Correct 1 ms 468 KB n=500
48 Correct 1 ms 468 KB n=500
49 Correct 1 ms 468 KB n=500
50 Correct 2 ms 468 KB n=500
51 Correct 1 ms 468 KB n=500
52 Correct 1 ms 468 KB n=500
53 Correct 1 ms 468 KB n=500
54 Correct 1 ms 468 KB n=500
55 Correct 1 ms 340 KB n=278
56 Correct 1 ms 468 KB n=500
57 Correct 1 ms 468 KB n=500
58 Correct 1 ms 468 KB n=500
59 Correct 3 ms 852 KB n=2000
60 Correct 3 ms 980 KB n=2000
61 Correct 3 ms 980 KB n=2000
62 Correct 3 ms 852 KB n=2000
63 Correct 4 ms 852 KB n=2000
64 Correct 3 ms 968 KB n=2000
65 Correct 3 ms 852 KB n=2000
66 Correct 3 ms 988 KB n=2000
67 Correct 3 ms 852 KB n=2000
68 Correct 3 ms 980 KB n=2000
69 Correct 2 ms 852 KB n=2000
70 Correct 2 ms 852 KB n=2000
71 Correct 2 ms 852 KB n=2000
72 Correct 2 ms 852 KB n=2000
73 Correct 2 ms 852 KB n=2000
74 Correct 2 ms 852 KB n=1844
75 Correct 2 ms 852 KB n=2000
76 Correct 3 ms 852 KB n=2000
77 Correct 4 ms 852 KB n=2000
78 Correct 3 ms 852 KB n=2000
79 Correct 3 ms 852 KB n=2000
80 Correct 3 ms 980 KB n=2000
81 Correct 3 ms 852 KB n=2000
82 Correct 4 ms 852 KB n=2000
83 Correct 3 ms 980 KB n=2000
84 Correct 3 ms 852 KB n=2000
85 Correct 3 ms 852 KB n=2000
86 Correct 3 ms 852 KB n=2000
87 Correct 3 ms 852 KB n=2000
88 Correct 3 ms 980 KB n=2000
89 Correct 2 ms 980 KB n=2000
90 Correct 3 ms 980 KB n=2000
91 Correct 2 ms 852 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 0 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 0 ms 340 KB n=100
9 Correct 1 ms 340 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 0 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 0 ms 340 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 0 ms 340 KB n=100
23 Correct 0 ms 340 KB n=100
24 Correct 0 ms 340 KB n=100
25 Correct 0 ms 340 KB n=100
26 Correct 0 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 1 ms 468 KB n=500
29 Correct 1 ms 468 KB n=500
30 Correct 1 ms 468 KB n=500
31 Correct 1 ms 468 KB n=500
32 Correct 1 ms 468 KB n=500
33 Correct 1 ms 468 KB n=500
34 Correct 2 ms 468 KB n=500
35 Correct 1 ms 468 KB n=500
36 Correct 1 ms 468 KB n=500
37 Correct 1 ms 468 KB n=500
38 Correct 1 ms 468 KB n=500
39 Correct 1 ms 468 KB n=500
40 Correct 1 ms 468 KB n=500
41 Correct 1 ms 468 KB n=500
42 Correct 1 ms 468 KB n=500
43 Correct 1 ms 468 KB n=500
44 Correct 1 ms 468 KB n=500
45 Correct 1 ms 468 KB n=500
46 Correct 1 ms 468 KB n=500
47 Correct 1 ms 468 KB n=500
48 Correct 1 ms 468 KB n=500
49 Correct 1 ms 468 KB n=500
50 Correct 2 ms 468 KB n=500
51 Correct 1 ms 468 KB n=500
52 Correct 1 ms 468 KB n=500
53 Correct 1 ms 468 KB n=500
54 Correct 1 ms 468 KB n=500
55 Correct 1 ms 340 KB n=278
56 Correct 1 ms 468 KB n=500
57 Correct 1 ms 468 KB n=500
58 Correct 1 ms 468 KB n=500
59 Correct 3 ms 852 KB n=2000
60 Correct 3 ms 980 KB n=2000
61 Correct 3 ms 980 KB n=2000
62 Correct 3 ms 852 KB n=2000
63 Correct 4 ms 852 KB n=2000
64 Correct 3 ms 968 KB n=2000
65 Correct 3 ms 852 KB n=2000
66 Correct 3 ms 988 KB n=2000
67 Correct 3 ms 852 KB n=2000
68 Correct 3 ms 980 KB n=2000
69 Correct 2 ms 852 KB n=2000
70 Correct 2 ms 852 KB n=2000
71 Correct 2 ms 852 KB n=2000
72 Correct 2 ms 852 KB n=2000
73 Correct 2 ms 852 KB n=2000
74 Correct 2 ms 852 KB n=1844
75 Correct 2 ms 852 KB n=2000
76 Correct 3 ms 852 KB n=2000
77 Correct 4 ms 852 KB n=2000
78 Correct 3 ms 852 KB n=2000
79 Correct 3 ms 852 KB n=2000
80 Correct 3 ms 980 KB n=2000
81 Correct 3 ms 852 KB n=2000
82 Correct 4 ms 852 KB n=2000
83 Correct 3 ms 980 KB n=2000
84 Correct 3 ms 852 KB n=2000
85 Correct 3 ms 852 KB n=2000
86 Correct 3 ms 852 KB n=2000
87 Correct 3 ms 852 KB n=2000
88 Correct 3 ms 980 KB n=2000
89 Correct 2 ms 980 KB n=2000
90 Correct 3 ms 980 KB n=2000
91 Correct 2 ms 852 KB n=2000
92 Correct 546 ms 57852 KB n=200000
93 Correct 684 ms 66108 KB n=200000
94 Correct 654 ms 72888 KB n=200000
95 Correct 545 ms 57808 KB n=200000
96 Correct 532 ms 57664 KB n=200000
97 Correct 756 ms 64412 KB n=200000
98 Correct 543 ms 57732 KB n=200000
99 Correct 652 ms 57052 KB n=200000
100 Correct 554 ms 57816 KB n=200000
101 Correct 638 ms 75368 KB n=200000
102 Correct 311 ms 58672 KB n=200000
103 Correct 337 ms 58636 KB n=200000
104 Correct 312 ms 58772 KB n=200000
105 Correct 313 ms 58412 KB n=200000
106 Correct 310 ms 58444 KB n=200000
107 Correct 321 ms 58372 KB n=200000
108 Correct 551 ms 57364 KB n=200000
109 Correct 543 ms 57412 KB n=200000
110 Correct 561 ms 57424 KB n=200000
111 Correct 532 ms 57660 KB n=200000
112 Correct 598 ms 73380 KB n=200000
113 Correct 686 ms 64356 KB n=200000
114 Correct 549 ms 57632 KB n=200000
115 Correct 713 ms 60248 KB n=200000
116 Correct 514 ms 57416 KB n=200000
117 Correct 623 ms 74344 KB n=200000
118 Correct 661 ms 62156 KB n=200000
119 Correct 498 ms 57428 KB n=200000
120 Correct 553 ms 75200 KB n=200000
121 Correct 569 ms 75196 KB n=200000
122 Correct 559 ms 75644 KB n=200000
123 Correct 342 ms 58132 KB n=200000
124 Correct 100 ms 16252 KB n=25264