제출 #1314578

#제출 시각아이디문제언어결과실행 시간메모리
1314578hrantsargsyanGift (IZhO18_nicegift)C++20
0 / 100
1 ms404 KiB
#include <iostream> #include <vector> using namespace std; vector<int> sequence; const int N = 2e5 + 5; const int LOG = 40; vector<int> adj[N]; int up[N][LOG]; int dept[N]; void dfs(int node, int parent) { for (auto i : adj[node]) { if (i == parent) continue; up[i][0] = node; for (int j = 1;j < LOG;++j) { up[i][j] = up[up[i][j - 1]][j - 1]; } dept[i] = dept[node] + 1; dfs(i, node); } } int lift(int a, int k) { for (int j = LOG - 1;j >= 0;--j) { if (k & (1 << j)) { a = up[a][j]; } } return a; } int lca(int a, int b) { if (a == b) return a; if (dept[a] < dept[b]) { swap(a, b); } int dif = dept[a] - dept[b]; a = lift(a, dif); 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() { int n, m, q; cin >> n >> m >> q; for (int i = 0;i < n - 1;++i) { int x, y; cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } dept[1] = 0; dfs(1, 0); for (int i = 0;i < m;++i) { int x; cin >> x; sequence.push_back(x); } for (int i = 0;i < q;++i) { int type; cin >> type; if (type == 1) { int ind, v; cin >> ind >> v; ind--; sequence[ind] = v; } else if (type == 2) { int l, r, v; cin >> l >> r >> v; --l; --r; bool flag = false; for (int j = l;j <= r;++j) { int x = sequence[j]; for (int k = l;k <= r;++k) { x = lca(x, sequence[k]); if (x == v) { cout << j+1 << " " << k+1 << endl; flag = true; break; } } if (flag) { break; } } if (!flag) { cout << -1 << " " << -1 << endl; } } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...