#include <bits/stdc++.h>
using namespace std;
#define idata vector<int>
#define graph vector<idata>
#define sv vector<set<int>>
const int MAX_K = 20;
int nbNodes, seqSize, nbQuery;
graph roads;
idata depth;
idata parents;
graph parents_2k;
void dfs (int node, int parent, int l_depth) {
depth [node] = l_depth;
parents [node] = parent;
parents_2k[node][0] = parent;
for (int next : roads[node]) if (next != parent)
dfs(next, node, l_depth + 1);
}
void propagate () {
for (int k = 0; k + 1 < MAX_K; k ++) {
for (int i = 0; i < nbNodes; i ++) {
if (parents_2k[i][k] == -1) continue ;
parents_2k[i][k + 1]
= parents_2k[parents_2k[i][k]][k];
}
}
}
int jump (int n, int k) {
for (int i = 0; i < MAX_K; i ++)
if (((1 << i) & k) != 0)
n = parents_2k[n][i];
return n;
}
int lca (int a, int b) {
if (depth[a] > depth[b]) return lca(b, a);
b = jump(b, depth[b] - depth[a]);
if (a == b) return a;
for (int i = MAX_K - 1; i >= 0; i --) {
if (parents_2k[a][i] == parents_2k[b][i]) continue ;
a = parents_2k[a][i];
b = parents_2k[b][i];
}
if (a == b) return a;
return parents[a];
}
struct ITree {
sv tree;
idata seq;
ITree () = default;
ITree (int vec_space, int seq_size) {
seq.resize(seq_size, -1);
tree.resize(vec_space);
}
void modify (int index, int value) {
if (seq[index] != -1)
tree[seq[index]].erase(index);
seq[index] = value;
tree[value].insert(index);
}
int contains (int left, int right, int value) {
auto &h = tree[value];
auto it = h.lower_bound(left);
if (it == h.end()) return -1;
int mu = *it;
return mu <= right ? mu : -1;
}
};
idata sequence;
ITree lca_sequence;
ITree seq_search;
void compute (int i) {
lca_sequence.modify(i, lca(sequence[i], sequence[i + 1]));
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> nbNodes >> seqSize >> nbQuery;
roads.resize(nbNodes);
for (int iR = 1; iR < nbNodes; iR ++) {
int a, b;
cin >> a >> b;
a --; b --;
roads[a].push_back(b);
roads[b].push_back(a);
}
depth .resize(nbNodes);
parents .resize(nbNodes);
parents_2k.resize(nbNodes, idata(MAX_K, -1));
dfs(0, -1, 0);
propagate();
sequence.resize(seqSize);
seq_search = ITree(nbNodes, seqSize);
for (int i = 0; i < seqSize; i ++) {
cin >> sequence[i];
sequence[i] --;
seq_search.modify(i, sequence[i]);
}
lca_sequence = ITree(nbNodes, seqSize - 1);
for (int i = 0; i + 1 < seqSize; i ++)
compute(i);
for (int iQ = 0; iQ < nbQuery; iQ ++) {
int type, a, b, c;
cin >> type >> a >> b;
a --; b --;
if (type == 2) {
cin >> c; c --;
int left = -1;
int right = -1;
int mu0 = lca_sequence.contains(a, b - 1, c);
int mu1 = seq_search.contains(a, b, c);
if (mu0 != -1) {
left = mu0;
right = mu0 + 1;
} else if (mu1 != -1) {
left = mu1;
right = mu1;
}
if (left == -1) cout << "-1 -1\n";
else
cout << left + 1 << " " << right + 1 << endl;
} else {
sequence[a] = b;
seq_search.modify(a, b);
if (a != 0) compute(a - 1);
if (a != seqSize) compute(a);
}
}
}
# |
결과 |
실행 시간 |
메모리 |
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 |
340 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 |
352 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 |
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 |
448 KB |
n=100 |
20 |
Correct |
1 ms |
464 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 |
212 KB |
n=12 |
27 |
Correct |
1 ms |
340 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 |
340 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 |
352 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 |
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 |
448 KB |
n=100 |
20 |
Correct |
1 ms |
464 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 |
212 KB |
n=12 |
27 |
Correct |
1 ms |
340 KB |
n=100 |
28 |
Correct |
1 ms |
468 KB |
n=500 |
29 |
Correct |
2 ms |
468 KB |
n=500 |
30 |
Correct |
3 ms |
468 KB |
n=500 |
31 |
Correct |
2 ms |
468 KB |
n=500 |
32 |
Correct |
1 ms |
468 KB |
n=500 |
33 |
Correct |
2 ms |
468 KB |
n=500 |
34 |
Correct |
1 ms |
468 KB |
n=500 |
35 |
Correct |
1 ms |
468 KB |
n=500 |
36 |
Correct |
2 ms |
468 KB |
n=500 |
37 |
Correct |
2 ms |
468 KB |
n=500 |
38 |
Correct |
2 ms |
468 KB |
n=500 |
39 |
Correct |
1 ms |
468 KB |
n=500 |
40 |
Correct |
1 ms |
596 KB |
n=500 |
41 |
Correct |
1 ms |
468 KB |
n=500 |
42 |
Correct |
2 ms |
468 KB |
n=500 |
43 |
Correct |
2 ms |
468 KB |
n=500 |
44 |
Correct |
2 ms |
468 KB |
n=500 |
45 |
Correct |
1 ms |
468 KB |
n=500 |
46 |
Correct |
2 ms |
468 KB |
n=500 |
47 |
Correct |
1 ms |
468 KB |
n=500 |
48 |
Correct |
2 ms |
468 KB |
n=500 |
49 |
Correct |
2 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 |
2 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 |
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 |
340 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 |
352 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 |
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 |
448 KB |
n=100 |
20 |
Correct |
1 ms |
464 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 |
212 KB |
n=12 |
27 |
Correct |
1 ms |
340 KB |
n=100 |
28 |
Correct |
1 ms |
468 KB |
n=500 |
29 |
Correct |
2 ms |
468 KB |
n=500 |
30 |
Correct |
3 ms |
468 KB |
n=500 |
31 |
Correct |
2 ms |
468 KB |
n=500 |
32 |
Correct |
1 ms |
468 KB |
n=500 |
33 |
Correct |
2 ms |
468 KB |
n=500 |
34 |
Correct |
1 ms |
468 KB |
n=500 |
35 |
Correct |
1 ms |
468 KB |
n=500 |
36 |
Correct |
2 ms |
468 KB |
n=500 |
37 |
Correct |
2 ms |
468 KB |
n=500 |
38 |
Correct |
2 ms |
468 KB |
n=500 |
39 |
Correct |
1 ms |
468 KB |
n=500 |
40 |
Correct |
1 ms |
596 KB |
n=500 |
41 |
Correct |
1 ms |
468 KB |
n=500 |
42 |
Correct |
2 ms |
468 KB |
n=500 |
43 |
Correct |
2 ms |
468 KB |
n=500 |
44 |
Correct |
2 ms |
468 KB |
n=500 |
45 |
Correct |
1 ms |
468 KB |
n=500 |
46 |
Correct |
2 ms |
468 KB |
n=500 |
47 |
Correct |
1 ms |
468 KB |
n=500 |
48 |
Correct |
2 ms |
468 KB |
n=500 |
49 |
Correct |
2 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 |
2 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 |
4 ms |
980 KB |
n=2000 |
60 |
Correct |
5 ms |
1108 KB |
n=2000 |
61 |
Correct |
7 ms |
1108 KB |
n=2000 |
62 |
Correct |
5 ms |
1108 KB |
n=2000 |
63 |
Correct |
5 ms |
980 KB |
n=2000 |
64 |
Correct |
4 ms |
1108 KB |
n=2000 |
65 |
Correct |
4 ms |
980 KB |
n=2000 |
66 |
Correct |
4 ms |
1108 KB |
n=2000 |
67 |
Correct |
5 ms |
980 KB |
n=2000 |
68 |
Correct |
4 ms |
1108 KB |
n=2000 |
69 |
Correct |
5 ms |
1084 KB |
n=2000 |
70 |
Correct |
7 ms |
980 KB |
n=2000 |
71 |
Correct |
7 ms |
1088 KB |
n=2000 |
72 |
Correct |
3 ms |
980 KB |
n=2000 |
73 |
Correct |
3 ms |
980 KB |
n=2000 |
74 |
Correct |
5 ms |
1048 KB |
n=1844 |
75 |
Correct |
2 ms |
980 KB |
n=2000 |
76 |
Correct |
5 ms |
980 KB |
n=2000 |
77 |
Correct |
5 ms |
980 KB |
n=2000 |
78 |
Correct |
5 ms |
980 KB |
n=2000 |
79 |
Correct |
4 ms |
1092 KB |
n=2000 |
80 |
Correct |
4 ms |
1108 KB |
n=2000 |
81 |
Correct |
6 ms |
1108 KB |
n=2000 |
82 |
Correct |
5 ms |
980 KB |
n=2000 |
83 |
Correct |
4 ms |
1108 KB |
n=2000 |
84 |
Correct |
4 ms |
980 KB |
n=2000 |
85 |
Correct |
3 ms |
1108 KB |
n=2000 |
86 |
Correct |
4 ms |
1108 KB |
n=2000 |
87 |
Correct |
4 ms |
980 KB |
n=2000 |
88 |
Correct |
3 ms |
1108 KB |
n=2000 |
89 |
Correct |
3 ms |
1108 KB |
n=2000 |
90 |
Correct |
4 ms |
1108 KB |
n=2000 |
91 |
Correct |
4 ms |
980 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 |
340 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 |
352 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 |
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 |
448 KB |
n=100 |
20 |
Correct |
1 ms |
464 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 |
212 KB |
n=12 |
27 |
Correct |
1 ms |
340 KB |
n=100 |
28 |
Correct |
1 ms |
468 KB |
n=500 |
29 |
Correct |
2 ms |
468 KB |
n=500 |
30 |
Correct |
3 ms |
468 KB |
n=500 |
31 |
Correct |
2 ms |
468 KB |
n=500 |
32 |
Correct |
1 ms |
468 KB |
n=500 |
33 |
Correct |
2 ms |
468 KB |
n=500 |
34 |
Correct |
1 ms |
468 KB |
n=500 |
35 |
Correct |
1 ms |
468 KB |
n=500 |
36 |
Correct |
2 ms |
468 KB |
n=500 |
37 |
Correct |
2 ms |
468 KB |
n=500 |
38 |
Correct |
2 ms |
468 KB |
n=500 |
39 |
Correct |
1 ms |
468 KB |
n=500 |
40 |
Correct |
1 ms |
596 KB |
n=500 |
41 |
Correct |
1 ms |
468 KB |
n=500 |
42 |
Correct |
2 ms |
468 KB |
n=500 |
43 |
Correct |
2 ms |
468 KB |
n=500 |
44 |
Correct |
2 ms |
468 KB |
n=500 |
45 |
Correct |
1 ms |
468 KB |
n=500 |
46 |
Correct |
2 ms |
468 KB |
n=500 |
47 |
Correct |
1 ms |
468 KB |
n=500 |
48 |
Correct |
2 ms |
468 KB |
n=500 |
49 |
Correct |
2 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 |
2 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 |
4 ms |
980 KB |
n=2000 |
60 |
Correct |
5 ms |
1108 KB |
n=2000 |
61 |
Correct |
7 ms |
1108 KB |
n=2000 |
62 |
Correct |
5 ms |
1108 KB |
n=2000 |
63 |
Correct |
5 ms |
980 KB |
n=2000 |
64 |
Correct |
4 ms |
1108 KB |
n=2000 |
65 |
Correct |
4 ms |
980 KB |
n=2000 |
66 |
Correct |
4 ms |
1108 KB |
n=2000 |
67 |
Correct |
5 ms |
980 KB |
n=2000 |
68 |
Correct |
4 ms |
1108 KB |
n=2000 |
69 |
Correct |
5 ms |
1084 KB |
n=2000 |
70 |
Correct |
7 ms |
980 KB |
n=2000 |
71 |
Correct |
7 ms |
1088 KB |
n=2000 |
72 |
Correct |
3 ms |
980 KB |
n=2000 |
73 |
Correct |
3 ms |
980 KB |
n=2000 |
74 |
Correct |
5 ms |
1048 KB |
n=1844 |
75 |
Correct |
2 ms |
980 KB |
n=2000 |
76 |
Correct |
5 ms |
980 KB |
n=2000 |
77 |
Correct |
5 ms |
980 KB |
n=2000 |
78 |
Correct |
5 ms |
980 KB |
n=2000 |
79 |
Correct |
4 ms |
1092 KB |
n=2000 |
80 |
Correct |
4 ms |
1108 KB |
n=2000 |
81 |
Correct |
6 ms |
1108 KB |
n=2000 |
82 |
Correct |
5 ms |
980 KB |
n=2000 |
83 |
Correct |
4 ms |
1108 KB |
n=2000 |
84 |
Correct |
4 ms |
980 KB |
n=2000 |
85 |
Correct |
3 ms |
1108 KB |
n=2000 |
86 |
Correct |
4 ms |
1108 KB |
n=2000 |
87 |
Correct |
4 ms |
980 KB |
n=2000 |
88 |
Correct |
3 ms |
1108 KB |
n=2000 |
89 |
Correct |
3 ms |
1108 KB |
n=2000 |
90 |
Correct |
4 ms |
1108 KB |
n=2000 |
91 |
Correct |
4 ms |
980 KB |
n=2000 |
92 |
Correct |
797 ms |
78476 KB |
n=200000 |
93 |
Correct |
1133 ms |
81948 KB |
n=200000 |
94 |
Correct |
1073 ms |
85244 KB |
n=200000 |
95 |
Correct |
820 ms |
84512 KB |
n=200000 |
96 |
Correct |
857 ms |
84644 KB |
n=200000 |
97 |
Correct |
1159 ms |
88708 KB |
n=200000 |
98 |
Correct |
817 ms |
84224 KB |
n=200000 |
99 |
Correct |
1029 ms |
84712 KB |
n=200000 |
100 |
Correct |
833 ms |
84508 KB |
n=200000 |
101 |
Correct |
1091 ms |
94400 KB |
n=200000 |
102 |
Correct |
678 ms |
85760 KB |
n=200000 |
103 |
Correct |
703 ms |
85884 KB |
n=200000 |
104 |
Correct |
692 ms |
85808 KB |
n=200000 |
105 |
Correct |
725 ms |
86140 KB |
n=200000 |
106 |
Correct |
699 ms |
86152 KB |
n=200000 |
107 |
Correct |
756 ms |
86128 KB |
n=200000 |
108 |
Correct |
961 ms |
84668 KB |
n=200000 |
109 |
Correct |
958 ms |
84532 KB |
n=200000 |
110 |
Correct |
972 ms |
84656 KB |
n=200000 |
111 |
Correct |
891 ms |
84208 KB |
n=200000 |
112 |
Correct |
1081 ms |
93272 KB |
n=200000 |
113 |
Correct |
1105 ms |
88664 KB |
n=200000 |
114 |
Correct |
856 ms |
84044 KB |
n=200000 |
115 |
Correct |
1182 ms |
86596 KB |
n=200000 |
116 |
Correct |
688 ms |
84672 KB |
n=200000 |
117 |
Correct |
924 ms |
93644 KB |
n=200000 |
118 |
Correct |
1193 ms |
87596 KB |
n=200000 |
119 |
Correct |
712 ms |
84644 KB |
n=200000 |
120 |
Correct |
913 ms |
93252 KB |
n=200000 |
121 |
Correct |
910 ms |
93184 KB |
n=200000 |
122 |
Correct |
966 ms |
93632 KB |
n=200000 |
123 |
Correct |
605 ms |
85912 KB |
n=200000 |
124 |
Correct |
219 ms |
19960 KB |
n=25264 |