Submission #1109141

#TimeUsernameProblemLanguageResultExecution timeMemory
1109141MuhammetBirthday gift (IZhO18_treearray)C++17
56 / 100
4067 ms48968 KiB
#include <bits/stdc++.h> using namespace std; vector <vector <int>> v, sp; vector <int> h, p; void dfs(int x, int y){ h[x] = h[y]+1; p[x] = y; for(auto i : v[x]){ if(i != y){ dfs(i,x); } } } int lca(int x, int y){ if(h[x] < h[y]) swap(x,y); for(int i = 20; i >= 0; i--){ if(h[sp[x][i]] >= h[y]){ x = sp[x][i]; } } for(int i = 20; i >= 0; i--){ if(sp[x][i] != sp[y][i]){ x = sp[x][i]; y = sp[y][i]; } } if(x == y) return x; return p[x]; } int main(){ int n, m, q; cin >> n >> m >> q; v.resize(n+1); h.resize(n+1,0); for(int i = 1; i < n; i++){ int u1, u2; cin >> u1 >> u2; v[u1].push_back(u2); v[u2].push_back(u1); } p.resize(n+1); sp.resize(n+1, vector <int> (25,0)); dfs(1,0); for(int i = 1; i <= n; i++){ sp[i][0] = p[i]; } for(int j = 1; j <= 20; j++){ for(int i = 1; i <= n; i++){ sp[i][j] = sp[sp[i][j-1]][j-1]; } } vector <int> a(m+1); for(int i = 1; i <= m; i++){ cin >> a[i]; } for(int i = 1; i <= q; i++){ int t; cin >> t; if(t == 1){ int in, vl; cin >> in >> vl; a[in] = vl; } else { int l, r, vl; cin >> l >> r >> vl; bool tr = 0; for(int j = l; j < r; j++){ if(a[j] == vl){ cout << j << ' ' << j << "\n"; tr = 1; break; } if(lca(a[j],a[j+1]) == vl){ tr = 1; cout << j << ' ' << j+1 << '\n'; break; } } if(tr == 0){ if(a[r] == vl){ cout << r << ' ' << r << '\n'; } else { cout << "-1 -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...