답안 #760344

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
760344 2023-06-17T13:08:57 Z NK_ Birthday gift (IZhO18_treearray) C++17
100 / 100
735 ms 75716 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; scanf("%d %d %d", &N, &M, &Q);
 
	V<V<int>> adj(N);
	for(int _ = 0; _ < N-1; _++) {
		int u, v; scanf("%d %d", &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) { scanf("%d", &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; scanf("%d", &t);
		if (t == 1) {
			int i, x; scanf("%d %d", &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; scanf("%d %d %d", &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])) printf("-1 -1\n");
			else {
				int L, R; tie(L, R) = *it;
				if (R > r) L = R = -1;
				else ++L, ++R;
				printf("%d %d\n", L, R);
			}
		}
	}
 
 
 
 
    return 0;
}

Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:46:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   46 |  int N, M, Q; scanf("%d %d %d", &N, &M, &Q);
      |               ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:50:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |   int u, v; scanf("%d %d", &u, &v); --u, --v;
      |             ~~~~~^~~~~~~~~~~~~~~~~
treearray.cpp:67:49: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |  V<int> A(M), LCA(M-1); for(auto& x : A) { scanf("%d", &x); --x; }
      |                                            ~~~~~^~~~~~~~~~
treearray.cpp:75:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |   int t; scanf("%d", &t);
      |          ~~~~~^~~~~~~~~~
treearray.cpp:77:19: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |    int i, x; scanf("%d %d", &i, &x); --i, --x;
      |              ~~~~~^~~~~~~~~~~~~~~~~
