Submission #495717

#TimeUsernameProblemLanguageResultExecution timeMemory
495717NalrimetBirthday gift (IZhO18_treearray)C++17
30 / 100
4070 ms5348 KiB
#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]; 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(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]; } } 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}; } } 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...