#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int L = 20;
int n, m, q;
vector<int> g[N];
int a[N];
int fst[N], et[2 * N], lv[N];
int lg2[2 * N];
pair<int, int> anc[L][2 * N];
int time_dfs = 0;
set<int> sf[N], pr[N];
void pre_dfs(int u, int p) {
time_dfs++;
fst[u] = time_dfs;
et[time_dfs] = u;
for (int v : g[u]) {
if (v != p) {
lv[v] = lv[u] + 1;
pre_dfs(v, u);
time_dfs++;
et[time_dfs] = u;
}
}
}
void build_lca() {
for (int i = 2; i < 2 * N; i++) {
lg2[i] = lg2[i >> 1] + 1;
}
for (int j = 0; j < L; j++) {
for (int i = 1; i + (1 << j) - 1 <= time_dfs; i++) {
anc[j][i] = (j == 0 ? pair<int, int>{lv[et[i]], et[i]} : min(anc[j - 1][i], anc[j - 1][i + (1 << (j - 1))]));
}
}
}
int lca(int u, int v) {
if (fst[u] > fst[v]) {
swap(u, v);
}
int w = lg2[fst[v] - fst[u] + 1];
return min(anc[w][fst[u]], anc[w][fst[v] - (1 << w) + 1]).second;
}
void upd(int pos, int v) {
sf[a[pos]].erase(pos);
if (pos > 1) {
pr[lca(a[pos - 1], a[pos])].erase(pos - 1);
}
if (pos < m) {
pr[lca(a[pos], a[pos + 1])].erase(pos);
}
a[pos] = v;
sf[a[pos]].insert(pos);
if (pos > 1) {
pr[lca(a[pos - 1], a[pos])].erase(pos - 1);
}
if (pos < m) {
pr[lca(a[pos], a[pos + 1])].erase(pos);
}
}
void solve(int l, int r, int v) {
auto it = sf[v].lower_bound(l);
if (it != sf[v].end()) {
int x = *it;
if (x <= r) {
cout << x << ' ' << x << '\n';
return;
}
}
it = pr[v].lower_bound(l);
if (it != pr[v].end()) {
int x = *it;
if (x <= r - 1) {
cout << x << ' ' << x + 1 << '\n';
return;
}
}
cout << -1 << ' ' << -1 << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> q;
for (int i = 1; i <= n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
pre_dfs(1, 1);
build_lca();
for (int i = 1; i <= m; i++) {
cin >> a[i];
}
for (int i = 1; i <= m; i++) {
sf[a[i]].insert(i);
if (i < m) {
pr[lca(a[i], a[i + 1])].insert(i);
}
}
while (q--) {
int t;
cin >> t;
if (t == 1) {
int pos, v;
cin >> pos >> v;
upd(pos, v);
} else if (t == 2) {
int l, r, v;
cin >> l >> r >> v;
solve(l, r, v);
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
37464 KB |
n=5 |
2 |
Correct |
9 ms |
45660 KB |
n=100 |
3 |
Correct |
10 ms |
45656 KB |
n=100 |
4 |
Incorrect |
9 ms |
45656 KB |
Jury has the answer but participant has not |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
37464 KB |
n=5 |
2 |
Correct |
9 ms |
45660 KB |
n=100 |
3 |
Correct |
10 ms |
45656 KB |
n=100 |
4 |
Incorrect |
9 ms |
45656 KB |
Jury has the answer but participant has not |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
37464 KB |
n=5 |
2 |
Correct |
9 ms |
45660 KB |
n=100 |
3 |
Correct |
10 ms |
45656 KB |
n=100 |
4 |
Incorrect |
9 ms |
45656 KB |
Jury has the answer but participant has not |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
37464 KB |
n=5 |
2 |
Correct |
9 ms |
45660 KB |
n=100 |
3 |
Correct |
10 ms |
45656 KB |
n=100 |
4 |
Incorrect |
9 ms |
45656 KB |
Jury has the answer but participant has not |
5 |
Halted |
0 ms |
0 KB |
- |