답안 #760335

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
760335 2023-06-17T12:58:35 Z NK_ Birthday gift (IZhO18_treearray) C++17
100 / 100
1010 ms 83860 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 = 20;

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); for(auto& x : A) { cin >> x; --x; }

	for(int i = 0; i < M; i++) {
		if (i+1 < M) X[lca(A[i], A[i+1])].insert(mp(i, i+1));
		X[A[i]].insert(mp(i, i));
	}
	
	auto upd = [&]() {
		int i, x; cin >> i >> x; --i, --x;
		if (i-1 >= 0) X[lca(A[i-1], A[i])].erase(mp(i-1, i));
		if (i+1 < M) X[lca(A[i], A[i+1])].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(A[i-1], A[i])].insert(mp(i-1, i));
		if (i+1 < M) X[lca(A[i], A[i+1])].insert(mp(i, i+1));
	};	


	while(Q--) {
		int t; cin >> t;
		if (t == 1) upd();
		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 1 ms 332 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 1 ms 340 KB n=100
9 Correct 1 ms 328 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 1 ms 328 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 1 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 292 KB n=100
20 Correct 1 ms 324 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 1 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 340 KB n=100
26 Correct 1 ms 340 KB n=12
27 Correct 1 ms 332 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 1 ms 332 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 1 ms 340 KB n=100
9 Correct 1 ms 328 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 1 ms 328 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 1 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 292 KB n=100
20 Correct 1 ms 324 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 1 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 340 KB n=100
26 Correct 1 ms 340 KB n=12
27 Correct 1 ms 332 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 1 ms 464 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 460 KB n=500
45 Correct 1 ms 468 KB n=500
46 Correct 1 ms 472 KB n=500
47 Correct 1 ms 472 KB n=500
48 Correct 1 ms 468 KB n=500
49 Correct 1 ms 468 KB n=500
50 Correct 1 ms 464 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 496 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 1 ms 332 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 1 ms 340 KB n=100
9 Correct 1 ms 328 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 1 ms 328 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 1 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 292 KB n=100
20 Correct 1 ms 324 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 1 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 340 KB n=100
26 Correct 1 ms 340 KB n=12
27 Correct 1 ms 332 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 1 ms 464 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 460 KB n=500
45 Correct 1 ms 468 KB n=500
46 Correct 1 ms 472 KB n=500
47 Correct 1 ms 472 KB n=500
48 Correct 1 ms 468 KB n=500
49 Correct 1 ms 468 KB n=500
50 Correct 1 ms 464 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 496 KB n=500
58 Correct 1 ms 468 KB n=500
59 Correct 3 ms 852 KB n=2000
60 Correct 3 ms 1108 KB n=2000
61 Correct 4 ms 980 KB n=2000
62 Correct 4 ms 984 KB n=2000
63 Correct 3 ms 852 KB n=2000
64 Correct 3 ms 980 KB n=2000
65 Correct 3 ms 852 KB n=2000
66 Correct 3 ms 980 KB n=2000
67 Correct 4 ms 852 KB n=2000
68 Correct 4 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 840 KB n=2000
72 Correct 2 ms 852 KB n=2000
73 Correct 2 ms 852 KB n=2000
74 Correct 3 ms 852 KB n=1844
75 Correct 2 ms 936 KB n=2000
76 Correct 3 ms 852 KB n=2000
77 Correct 3 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 1108 KB n=2000
81 Correct 3 ms 984 KB n=2000
82 Correct 3 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 980 KB n=2000
86 Correct 4 ms 1112 KB n=2000
87 Correct 3 ms 852 KB n=2000
88 Correct 3 ms 1108 KB n=2000
89 Correct 3 ms 1108 KB n=2000
90 Correct 3 ms 1108 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 1 ms 332 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 1 ms 340 KB n=100
9 Correct 1 ms 328 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 1 ms 328 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 1 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 292 KB n=100
20 Correct 1 ms 324 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 1 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 340 KB n=100
26 Correct 1 ms 340 KB n=12
27 Correct 1 ms 332 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 1 ms 464 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 460 KB n=500
45 Correct 1 ms 468 KB n=500
46 Correct 1 ms 472 KB n=500
47 Correct 1 ms 472 KB n=500
48 Correct 1 ms 468 KB n=500
49 Correct 1 ms 468 KB n=500
50 Correct 1 ms 464 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 496 KB n=500
58 Correct 1 ms 468 KB n=500
59 Correct 3 ms 852 KB n=2000
60 Correct 3 ms 1108 KB n=2000
61 Correct 4 ms 980 KB n=2000
62 Correct 4 ms 984 KB n=2000
63 Correct 3 ms 852 KB n=2000
64 Correct 3 ms 980 KB n=2000
65 Correct 3 ms 852 KB n=2000
66 Correct 3 ms 980 KB n=2000
67 Correct 4 ms 852 KB n=2000
68 Correct 4 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 840 KB n=2000
72 Correct 2 ms 852 KB n=2000
73 Correct 2 ms 852 KB n=2000
74 Correct 3 ms 852 KB n=1844
75 Correct 2 ms 936 KB n=2000
76 Correct 3 ms 852 KB n=2000
77 Correct 3 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 1108 KB n=2000
81 Correct 3 ms 984 KB n=2000
82 Correct 3 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 980 KB n=2000
86 Correct 4 ms 1112 KB n=2000
87 Correct 3 ms 852 KB n=2000
88 Correct 3 ms 1108 KB n=2000
89 Correct 3 ms 1108 KB n=2000
90 Correct 3 ms 1108 KB n=2000
91 Correct 2 ms 852 KB n=2000
92 Correct 606 ms 65084 KB n=200000
93 Correct 841 ms 74680 KB n=200000
94 Correct 803 ms 81484 KB n=200000
95 Correct 622 ms 64960 KB n=200000
96 Correct 586 ms 64952 KB n=200000
97 Correct 803 ms 72780 KB n=200000
98 Correct 585 ms 64916 KB n=200000
99 Correct 745 ms 65128 KB n=200000
100 Correct 654 ms 64988 KB n=200000
101 Correct 1010 ms 83860 KB n=200000
102 Correct 359 ms 66084 KB n=200000
103 Correct 344 ms 66192 KB n=200000
104 Correct 352 ms 66176 KB n=200000
105 Correct 374 ms 66572 KB n=200000
106 Correct 312 ms 66564 KB n=200000
107 Correct 345 ms 66708 KB n=200000
108 Correct 643 ms 65116 KB n=200000
109 Correct 647 ms 64972 KB n=200000
110 Correct 672 ms 65036 KB n=200000
111 Correct 557 ms 64440 KB n=200000
112 Correct 768 ms 81868 KB n=200000
113 Correct 855 ms 72848 KB n=200000
114 Correct 599 ms 64488 KB n=200000
115 Correct 967 ms 68616 KB n=200000
116 Correct 578 ms 65068 KB n=200000
117 Correct 765 ms 82772 KB n=200000
118 Correct 869 ms 70684 KB n=200000
119 Correct 603 ms 65152 KB n=200000
120 Correct 782 ms 83012 KB n=200000
121 Correct 741 ms 82928 KB n=200000
122 Correct 684 ms 83352 KB n=200000
123 Correct 338 ms 66332 KB n=200000
124 Correct 137 ms 18000 KB n=25264