#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2e5 + 2;
const int mod = 1e9 + 7;
vector<int> g[N];
int nxt[N][18], in[N], out[N], tsz;
void Dfs(int s, int e) {
in[s] = ++tsz;
nxt[s][0] = e;
for (int i = 1; i < 18; i++) nxt[s][i] = nxt[nxt[s][i - 1]][i - 1];
for (int u : g[s]) {
if (u != e) Dfs(u, s);
}
out[s] = tsz;
}
bool Ancestor(int u, int v) {
return in[u] <= in[v] && out[v] <= out[u];
}
int Lca(int u, int v) {
if (Ancestor(u, v)) return u;
if (Ancestor(v, u)) return v;
for (int i = 17; i >= 0; i--) {
if (nxt[u][i] && !Ancestor(nxt[u][i], v)) u = nxt[u][i];
}
return nxt[u][0];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, q;
cin >> n >> m >> q;
for (int i = 0; i < n - 1; i++) {
int u, v; cin >> u >> v;
u -= 1; v -= 1;
g[u].push_back(v);
g[v].push_back(u);
}
Dfs(0, -1);
vector<int> a(m), b(m - 1);
for (int i = 0; i < m; i++) {
cin >> a[i];
a[i] -= 1;
}
for (int i = 0; i < m - 1; i++) b[i] = Lca(a[i], a[i + 1]);
vector<set<int>> sa(n), sb(n);
for (int i = 0; i < m; i++) sa[a[i]].insert(i);
for (int i = 0; i < m - 1; i++) sb[b[i]].insert(i);
while (q--) {
int ty; cin >> ty;
if (ty == 1) {
int p, v; cin >> p >> v;
p -= 1; v -= 1;
sa[a[p]].erase(p);
if (p > 0) sb[b[p - 1]].erase(p - 1);
if (p < m - 1) sb[b[p]].erase(p);
a[p] = v;
sa[a[p]].insert(p);
if (p > 0) {
b[p - 1] = Lca(a[p - 1], a[p]);
sb[b[p - 1]].insert(p - 1);
}
if (p < m - 1) {
b[p] = Lca(a[p], a[p + 1]);
sb[b[p]].insert(p);
}
continue;
}
int l, r, p; cin >> l >> r >> p;
l -= 1; r -= 1; p -= 1;
auto it = sa[p].lower_bound(l);
if (it != sa[p].end() && *it <= r) {
cout << *it + 1 << sp << *it + 1 << en;
continue;
}
it = sb[p].lower_bound(l);
if (it != sb[p].end() && *it < r) {
cout << *it + 1 << sp << *it + 2 << en;
continue;
}
cout << -1 << sp << -1 << en;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5024 KB |
n=5 |
2 |
Incorrect |
4 ms |
5016 KB |
Wrong range. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5024 KB |
n=5 |
2 |
Incorrect |
4 ms |
5016 KB |
Wrong range. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5024 KB |
n=5 |
2 |
Incorrect |
4 ms |
5016 KB |
Wrong range. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5024 KB |
n=5 |
2 |
Incorrect |
4 ms |
5016 KB |
Wrong range. |
3 |
Halted |
0 ms |
0 KB |
- |