treearray.cpp:88:22: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |    int l, r, x; scanf("%d %d %d", &l, &r, &x); --l, --r, --x;
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB n=5
2 Correct 0 ms 340 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 0 ms 340 KB n=100
5 Correct 0 ms 340 KB n=100
6 Correct 0 ms 340 KB n=100
7 Correct 0 ms 340 KB n=100
8 Correct 0 ms 340 KB n=100
9 Correct 0 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 1 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 0 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 0 ms 340 KB n=100
18 Correct 0 ms 340 KB n=100
19 Correct 0 ms 340 KB n=100
20 Correct 0 ms 340 KB n=100
21 Correct 0 ms 340 KB n=100
22 Correct 0 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 0 ms 340 KB n=100
25 Correct 1 ms 340 KB n=100
26 Correct 0 ms 340 KB n=12
27 Correct 0 ms 340 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB n=5
2 Correct 0 ms 340 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 0 ms 340 KB n=100
5 Correct 0 ms 340 KB n=100
6 Correct 0 ms 340 KB n=100
7 Correct 0 ms 340 KB n=100
8 Correct 0 ms 340 KB n=100
9 Correct 0 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 1 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 0 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 0 ms 340 KB n=100
18 Correct 0 ms 340 KB n=100
19 Correct 0 ms 340 KB n=100
20 Correct 0 ms 340 KB n=100
21 Correct 0 ms 340 KB n=100
22 Correct 0 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 0 ms 340 KB n=100
25 Correct 1 ms 340 KB n=100
26 Correct 0 ms 340 KB n=12
27 Correct 0 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 1 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 1 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 0 ms 340 KB n=5
2 Correct 0 ms 340 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 0 ms 340 KB n=100
5 Correct 0 ms 340 KB n=100
6 Correct 0 ms 340 KB n=100
7 Correct 0 ms 340 KB n=100
8 Correct 0 ms 340 KB n=100
9 Correct 0 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 1 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 0 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 0 ms 340 KB n=100
18 Correct 0 ms 340 KB n=100
19 Correct 0 ms 340 KB n=100
20 Correct 0 ms 340 KB n=100
21 Correct 0 ms 340 KB n=100
22 Correct 0 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 0 ms 340 KB n=100
25 Correct 1 ms 340 KB n=100
26 Correct 0 ms 340 KB n=12
27 Correct 0 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 1 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 1 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 3 ms 852 KB n=2000
64 Correct 3 ms 852 KB n=2000
65 Correct 3 ms 852 KB n=2000
66 Correct 3 ms 980 KB n=2000
67 Correct 3 ms 852 KB n=2000
68 Correct 3 ms 944 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 3 ms 852 KB n=1844
75 Correct 2 ms 852 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 980 KB n=2000
81 Correct 3 ms 852 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 888 KB n=2000
86 Correct 3 ms 852 KB n=2000
87 Correct 4 ms 980 KB n=2000
88 Correct 3 ms 980 KB n=2000
89 Correct 4 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 0 ms 340 KB n=5
2 Correct 0 ms 340 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 0 ms 340 KB n=100
5 Correct 0 ms 340 KB n=100
6 Correct 0 ms 340 KB n=100
7 Correct 0 ms 340 KB n=100
8 Correct 0 ms 340 KB n=100
9 Correct 0 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 1 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 0 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 0 ms 340 KB n=100
18 Correct 0 ms 340 KB n=100
19 Correct 0 ms 340 KB n=100
20 Correct 0 ms 340 KB n=100
21 Correct 0 ms 340 KB n=100
22 Correct 0 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 0 ms 340 KB n=100
25 Correct 1 ms 340 KB n=100
26 Correct 0 ms 340 KB n=12
27 Correct 0 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 1 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 1 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 3 ms 852 KB n=2000
64 Correct 3 ms 852 KB n=2000
65 Correct 3 ms 852 KB n=2000
66 Correct 3 ms 980 KB n=2000
67 Correct 3 ms 852 KB n=2000
68 Correct 3 ms 944 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 3 ms 852 KB n=1844
75 Correct 2 ms 852 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 980 KB n=2000
81 Correct 3 ms 852 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 888 KB n=2000
86 Correct 3 ms 852 KB n=2000
87 Correct 4 ms 980 KB n=2000
88 Correct 3 ms 980 KB n=2000
89 Correct 4 ms 980 KB n=2000
90 Correct 3 ms 980 KB n=2000
91 Correct 2 ms 852 KB n=2000
92 Correct 575 ms 57840 KB n=200000
93 Correct 670 ms 66248 KB n=200000
94 Correct 647 ms 72920 KB n=200000
95 Correct 549 ms 57696 KB n=200000
96 Correct 569 ms 57648 KB n=200000
97 Correct 701 ms 64460 KB n=200000
98 Correct 569 ms 57664 KB n=200000
99 Correct 713 ms 57048 KB n=200000
100 Correct 564 ms 57752 KB n=200000
101 Correct 621 ms 75368 KB n=200000
102 Correct 336 ms 58700 KB n=200000
103 Correct 337 ms 58632 KB n=200000
104 Correct 337 ms 58700 KB n=200000
105 Correct 332 ms 58444 KB n=200000
106 Correct 362 ms 58432 KB n=200000
107 Correct 351 ms 58456 KB n=200000
108 Correct 589 ms 57540 KB n=200000
109 Correct 603 ms 57488 KB n=200000
110 Correct 668 ms 57476 KB n=200000
111 Correct 564 ms 57752 KB n=200000
112 Correct 706 ms 73448 KB n=200000
113 Correct 733 ms 64332 KB n=200000
114 Correct 566 ms 57624 KB n=200000
115 Correct 715 ms 60304 KB n=200000
116 Correct 548 ms 57588 KB n=200000
117 Correct 673 ms 74220 KB n=200000
118 Correct 735 ms 62124 KB n=200000
119 Correct 544 ms 57376 KB n=200000
120 Correct 633 ms 75248 KB n=200000
121 Correct 627 ms 75200 KB n=200000
122 Correct 603 ms 75716 KB n=200000
123 Correct 367 ms 58012 KB n=200000
124 Correct 107 ms 16204 KB n=25264