This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int LOG = 20;
const int maxn = 2e5 + 5;
int n, m, q, D[maxn], up[maxn][LOG];
vector<int> G[maxn], V(maxn);
set<int> S[maxn], M[maxn];
void dfs(int u, int p) {
for(int i=1; i<LOG; i++) up[u][i] = up[up[u][i-1]][i-1];
for(int &v : G[u]) {
if(v == p) continue;
up[v][0] = u;
D[v] = D[u] + 1;
dfs(v, u);
}
}
int jmp(int x, int d) {
for(int j=LOG-1; j>=0; j--)
if(d & (1 << j)) x = up[x][j];
return x;
}
int get_lca(int a, int b) {
if(D[a] < D[b]) swap(a, b);
a = jmp(a, D[a] - D[b]);
if(a == b) return a;
for(int j=LOG-1; j>=0; j--)
if(up[a][j] != up[b][j])
a = up[a][j], b = up[b][j];
return up[a][0];
}
int main() {
cin >> n >> m >> q;
for(int i=0; i<n-1; i++) {
int a, b;
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
dfs(1, 0);
for(int i=1; i<=m; i++) {
cin >> V[i];
}
for(int i=1; i<=m; i++) S[V[i]].insert(i);
for(int i=1; i+1<=m; i++) M[get_lca(V[i], V[i+1])].insert(i);
while(q--) {
int t;
cin >> t;
if(t == 1) {
int p, v;
cin >> p >> v;
S[V[p]].erase(p);
if(p < m) M[get_lca(V[p], V[p+1])].erase(p);
if(p) M[get_lca(V[p], V[p-1])].erase(p-1);
V[p] = v;
S[V[p]].insert(p);
if(p < m) M[get_lca(V[p], V[p+1])].insert(p);
if(p) M[get_lca(V[p], V[p-1])].insert(p-1);
} else {
int l, r, v;
cin >> l >> r >> v;
auto it = S[v].lower_bound(l);
if(it != S[v].end() && *it <= r && *it >= l) {
cout << *it << " " << *it << '\n';
continue;
}
auto it2 = M[v].lower_bound(l);
if(it2 != M[v].end() && *it2 < r && *it2 >= l) {
cout << *it2 << " " << *it2 + 1 << '\n';
continue;
}
cout << "-1 -1" << '\n';
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |