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>
#define pb push_back
#define F first
#define S second
using namespace std;
const int N = 2 * 1e5 + 5;
const int MOD = 1e9 + 7;
const int K = 18;
int n, m, q, a[N], par[N][K], h[N], lca1[2005][2005];
vector<int> g[N];
void dfs(int v, int pr){
par[v][0] = pr;
for(int to : g[v]){
if(to != pr){
h[to] = h[v] + 1;
dfs(to, v);
}
}
}
bool bit(int x, int p){
return x & (1 << p);
}
int lift(int v, int k){
for(int i = 0; i < K; i++){
if (bit(k, i)) {
v = par[v][i];
}
}
return v;
}
int lca(int a, int b){
if(lca1[a][b]) return lca1[a][b];
if(h[a] < h[b]){
swap(a, b);
}
a = lift(a, h[a] - h[b]);
if(a == b){
return a;
}
for(int i = K - 1; i >= 0; --i){
if(par[a][i] != par[b][i]){
a = par[a][i];
b = par[b][i];
}
}
lca1[a][b] = par[a][0];
lca1[b][a] = par[a][0];
return par[a][0];
}
pair<int, int> get(int l, int r, int val){
for(int i = l; i <= r; ++i){
int v = a[i];
for(int j = i; j <= r; ++j){
v = lca(a[j], v);
if(v == val) return {i, j};
if(lca(v, val) != val) break;
}
}
return {-1, -1};
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> q;
for(int i = 1, u, v; i < n; i++){
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
dfs(1, 1);
for(int i = 1; i < K; i++){
for(int v = 1; v <= n; v++){
par[v][i] = par[par[v][i - 1]][i - 1];
}
}
for(int i = 1; i <= m; ++i){
cin >> a[i];
}
int pos, v, l, r, typ;
for (int i = 1; i <= q; i++) {
cin >> typ;
if(typ == 1){
cin >> pos >> v;
a[pos] = v;
}
else{
cin >> l >> r >> v;
auto ans = get(l, r, v);
cout << ans.F << ' ' << ans.S << '\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... |