Submission #1109213

#TimeUsernameProblemLanguageResultExecution timeMemory
1109213stdfloatBirthday gift (IZhO18_treearray)C++17
0 / 100
2 ms592 KiB
#include <bits/stdc++.h> using namespace std; vector<int> d; vector<vector<int>> E, sp; void dfs(int x, int p = -1) { sp[x][0] = p; if (p != -1) d[x] = d[p] + 1; for (auto i : E[x]) if (i != p) dfs(i, x); } int lca(int x, int y) { if (d[x] < d[y]) swap(x, y); for (int i = 19; i >= 0; i--) if (d[x] - (1 << i) >= d[y]) x = sp[x][i]; if (x == y) return x; for (int i = 19; i >= 0; i--) { if (sp[x][i] != sp[y][i]) { x = sp[x][i]; y = sp[y][i]; } } return sp[x][0]; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m, q; cin >> n >> m >> q; E.assign(n, {}); for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; x--; y--; E[x].push_back(y); E[y].push_back(x); } assert(n == 5); d.assign(n, 0); sp.assign(n, vector<int>(20, -1)); dfs(0); for (int i = 1; i < 20; i++) { for (int j = 0; j < n; j++) if (sp[j][i - 1] != -1) sp[j][i] = sp[sp[j][i - 1]][i - 1]; } vector<int> a(m); for (auto &i : a) { cin >> i; i--; } while (q--) { int tp; cin >> tp; if (tp == 1) { int ps, v; cin >> ps >> v; ps--; v--; a[ps] = v; } else { int l, r, v; cin >> l >> r >> v; l--; r--; v--; pair<int, int> ans = {-2, -2}; for (int i = l; i <= r; i++) { if (a[i] == v) { ans = {i, i}; break; } if (i != r && lca(a[i], a[i + 1]) == v) { ans = {i, i + 1}; break; } } cout << ans.first + 1 << ' ' << ans.second + 1 << '\n'; } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...