답안 #339997

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
339997 2020-12-26T14:01:25 Z wind_reaper Birthday gift (IZhO18_treearray) C++17
100 / 100
1393 ms 83436 KB
#include <bits/stdc++.h>

using namespace std;

int n, l, timer;
vector<vector<int>> adj, up;
vector<int> tin, tout;

void dfs(int node, int p){
	tin[node] = ++timer;

	up[node][0] = p;

	for(int i = 1; i <= l; i++)
		up[node][i] = up[up[node][i-1]][i-1];

	for(int v : adj[node])
		if(v != p)
			dfs(v, node);

	tout[node] = ++timer;
}
bool is_anc(int u, int v){
	return (tin[u] <= tin[v] && tout[u] >= tout[v]);
}
int lca(int u, int v){
	if(is_anc(u, v))
		return u;
	if(is_anc(v, u))
		return v;

	for(int i = l; i >= 0; --i)
		if(!is_anc(up[u][i], v))
			u = up[u][i];

	return up[u][0];
}
void preprocess(int root) {
    tin.resize(n);
    tout.resize(n);
    timer = 0;
    l = ceil(log2(n));
    up.assign(n, vector<int>(l + 1));
    dfs(root, root);
}
int32_t main(){
	ios_base::sync_with_stdio(false); 
	cin.tie(NULL); 
	int m, q; 
	cin >> n >> m >> q;

	adj.resize(n); 

	for(int i = 0; i < n - 1; i++){
		int u, v;
		cin >> u >> v;
		--u, --v; 
		adj[u].push_back(v); 
		adj[v].push_back(u); 
	}

	preprocess(0); 

	vector<multiset<int>> lcas(n); 	
	vector<int> arr(m); 

	for(int i = 0; i < m; i++){
		cin >> arr[i]; 
		--arr[i]; 
		lcas[arr[i]].insert(i); 
	}

	for(int i = 0; i < m - 1; i++){
		int r = lca(arr[i], arr[i+1]); 
		lcas[r].insert(i); 
	}

	for(int Q = 0; Q < q; Q++){
		int type;
		cin >> type; 
		if(type == 1){
			int i, v;
			cin >> i >> v; 
			--i, --v; 
			
			if(i > 0){
				int r = lca(arr[i], arr[i-1]); 
				lcas[r].erase(lcas[r].find(i-1)); 
				lcas[lca(arr[i-1], v)].insert(i-1); 
			}
			if(i < m -1){
				int r = lca(arr[i], arr[i+1]); 
				lcas[r].erase(lcas[r].find(i)); 
				lcas[lca(arr[i+1], v)].insert(i); 
			} 
			lcas[arr[i]].erase(lcas[arr[i]].find(i)); 
			arr[i] = v; 
			lcas[v].insert(i); 
		}
		else{
			int l, r, v;
			cin >> l >> r >> v; 
			--l, --r, --v; 	
			auto it = lcas[v].lower_bound(l); 
			int ax = -2, ay = -2; 
			if(it != lcas[v].end() && *it <= r){
				int idx = *it; 
				if(arr[idx] == v)
					ax = idx, ay = idx; 
				else if(idx < r)
					ax = idx, ay = ax + 1; 
			}
			cout << ax+1 << ' ' << ay+1 << '\n';
		}
	}
	return 0; 
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB n=5
2 Correct 1 ms 364 KB n=100
3 Correct 1 ms 364 KB n=100
4 Correct 1 ms 364 KB n=100
5 Correct 1 ms 364 KB n=100
6 Correct 1 ms 364 KB n=100
7 Correct 1 ms 364 KB n=100
8 Correct 1 ms 364 KB n=100
9 Correct 1 ms 364 KB n=100
10 Correct 1 ms 364 KB n=100
11 Correct 1 ms 364 KB n=100
12 Correct 1 ms 364 KB n=100
13 Correct 1 ms 364 KB n=100
14 Correct 1 ms 364 KB n=100
15 Correct 1 ms 364 KB n=100
16 Correct 1 ms 364 KB n=100
17 Correct 1 ms 364 KB n=100
18 Correct 1 ms 364 KB n=100
19 Correct 1 ms 364 KB n=100
20 Correct 1 ms 364 KB n=100
21 Correct 1 ms 364 KB n=100
22 Correct 1 ms 364 KB n=100
23 Correct 1 ms 364 KB n=100
24 Correct 1 ms 364 KB n=100
25 Correct 1 ms 364 KB n=100
26 Correct 1 ms 364 KB n=12
27 Correct 1 ms 364 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB n=5
2 Correct 1 ms 364 KB n=100
3 Correct 1 ms 364 KB n=100
4 Correct 1 ms 364 KB n=100
5 Correct 1 ms 364 KB n=100
6 Correct 1 ms 364 KB n=100
7 Correct 1 ms 364 KB n=100
8 Correct 1 ms 364 KB n=100
9 Correct 1 ms 364 KB n=100
10 Correct 1 ms 364 KB n=100
11 Correct 1 ms 364 KB n=100
12 Correct 1 ms 364 KB n=100
13 Correct 1 ms 364 KB n=100
14 Correct 1 ms 364 KB n=100
15 Correct 1 ms 364 KB n=100
16 Correct 1 ms 364 KB n=100
17 Correct 1 ms 364 KB n=100
18 Correct 1 ms 364 KB n=100
19 Correct 1 ms 364 KB n=100
20 Correct 1 ms 364 KB n=100
21 Correct 1 ms 364 KB n=100
22 Correct 1 ms 364 KB n=100
23 Correct 1 ms 364 KB n=100
24 Correct 1 ms 364 KB n=100
25 Correct 1 ms 364 KB n=100
26 Correct 1 ms 364 KB n=12
27 Correct 1 ms 364 KB n=100
28 Correct 1 ms 492 KB n=500
29 Correct 1 ms 492 KB n=500
30 Correct 2 ms 492 KB n=500
31 Correct 1 ms 492 KB n=500
32 Correct 1 ms 492 KB n=500
33 Correct 1 ms 492 KB n=500
34 Correct 2 ms 492 KB n=500
35 Correct 1 ms 492 KB n=500
36 Correct 1 ms 492 KB n=500
37 Correct 1 ms 492 KB n=500
38 Correct 1 ms 492 KB n=500
39 Correct 1 ms 492 KB n=500
40 Correct 1 ms 492 KB n=500
41 Correct 1 ms 492 KB n=500
42 Correct 1 ms 492 KB n=500
43 Correct 2 ms 492 KB n=500
44 Correct 2 ms 492 KB n=500
45 Correct 2 ms 492 KB n=500
46 Correct 1 ms 492 KB n=500
47 Correct 1 ms 492 KB n=500
48 Correct 1 ms 492 KB n=500
49 Correct 1 ms 492 KB n=500
50 Correct 1 ms 492 KB n=500
51 Correct 1 ms 492 KB n=500
52 Correct 1 ms 492 KB n=500
53 Correct 1 ms 492 KB n=500
54 Correct 1 ms 492 KB n=500
55 Correct 1 ms 512 KB n=278
56 Correct 1 ms 492 KB n=500
57 Correct 1 ms 492 KB n=500
58 Correct 1 ms 492 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB n=5
2 Correct 1 ms 364 KB n=100
3 Correct 1 ms 364 KB n=100
4 Correct 1 ms 364 KB n=100
5 Correct 1 ms 364 KB n=100
6 Correct 1 ms 364 KB n=100
7 Correct 1 ms 364 KB n=100
8 Correct 1 ms 364 KB n=100
9 Correct 1 ms 364 KB n=100
10 Correct 1 ms 364 KB n=100
11 Correct 1 ms 364 KB n=100
12 Correct 1 ms 364 KB n=100
13 Correct 1 ms 364 KB n=100
14 Correct 1 ms 364 KB n=100
15 Correct 1 ms 364 KB n=100
16 Correct 1 ms 364 KB n=100
17 Correct 1 ms 364 KB n=100
18 Correct 1 ms 364 KB n=100
19 Correct 1 ms 364 KB n=100
20 Correct 1 ms 364 KB n=100
21 Correct 1 ms 364 KB n=100
22 Correct 1 ms 364 KB n=100
23 Correct 1 ms 364 KB n=100
24 Correct 1 ms 364 KB n=100
25 Correct 1 ms 364 KB n=100
26 Correct 1 ms 364 KB n=12
27 Correct 1 ms 364 KB n=100
28 Correct 1 ms 492 KB n=500
29 Correct 1 ms 492 KB n=500
30 Correct 2 ms 492 KB n=500
31 Correct 1 ms 492 KB n=500
32 Correct 1 ms 492 KB n=500
33 Correct 1 ms 492 KB n=500
34 Correct 2 ms 492 KB n=500
35 Correct 1 ms 492 KB n=500
36 Correct 1 ms 492 KB n=500
37 Correct 1 ms 492 KB n=500
38 Correct 1 ms 492 KB n=500
39 Correct 1 ms 492 KB n=500
40 Correct 1 ms 492 KB n=500
41 Correct 1 ms 492 KB n=500
42 Correct 1 ms 492 KB n=500
43 Correct 2 ms 492 KB n=500
44 Correct 2 ms 492 KB n=500
45 Correct 2 ms 492 KB n=500
46 Correct 1 ms 492 KB n=500
47 Correct 1 ms 492 KB n=500
48 Correct 1 ms 492 KB n=500
49 Correct 1 ms 492 KB n=500
50 Correct 1 ms 492 KB n=500
51 Correct 1 ms 492 KB n=500
52 Correct 1 ms 492 KB n=500
53 Correct 1 ms 492 KB n=500
54 Correct 1 ms 492 KB n=500
55 Correct 1 ms 512 KB n=278
56 Correct 1 ms 492 KB n=500
57 Correct 1 ms 492 KB n=500
58 Correct 1 ms 492 KB n=500
59 Correct 4 ms 1004 KB n=2000
60 Correct 4 ms 1132 KB n=2000
61 Correct 4 ms 1004 KB n=2000
62 Correct 4 ms 1004 KB n=2000
63 Correct 5 ms 1004 KB n=2000
64 Correct 4 ms 1004 KB n=2000
65 Correct 4 ms 1004 KB n=2000
66 Correct 4 ms 1132 KB n=2000
67 Correct 6 ms 1004 KB n=2000
68 Correct 4 ms 1004 KB n=2000
69 Correct 3 ms 1004 KB n=2000
70 Correct 4 ms 1004 KB n=2000
71 Correct 5 ms 1004 KB n=2000
72 Correct 4 ms 1004 KB n=2000
73 Correct 4 ms 1004 KB n=2000
74 Correct 4 ms 1004 KB n=1844
75 Correct 5 ms 1132 KB n=2000
76 Correct 4 ms 1004 KB n=2000
77 Correct 4 ms 1004 KB n=2000
78 Correct 4 ms 1004 KB n=2000
79 Correct 4 ms 1004 KB n=2000
80 Correct 4 ms 1132 KB n=2000
81 Correct 4 ms 1004 KB n=2000
82 Correct 4 ms 1004 KB n=2000
83 Correct 4 ms 1004 KB n=2000
84 Correct 4 ms 1004 KB n=2000
85 Correct 4 ms 1004 KB n=2000
86 Correct 4 ms 1004 KB n=2000
87 Correct 4 ms 1004 KB n=2000
88 Correct 4 ms 1152 KB n=2000
89 Correct 3 ms 1132 KB n=2000
90 Correct 4 ms 1132 KB n=2000
91 Correct 3 ms 1004 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB n=5
2 Correct 1 ms 364 KB n=100
3 Correct 1 ms 364 KB n=100
4 Correct 1 ms 364 KB n=100
5 Correct 1 ms 364 KB n=100
6 Correct 1 ms 364 KB n=100
7 Correct 1 ms 364 KB n=100
8 Correct 1 ms 364 KB n=100
9 Correct 1 ms 364 KB n=100
10 Correct 1 ms 364 KB n=100
11 Correct 1 ms 364 KB n=100
12 Correct 1 ms 364 KB n=100
13 Correct 1 ms 364 KB n=100
14 Correct 1 ms 364 KB n=100
15 Correct 1 ms 364 KB n=100
16 Correct 1 ms 364 KB n=100
17 Correct 1 ms 364 KB n=100
18 Correct 1 ms 364 KB n=100
19 Correct 1 ms 364 KB n=100
20 Correct 1 ms 364 KB n=100
21 Correct 1 ms 364 KB n=100
22 Correct 1 ms 364 KB n=100
23 Correct 1 ms 364 KB n=100
24 Correct 1 ms 364 KB n=100
25 Correct 1 ms 364 KB n=100
26 Correct 1 ms 364 KB n=12
27 Correct 1 ms 364 KB n=100
28 Correct 1 ms 492 KB n=500
29 Correct 1 ms 492 KB n=500
30 Correct 2 ms 492 KB n=500
31 Correct 1 ms 492 KB n=500
32 Correct 1 ms 492 KB n=500
33 Correct 1 ms 492 KB n=500
34 Correct 2 ms 492 KB n=500
35 Correct 1 ms 492 KB n=500
36 Correct 1 ms 492 KB n=500
37 Correct 1 ms 492 KB n=500
38 Correct 1 ms 492 KB n=500
39 Correct 1 ms 492 KB n=500
40 Correct 1 ms 492 KB n=500
41 Correct 1 ms 492 KB n=500
42 Correct 1 ms 492 KB n=500
43 Correct 2 ms 492 KB n=500
44 Correct 2 ms 492 KB n=500
45 Correct 2 ms 492 KB n=500
46 Correct 1 ms 492 KB n=500
47 Correct 1 ms 492 KB n=500
48 Correct 1 ms 492 KB n=500
49 Correct 1 ms 492 KB n=500
50 Correct 1 ms 492 KB n=500
51 Correct 1 ms 492 KB n=500
52 Correct 1 ms 492 KB n=500
53 Correct 1 ms 492 KB n=500
54 Correct 1 ms 492 KB n=500
55 Correct 1 ms 512 KB n=278
56 Correct 1 ms 492 KB n=500
57 Correct 1 ms 492 KB n=500
58 Correct 1 ms 492 KB n=500
59 Correct 4 ms 1004 KB n=2000
60 Correct 4 ms 1132 KB n=2000
61 Correct 4 ms 1004 KB n=2000
62 Correct 4 ms 1004 KB n=2000
63 Correct 5 ms 1004 KB n=2000
64 Correct 4 ms 1004 KB n=2000
65 Correct 4 ms 1004 KB n=2000
66 Correct 4 ms 1132 KB n=2000
67 Correct 6 ms 1004 KB n=2000
68 Correct 4 ms 1004 KB n=2000
69 Correct 3 ms 1004 KB n=2000
70 Correct 4 ms 1004 KB n=2000
71 Correct 5 ms 1004 KB n=2000
72 Correct 4 ms 1004 KB n=2000
73 Correct 4 ms 1004 KB n=2000
74 Correct 4 ms 1004 KB n=1844
75 Correct 5 ms 1132 KB n=2000
76 Correct 4 ms 1004 KB n=2000
77 Correct 4 ms 1004 KB n=2000
78 Correct 4 ms 1004 KB n=2000
79 Correct 4 ms 1004 KB n=2000
80 Correct 4 ms 1132 KB n=2000
81 Correct 4 ms 1004 KB n=2000
82 Correct 4 ms 1004 KB n=2000
83 Correct 4 ms 1004 KB n=2000
84 Correct 4 ms 1004 KB n=2000
85 Correct 4 ms 1004 KB n=2000
86 Correct 4 ms 1004 KB n=2000
87 Correct 4 ms 1004 KB n=2000
88 Correct 4 ms 1152 KB n=2000
89 Correct 3 ms 1132 KB n=2000
90 Correct 4 ms 1132 KB n=2000
91 Correct 3 ms 1004 KB n=2000
92 Correct 893 ms 73836 KB n=200000
93 Correct 1065 ms 78712 KB n=200000
94 Correct 738 ms 82028 KB n=200000
95 Correct 900 ms 73632 KB n=200000
96 Correct 903 ms 73568 KB n=200000
97 Correct 1140 ms 77804 KB n=200000
98 Correct 887 ms 73684 KB n=200000
99 Correct 1107 ms 73836 KB n=200000
100 Correct 889 ms 73828 KB n=200000
101 Correct 668 ms 83436 KB n=200000
102 Correct 562 ms 74988 KB n=200000
103 Correct 532 ms 74988 KB n=200000
104 Correct 544 ms 74860 KB n=200000
105 Correct 542 ms 75244 KB n=200000
106 Correct 542 ms 75244 KB n=200000
107 Correct 539 ms 75244 KB n=200000
108 Correct 935 ms 73720 KB n=200000
109 Correct 952 ms 73700 KB n=200000
110 Correct 990 ms 73704 KB n=200000
111 Correct 913 ms 73204 KB n=200000
112 Correct 720 ms 82284 KB n=200000
113 Correct 1072 ms 77788 KB n=200000
114 Correct 925 ms 73184 KB n=200000
115 Correct 1393 ms 75628 KB n=200000
116 Correct 836 ms 73824 KB n=200000
117 Correct 676 ms 82792 KB n=200000
118 Correct 1239 ms 77036 KB n=200000
119 Correct 826 ms 73952 KB n=200000
120 Correct 599 ms 82364 KB n=200000
121 Correct 596 ms 82412 KB n=200000
122 Correct 589 ms 82796 KB n=200000
123 Correct 539 ms 74988 KB n=200000
124 Correct 194 ms 17772 KB n=25264