답안 #1109141

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1109141 2024-11-06T05:35:25 Z Muhammet Birthday gift (IZhO18_treearray) C++17
56 / 100
4000 ms 48968 KB
#include <bits/stdc++.h>

using namespace std;

vector <vector <int>> v, sp;

vector <int> h, p;

void dfs(int x, int y){
	h[x] = h[y]+1;
	p[x] = y;
	for(auto i : v[x]){
		if(i != y){
			dfs(i,x);
		}
	}
}

int lca(int x, int y){
	if(h[x] < h[y]) swap(x,y);
	for(int i = 20; i >= 0; i--){
		if(h[sp[x][i]] >= h[y]){
			x = sp[x][i];
		}
	}
	for(int i = 20; i >= 0; i--){
		if(sp[x][i] != sp[y][i]){
			x = sp[x][i];
			y = sp[y][i];
		}
	}
	if(x == y) return x;
	return p[x];
}

int main(){
	int n, m, q;
	cin >> n >> m >> q;
	v.resize(n+1);
	h.resize(n+1,0);
	for(int i = 1; i < n; i++){
		int u1, u2;
		cin >> u1 >> u2;
		v[u1].push_back(u2);
		v[u2].push_back(u1);
	}
	p.resize(n+1);
	sp.resize(n+1, vector <int> (25,0));
	dfs(1,0);
	for(int i = 1; i <= n; i++){
		sp[i][0] = p[i];
	}
	for(int j = 1; j <= 20; j++){
		for(int i = 1; i <= n; i++){
			sp[i][j] = sp[sp[i][j-1]][j-1];
		}
	}
	vector <int> a(m+1);
	for(int i = 1; i <= m; i++){
		cin >> a[i];
	}
	for(int i = 1; i <= q; i++){
		int t;
		cin >> t;
		if(t == 1){
			int in, vl;
			cin >> in >> vl;
			a[in] = vl;
		}
		else {
			int l, r, vl;
			cin >> l >> r >> vl;
			bool tr = 0;
			for(int j = l; j < r; j++){
				if(a[j] == vl){
					cout << j << ' ' << j << "\n";
					tr = 1;
					break;
				}
				if(lca(a[j],a[j+1]) == vl){
					tr = 1;
					cout << j << ' ' << j+1 << '\n';
					break;
				}
			}
			if(tr == 0){
				if(a[r] == vl){
					cout << r << ' ' << r << '\n';
				}
				else {
					cout << "-1 -1\n";
				}
			}
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB n=5
2 Correct 1 ms 336 KB n=100
3 Correct 1 ms 336 KB n=100
4 Correct 1 ms 336 KB n=100
5 Correct 1 ms 336 KB n=100
6 Correct 2 ms 336 KB n=100
7 Correct 2 ms 336 KB n=100
8 Correct 2 ms 592 KB n=100
9 Correct 1 ms 500 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 504 KB n=100
16 Correct 1 ms 336 KB n=100
17 Correct 1 ms 336 KB n=100
18 Correct 1 ms 336 KB n=100
19 Correct 1 ms 336 KB n=100
20 Correct 1 ms 336 KB n=100
21 Correct 1 ms 336 KB n=100
22 Correct 1 ms 336 KB n=100
23 Correct 1 ms 336 KB n=100
24 Correct 1 ms 336 KB n=100
25 Correct 1 ms 336 KB n=100
26 Correct 1 ms 336 KB n=12
27 Correct 2 ms 336 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB n=5
2 Correct 1 ms 336 KB n=100
3 Correct 1 ms 336 KB n=100
4 Correct 1 ms 336 KB n=100
5 Correct 1 ms 336 KB n=100
6 Correct 2 ms 336 KB n=100
7 Correct 2 ms 336 KB n=100
8 Correct 2 ms 592 KB n=100
9 Correct 1 ms 500 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 504 KB n=100
16 Correct 1 ms 336 KB n=100
17 Correct 1 ms 336 KB n=100
18 Correct 1 ms 336 KB n=100
19 Correct 1 ms 336 KB n=100
20 Correct 1 ms 336 KB n=100
21 Correct 1 ms 336 KB n=100
22 Correct 1 ms 336 KB n=100
23 Correct 1 ms 336 KB n=100
24 Correct 1 ms 336 KB n=100
25 Correct 1 ms 336 KB n=100
26 Correct 1 ms 336 KB n=12
27 Correct 2 ms 336 KB n=100
28 Correct 2 ms 336 KB n=500
29 Correct 5 ms 336 KB n=500
30 Correct 4 ms 336 KB n=500
31 Correct 4 ms 336 KB n=500
32 Correct 2 ms 336 KB n=500
33 Correct 4 ms 336 KB n=500
34 Correct 2 ms 336 KB n=500
35 Correct 4 ms 336 KB n=500
36 Correct 15 ms 336 KB n=500
37 Correct 18 ms 336 KB n=500
38 Correct 15 ms 336 KB n=500
39 Correct 9 ms 336 KB n=500
40 Correct 8 ms 336 KB n=500
41 Correct 9 ms 336 KB n=500
42 Correct 8 ms 336 KB n=500
43 Correct 8 ms 336 KB n=500
44 Correct 8 ms 336 KB n=500
45 Correct 2 ms 336 KB n=500
46 Correct 6 ms 592 KB n=500
47 Correct 4 ms 336 KB n=500
48 Correct 2 ms 336 KB n=500
49 Correct 4 ms 336 KB n=500
50 Correct 5 ms 336 KB n=500
51 Correct 5 ms 336 KB n=500
52 Correct 6 ms 336 KB n=500
53 Correct 6 ms 336 KB n=500
54 Correct 8 ms 336 KB n=500
55 Correct 1 ms 336 KB n=278
56 Correct 13 ms 336 KB n=500
57 Correct 13 ms 504 KB n=500
58 Correct 14 ms 592 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB n=5
2 Correct 1 ms 336 KB n=100
3 Correct 1 ms 336 KB n=100
4 Correct 1 ms 336 KB n=100
5 Correct 1 ms 336 KB n=100
6 Correct 2 ms 336 KB n=100
7 Correct 2 ms 336 KB n=100
8 Correct 2 ms 592 KB n=100
9 Correct 1 ms 500 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 504 KB n=100
16 Correct 1 ms 336 KB n=100
17 Correct 1 ms 336 KB n=100
18 Correct 1 ms 336 KB n=100
19 Correct 1 ms 336 KB n=100
20 Correct 1 ms 336 KB n=100
21 Correct 1 ms 336 KB n=100
22 Correct 1 ms 336 KB n=100
23 Correct 1 ms 336 KB n=100
24 Correct 1 ms 336 KB n=100
25 Correct 1 ms 336 KB n=100
26 Correct 1 ms 336 KB n=12
27 Correct 2 ms 336 KB n=100
28 Correct 2 ms 336 KB n=500
29 Correct 5 ms 336 KB n=500
30 Correct 4 ms 336 KB n=500
31 Correct 4 ms 336 KB n=500
32 Correct 2 ms 336 KB n=500
33 Correct 4 ms 336 KB n=500
34 Correct 2 ms 336 KB n=500
35 Correct 4 ms 336 KB n=500
36 Correct 15 ms 336 KB n=500
37 Correct 18 ms 336 KB n=500
38 Correct 15 ms 336 KB n=500
39 Correct 9 ms 336 KB n=500
40 Correct 8 ms 336 KB n=500
41 Correct 9 ms 336 KB n=500
42 Correct 8 ms 336 KB n=500
43 Correct 8 ms 336 KB n=500
44 Correct 8 ms 336 KB n=500
45 Correct 2 ms 336 KB n=500
46 Correct 6 ms 592 KB n=500
47 Correct 4 ms 336 KB n=500
48 Correct 2 ms 336 KB n=500
49 Correct 4 ms 336 KB n=500
50 Correct 5 ms 336 KB n=500
51 Correct 5 ms 336 KB n=500
52 Correct 6 ms 336 KB n=500
53 Correct 6 ms 336 KB n=500
54 Correct 8 ms 336 KB n=500
55 Correct 1 ms 336 KB n=278
56 Correct 13 ms 336 KB n=500
57 Correct 13 ms 504 KB n=500
58 Correct 14 ms 592 KB n=500
59 Correct 7 ms 848 KB n=2000
60 Correct 56 ms 848 KB n=2000
61 Correct 53 ms 952 KB n=2000
62 Correct 36 ms 848 KB n=2000
63 Correct 8 ms 968 KB n=2000
64 Correct 45 ms 848 KB n=2000
65 Correct 5 ms 848 KB n=2000
66 Correct 59 ms 968 KB n=2000
67 Correct 7 ms 848 KB n=2000
68 Correct 49 ms 944 KB n=2000
69 Correct 231 ms 848 KB n=2000
70 Correct 225 ms 848 KB n=2000
71 Correct 223 ms 848 KB n=2000
72 Correct 114 ms 848 KB n=2000
73 Correct 114 ms 904 KB n=2000
74 Correct 4 ms 848 KB n=1844
75 Correct 116 ms 860 KB n=2000
76 Correct 114 ms 848 KB n=2000
77 Correct 120 ms 848 KB n=2000
78 Correct 118 ms 848 KB n=2000
79 Correct 5 ms 848 KB n=2000
80 Correct 54 ms 1020 KB n=2000
81 Correct 42 ms 848 KB n=2000
82 Correct 5 ms 848 KB n=2000
83 Correct 49 ms 848 KB n=2000
84 Correct 60 ms 848 KB n=2000
85 Correct 85 ms 848 KB n=2000
86 Correct 88 ms 848 KB n=2000
87 Correct 65 ms 848 KB n=2000
88 Correct 323 ms 848 KB n=2000
89 Correct 319 ms 848 KB n=2000
90 Correct 141 ms 848 KB n=2000
91 Correct 175 ms 896 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB n=5
2 Correct 1 ms 336 KB n=100
3 Correct 1 ms 336 KB n=100
4 Correct 1 ms 336 KB n=100
5 Correct 1 ms 336 KB n=100
6 Correct 2 ms 336 KB n=100
7 Correct 2 ms 336 KB n=100
8 Correct 2 ms 592 KB n=100
9 Correct 1 ms 500 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 504 KB n=100
16 Correct 1 ms 336 KB n=100
17 Correct 1 ms 336 KB n=100
18 Correct 1 ms 336 KB n=100
19 Correct 1 ms 336 KB n=100
20 Correct 1 ms 336 KB n=100
21 Correct 1 ms 336 KB n=100
22 Correct 1 ms 336 KB n=100
23 Correct 1 ms 336 KB n=100
24 Correct 1 ms 336 KB n=100
25 Correct 1 ms 336 KB n=100
26 Correct 1 ms 336 KB n=12
27 Correct 2 ms 336 KB n=100
28 Correct 2 ms 336 KB n=500
29 Correct 5 ms 336 KB n=500
30 Correct 4 ms 336 KB n=500
31 Correct 4 ms 336 KB n=500
32 Correct 2 ms 336 KB n=500
33 Correct 4 ms 336 KB n=500
34 Correct 2 ms 336 KB n=500
35 Correct 4 ms 336 KB n=500
36 Correct 15 ms 336 KB n=500
37 Correct 18 ms 336 KB n=500
38 Correct 15 ms 336 KB n=500
39 Correct 9 ms 336 KB n=500
40 Correct 8 ms 336 KB n=500
41 Correct 9 ms 336 KB n=500
42 Correct 8 ms 336 KB n=500
43 Correct 8 ms 336 KB n=500
44 Correct 8 ms 336 KB n=500
45 Correct 2 ms 336 KB n=500
46 Correct 6 ms 592 KB n=500
47 Correct 4 ms 336 KB n=500
48 Correct 2 ms 336 KB n=500
49 Correct 4 ms 336 KB n=500
50 Correct 5 ms 336 KB n=500
51 Correct 5 ms 336 KB n=500
52 Correct 6 ms 336 KB n=500
53 Correct 6 ms 336 KB n=500
54 Correct 8 ms 336 KB n=500
55 Correct 1 ms 336 KB n=278
56 Correct 13 ms 336 KB n=500
57 Correct 13 ms 504 KB n=500
58 Correct 14 ms 592 KB n=500
59 Correct 7 ms 848 KB n=2000
60 Correct 56 ms 848 KB n=2000
61 Correct 53 ms 952 KB n=2000
62 Correct 36 ms 848 KB n=2000
63 Correct 8 ms 968 KB n=2000
64 Correct 45 ms 848 KB n=2000
65 Correct 5 ms 848 KB n=2000
66 Correct 59 ms 968 KB n=2000
67 Correct 7 ms 848 KB n=2000
68 Correct 49 ms 944 KB n=2000
69 Correct 231 ms 848 KB n=2000
70 Correct 225 ms 848 KB n=2000
71 Correct 223 ms 848 KB n=2000
72 Correct 114 ms 848 KB n=2000
73 Correct 114 ms 904 KB n=2000
74 Correct 4 ms 848 KB n=1844
75 Correct 116 ms 860 KB n=2000
76 Correct 114 ms 848 KB n=2000
77 Correct 120 ms 848 KB n=2000
78 Correct 118 ms 848 KB n=2000
79 Correct 5 ms 848 KB n=2000
80 Correct 54 ms 1020 KB n=2000
81 Correct 42 ms 848 KB n=2000
82 Correct 5 ms 848 KB n=2000
83 Correct 49 ms 848 KB n=2000
84 Correct 60 ms 848 KB n=2000
85 Correct 85 ms 848 KB n=2000
86 Correct 88 ms 848 KB n=2000
87 Correct 65 ms 848 KB n=2000
88 Correct 323 ms 848 KB n=2000
89 Correct 319 ms 848 KB n=2000
90 Correct 141 ms 848 KB n=2000
91 Correct 175 ms 896 KB n=2000
92 Correct 899 ms 48672 KB n=200000
93 Execution timed out 4067 ms 48968 KB Time limit exceeded
94 Halted 0 ms 0 KB -