답안 #947570

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
947570 2024-03-16T12:31:04 Z oblantis Birthday gift (IZhO18_treearray) C++17
100 / 100
770 ms 87060 KB
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
//#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define pb push_back
#define ss second
#define ff first
#define vt vector
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 1e9;
const int mod = 1e9+7;
const int maxn = 2e5 + 12;
vt<int> g[maxn];
set<int> nei[maxn], sing[maxn];
int dp[maxn], up[maxn][20], a[maxn];
void dfs(int v){
	for(auto i : g[v]){
		if(up[v][0] == i)continue;
		dp[i] = dp[v] + 1;
		up[i][0] = v;
		dfs(i);
	}
}
int lca(int x, int y){
	if(dp[x] > dp[y])swap(x, y);
	int k = dp[y] - dp[x], o = 0;
	while(k){
		if(k & 1)y = up[y][o];
		o++;
		k >>= 1;
	}
	if(x == y)return x;
	for(int i = 19; i >= 0; i--){
		if(up[x][i] != up[y][i]){
			x = up[x][i], y = up[y][i];
		}
	}
	return up[x][0];
}
void solve() {
	int n, m, q;
	cin >> n >> m >> q;
	for(int i = 0, u, v; i < n - 1; i++){
		cin >> u >> v;
		g[u].pb(v), g[v].pb(u);
	}
	up[1][0] = 1;
	dfs(1);
	for(int i = 1; i < 20; i++){
		for(int j = 1; j <= n; j++)up[j][i] = up[up[j][i - 1]][i - 1];
	}
	for(int i = 1; i <= m; i++){
		cin >> a[i];
		sing[a[i]].insert(i);
		if(i > 1){
			nei[lca(a[i], a[i - 1])].insert(i - 1);
		}
	}
	for(int i = 0; i < q; i++){
		int w;
		cin >> w;
		if(w == 1){
			int pos, v, head;
			cin >> pos >> v;
			if(pos > 1){
				head = lca(a[pos - 1], a[pos]);
				nei[head].erase(pos - 1);
				head = lca(a[pos - 1], v);
				nei[head].insert(pos - 1);
			}
			if(pos < m){
				head = lca(a[pos], a[pos + 1]);
				nei[head].erase(pos);
				head = lca(v, a[pos + 1]);
				nei[head].insert(pos);	
			}
			sing[a[pos]].erase(pos);
			a[pos] = v;
			sing[a[pos]].insert(pos);
		}
		else {
			int l, r, x;
			cin >> l >> r >> x;
			if(sing[x].lower_bound(l) != sing[x].end() && *sing[x].lower_bound(l) <= r){
				cout << *sing[x].lower_bound(l) << ' ' << *sing[x].lower_bound(l) << '\n';
			}
			else if(nei[x].lower_bound(l) != nei[x].end() && *nei[x].lower_bound(l) < r){
				cout << *nei[x].lower_bound(l) << ' ' << *nei[x].lower_bound(l) + 1 << '\n';
			}
			else cout << "-1 -1\n";
		}
	}
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int times = 1;
	//cin >> times;
	for(int i = 1; i <= times; i++) {
		solve();
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 26460 KB n=5
2 Correct 6 ms 24412 KB n=100
3 Correct 7 ms 26456 KB n=100
4 Correct 6 ms 24412 KB n=100
5 Correct 6 ms 26460 KB n=100
6 Correct 6 ms 24412 KB n=100
7 Correct 6 ms 26460 KB n=100
8 Correct 6 ms 26460 KB n=100
9 Correct 6 ms 26460 KB n=100
10 Correct 6 ms 24408 KB n=100
11 Correct 6 ms 24412 KB n=100
12 Correct 6 ms 24412 KB n=100
13 Correct 7 ms 24412 KB n=100
14 Correct 6 ms 24576 KB n=100
15 Correct 6 ms 26460 KB n=100
16 Correct 6 ms 24412 KB n=100
17 Correct 6 ms 24412 KB n=100
18 Correct 6 ms 24412 KB n=100
19 Correct 6 ms 26456 KB n=100
20 Correct 6 ms 26460 KB n=100
21 Correct 6 ms 24412 KB n=100
22 Correct 6 ms 24408 KB n=100
23 Correct 6 ms 26460 KB n=100
24 Correct 6 ms 26460 KB n=100
25 Correct 6 ms 24528 KB n=100
26 Correct 6 ms 26456 KB n=12
27 Correct 6 ms 24608 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 26460 KB n=5
2 Correct 6 ms 24412 KB n=100
3 Correct 7 ms 26456 KB n=100
4 Correct 6 ms 24412 KB n=100
5 Correct 6 ms 26460 KB n=100
6 Correct 6 ms 24412 KB n=100
7 Correct 6 ms 26460 KB n=100
8 Correct 6 ms 26460 KB n=100
9 Correct 6 ms 26460 KB n=100
10 Correct 6 ms 24408 KB n=100
11 Correct 6 ms 24412 KB n=100
12 Correct 6 ms 24412 KB n=100
13 Correct 7 ms 24412 KB n=100
14 Correct 6 ms 24576 KB n=100
15 Correct 6 ms 26460 KB n=100
16 Correct 6 ms 24412 KB n=100
17 Correct 6 ms 24412 KB n=100
18 Correct 6 ms 24412 KB n=100
19 Correct 6 ms 26456 KB n=100
20 Correct 6 ms 26460 KB n=100
21 Correct 6 ms 24412 KB n=100
22 Correct 6 ms 24408 KB n=100
23 Correct 6 ms 26460 KB n=100
24 Correct 6 ms 26460 KB n=100
25 Correct 6 ms 24528 KB n=100
26 Correct 6 ms 26456 KB n=12
27 Correct 6 ms 24608 KB n=100
28 Correct 7 ms 26712 KB n=500
29 Correct 6 ms 24668 KB n=500
30 Correct 6 ms 24668 KB n=500
31 Correct 6 ms 26716 KB n=500
32 Correct 6 ms 26716 KB n=500
33 Correct 6 ms 24664 KB n=500
34 Correct 6 ms 26456 KB n=500
35 Correct 6 ms 26716 KB n=500
36 Correct 6 ms 26716 KB n=500
37 Correct 6 ms 24668 KB n=500
38 Correct 6 ms 26716 KB n=500
39 Correct 6 ms 24668 KB n=500
40 Correct 7 ms 26716 KB n=500
41 Correct 6 ms 24664 KB n=500
42 Correct 6 ms 24664 KB n=500
43 Correct 6 ms 24668 KB n=500
44 Correct 6 ms 24668 KB n=500
45 Correct 6 ms 24668 KB n=500
46 Correct 6 ms 24668 KB n=500
47 Correct 6 ms 24668 KB n=500
48 Correct 6 ms 24668 KB n=500
49 Correct 9 ms 24668 KB n=500
50 Correct 9 ms 26716 KB n=500
51 Correct 6 ms 24668 KB n=500
52 Correct 6 ms 24668 KB n=500
53 Correct 8 ms 26712 KB n=500
54 Correct 6 ms 26716 KB n=500
55 Correct 6 ms 24668 KB n=278
56 Correct 6 ms 24668 KB n=500
57 Correct 7 ms 24668 KB n=500
58 Correct 7 ms 24584 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 26460 KB n=5
2 Correct 6 ms 24412 KB n=100
3 Correct 7 ms 26456 KB n=100
4 Correct 6 ms 24412 KB n=100
5 Correct 6 ms 26460 KB n=100
6 Correct 6 ms 24412 KB n=100
7 Correct 6 ms 26460 KB n=100
8 Correct 6 ms 26460 KB n=100
9 Correct 6 ms 26460 KB n=100
10 Correct 6 ms 24408 KB n=100
11 Correct 6 ms 24412 KB n=100
12 Correct 6 ms 24412 KB n=100
13 Correct 7 ms 24412 KB n=100
14 Correct 6 ms 24576 KB n=100
15 Correct 6 ms 26460 KB n=100
16 Correct 6 ms 24412 KB n=100
17 Correct 6 ms 24412 KB n=100
18 Correct 6 ms 24412 KB n=100
19 Correct 6 ms 26456 KB n=100
20 Correct 6 ms 26460 KB n=100
21 Correct 6 ms 24412 KB n=100
22 Correct 6 ms 24408 KB n=100
23 Correct 6 ms 26460 KB n=100
24 Correct 6 ms 26460 KB n=100
25 Correct 6 ms 24528 KB n=100
26 Correct 6 ms 26456 KB n=12
27 Correct 6 ms 24608 KB n=100
28 Correct 7 ms 26712 KB n=500
29 Correct 6 ms 24668 KB n=500
30 Correct 6 ms 24668 KB n=500
31 Correct 6 ms 26716 KB n=500
32 Correct 6 ms 26716 KB n=500
33 Correct 6 ms 24664 KB n=500
34 Correct 6 ms 26456 KB n=500
35 Correct 6 ms 26716 KB n=500
36 Correct 6 ms 26716 KB n=500
37 Correct 6 ms 24668 KB n=500
38 Correct 6 ms 26716 KB n=500
39 Correct 6 ms 24668 KB n=500
40 Correct 7 ms 26716 KB n=500
41 Correct 6 ms 24664 KB n=500
42 Correct 6 ms 24664 KB n=500
43 Correct 6 ms 24668 KB n=500
44 Correct 6 ms 24668 KB n=500
45 Correct 6 ms 24668 KB n=500
46 Correct 6 ms 24668 KB n=500
47 Correct 6 ms 24668 KB n=500
48 Correct 6 ms 24668 KB n=500
49 Correct 9 ms 24668 KB n=500
50 Correct 9 ms 26716 KB n=500
51 Correct 6 ms 24668 KB n=500
52 Correct 6 ms 24668 KB n=500
53 Correct 8 ms 26712 KB n=500
54 Correct 6 ms 26716 KB n=500
55 Correct 6 ms 24668 KB n=278
56 Correct 6 ms 24668 KB n=500
57 Correct 7 ms 24668 KB n=500
58 Correct 7 ms 24584 KB n=500
59 Correct 8 ms 26972 KB n=2000
60 Correct 8 ms 26972 KB n=2000
61 Correct 9 ms 24924 KB n=2000
62 Correct 9 ms 26892 KB n=2000
63 Correct 8 ms 24924 KB n=2000
64 Correct 8 ms 26972 KB n=2000
65 Correct 8 ms 26972 KB n=2000
66 Correct 8 ms 27304 KB n=2000
67 Correct 9 ms 25180 KB n=2000
68 Correct 8 ms 26972 KB n=2000
69 Correct 10 ms 25176 KB n=2000
70 Correct 7 ms 24920 KB n=2000
71 Correct 8 ms 26972 KB n=2000
72 Correct 9 ms 26968 KB n=2000
73 Correct 8 ms 24924 KB n=2000
74 Correct 8 ms 26712 KB n=1844
75 Correct 7 ms 26972 KB n=2000
76 Correct 8 ms 24924 KB n=2000
77 Correct 8 ms 25176 KB n=2000
78 Correct 9 ms 26716 KB n=2000
79 Correct 8 ms 26968 KB n=2000
80 Correct 8 ms 25176 KB n=2000
81 Correct 8 ms 25176 KB n=2000
82 Correct 8 ms 26716 KB n=2000
83 Correct 8 ms 26968 KB n=2000
84 Correct 8 ms 24920 KB n=2000
85 Correct 8 ms 26972 KB n=2000
86 Correct 8 ms 26992 KB n=2000
87 Correct 10 ms 26716 KB n=2000
88 Correct 8 ms 25168 KB n=2000
89 Correct 8 ms 25180 KB n=2000
90 Correct 9 ms 25180 KB n=2000
91 Correct 7 ms 24924 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 26460 KB n=5
2 Correct 6 ms 24412 KB n=100
3 Correct 7 ms 26456 KB n=100
4 Correct 6 ms 24412 KB n=100
5 Correct 6 ms 26460 KB n=100
6 Correct 6 ms 24412 KB n=100
7 Correct 6 ms 26460 KB n=100
8 Correct 6 ms 26460 KB n=100
9 Correct 6 ms 26460 KB n=100
10 Correct 6 ms 24408 KB n=100
11 Correct 6 ms 24412 KB n=100
12 Correct 6 ms 24412 KB n=100
13 Correct 7 ms 24412 KB n=100
14 Correct 6 ms 24576 KB n=100
15 Correct 6 ms 26460 KB n=100
16 Correct 6 ms 24412 KB n=100
17 Correct 6 ms 24412 KB n=100
18 Correct 6 ms 24412 KB n=100
19 Correct 6 ms 26456 KB n=100
20 Correct 6 ms 26460 KB n=100
21 Correct 6 ms 24412 KB n=100
22 Correct 6 ms 24408 KB n=100
23 Correct 6 ms 26460 KB n=100
24 Correct 6 ms 26460 KB n=100
25 Correct 6 ms 24528 KB n=100
26 Correct 6 ms 26456 KB n=12
27 Correct 6 ms 24608 KB n=100
28 Correct 7 ms 26712 KB n=500
29 Correct 6 ms 24668 KB n=500
30 Correct 6 ms 24668 KB n=500
31 Correct 6 ms 26716 KB n=500
32 Correct 6 ms 26716 KB n=500
33 Correct 6 ms 24664 KB n=500
34 Correct 6 ms 26456 KB n=500
35 Correct 6 ms 26716 KB n=500
36 Correct 6 ms 26716 KB n=500
37 Correct 6 ms 24668 KB n=500
38 Correct 6 ms 26716 KB n=500
39 Correct 6 ms 24668 KB n=500
40 Correct 7 ms 26716 KB n=500
41 Correct 6 ms 24664 KB n=500
42 Correct 6 ms 24664 KB n=500
43 Correct 6 ms 24668 KB n=500
44 Correct 6 ms 24668 KB n=500
45 Correct 6 ms 24668 KB n=500
46 Correct 6 ms 24668 KB n=500
47 Correct 6 ms 24668 KB n=500
48 Correct 6 ms 24668 KB n=500
49 Correct 9 ms 24668 KB n=500
50 Correct 9 ms 26716 KB n=500
51 Correct 6 ms 24668 KB n=500
52 Correct 6 ms 24668 KB n=500
53 Correct 8 ms 26712 KB n=500
54 Correct 6 ms 26716 KB n=500
55 Correct 6 ms 24668 KB n=278
56 Correct 6 ms 24668 KB n=500
57 Correct 7 ms 24668 KB n=500
58 Correct 7 ms 24584 KB n=500
59 Correct 8 ms 26972 KB n=2000
60 Correct 8 ms 26972 KB n=2000
61 Correct 9 ms 24924 KB n=2000
62 Correct 9 ms 26892 KB n=2000
63 Correct 8 ms 24924 KB n=2000
64 Correct 8 ms 26972 KB n=2000
65 Correct 8 ms 26972 KB n=2000
66 Correct 8 ms 27304 KB n=2000
67 Correct 9 ms 25180 KB n=2000
68 Correct 8 ms 26972 KB n=2000
69 Correct 10 ms 25176 KB n=2000
70 Correct 7 ms 24920 KB n=2000
71 Correct 8 ms 26972 KB n=2000
72 Correct 9 ms 26968 KB n=2000
73 Correct 8 ms 24924 KB n=2000
74 Correct 8 ms 26712 KB n=1844
75 Correct 7 ms 26972 KB n=2000
76 Correct 8 ms 24924 KB n=2000
77 Correct 8 ms 25176 KB n=2000
78 Correct 9 ms 26716 KB n=2000
79 Correct 8 ms 26968 KB n=2000
80 Correct 8 ms 25176 KB n=2000
81 Correct 8 ms 25176 KB n=2000
82 Correct 8 ms 26716 KB n=2000
83 Correct 8 ms 26968 KB n=2000
84 Correct 8 ms 24920 KB n=2000
85 Correct 8 ms 26972 KB n=2000
86 Correct 8 ms 26992 KB n=2000
87 Correct 10 ms 26716 KB n=2000
88 Correct 8 ms 25168 KB n=2000
89 Correct 8 ms 25180 KB n=2000
90 Correct 9 ms 25180 KB n=2000
91 Correct 7 ms 24924 KB n=2000
92 Correct 530 ms 74888 KB n=200000
93 Correct 770 ms 81368 KB n=200000
94 Correct 690 ms 85428 KB n=200000
95 Correct 499 ms 74436 KB n=200000
96 Correct 495 ms 74680 KB n=200000
97 Correct 735 ms 80160 KB n=200000
98 Correct 519 ms 74600 KB n=200000
99 Correct 614 ms 74712 KB n=200000
100 Correct 587 ms 74440 KB n=200000
101 Correct 688 ms 87060 KB n=200000
102 Correct 285 ms 75604 KB n=200000
103 Correct 300 ms 75600 KB n=200000
104 Correct 309 ms 76092 KB n=200000
105 Correct 306 ms 76016 KB n=200000
106 Correct 306 ms 75856 KB n=200000
107 Correct 301 ms 75916 KB n=200000
108 Correct 556 ms 74392 KB n=200000
109 Correct 542 ms 74580 KB n=200000
110 Correct 547 ms 74520 KB n=200000
111 Correct 505 ms 73748 KB n=200000
112 Correct 694 ms 85912 KB n=200000
113 Correct 746 ms 80004 KB n=200000
114 Correct 517 ms 74152 KB n=200000
115 Correct 746 ms 76956 KB n=200000
116 Correct 474 ms 74436 KB n=200000
117 Correct 725 ms 86356 KB n=200000
118 Correct 714 ms 78240 KB n=200000
119 Correct 486 ms 74436 KB n=200000
120 Correct 678 ms 86280 KB n=200000
121 Correct 648 ms 86036 KB n=200000
122 Correct 660 ms 86440 KB n=200000
123 Correct 340 ms 75960 KB n=200000
124 Correct 121 ms 41624 KB n=25264