Submission #502834

#TimeUsernameProblemLanguageResultExecution timeMemory
502834blueBirthday gift (IZhO18_treearray)C++17
100 / 100
1036 ms84400 KiB
#include <iostream> #include <vector> #include <algorithm> #include <set> using namespace std; using vi = vector<int>; using vvi = vector<vi>; const int lg = 18; const int mx = 200'000; vvi anc(1+mx, vi(1+lg, 1)); vi edge[1+mx]; vi depth(1+mx, 0); void dfs(int u) { for(int v: edge[u]) { if(anc[u][0] == v) continue; depth[v] = 1 + depth[u]; anc[v][0] = u; dfs(v); } } int lca(int u, int v) { if(depth[u] > depth[v]) swap(u, v); for(int e = 0; e <= lg; e++) if((depth[v] - depth[u]) & (1 << e)) v = anc[v][e]; if(u == v) return u; for(int e = lg; e >= 0; e--) { if(anc[u][e] != anc[v][e]) { u = anc[u][e]; v = anc[v][e]; } } return anc[u][0]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, q; cin >> n >> m >> q; for(int e = 1; e <= n-1; e++) { int u, v; cin >> u >> v; edge[u].push_back(v); edge[v].push_back(u); } dfs(1); for(int e = 1; e <= lg; e++) for(int u = 1; u <= n; u++) anc[u][e] = anc[ anc[u][e-1] ][e-1]; vi a(1+m); for(int i = 1; i <= m; i++) cin >> a[i]; vi L(m); for(int i = 1; i < m; i++) L[i] = lca(a[i], a[i+1]); // for(int i = 1; i < m; i++) cerr << i << " : " << L[i] << '\n'; set<int> occ[1+n]; for(int i = 1; i < m; i++) occ[L[i]].insert(i); set<int> occ_i[1+n]; for(int i = 1; i <= m; i++) occ_i[a[i]].insert(i); // for(int v = 1; v <= n; v++) // { // cerr << v << " : "; // for(int o: occ_i[v]) cerr << o << ' '; // cerr << '\n'; // } for(int x = 1; x <= q; x++) { int t; cin >> t; if(t == 1) { int p, v; cin >> p >> v; if(p > 1) occ[L[p-1]].erase(p-1); if(p < m) occ[L[p]].erase(p); occ_i[a[p]].erase(p); a[p] = v; if(p > 1) L[p-1] = lca(a[p-1], a[p]); if(p < m) L[p] = lca(a[p], a[p+1]); if(p > 1) occ[L[p-1]].insert(p-1); if(p < m) occ[L[p]].insert(p); occ_i[a[p]].insert(p); // for(int i = 1; i <= m; i++) cerr << a[i] << ' '; // cerr << '\n'; // for(int i = 1; i < m; i++) cerr << i << " : " << L[i] << '\n'; } else { int l, r, v; cin >> l >> r >> v; auto z = occ[v].lower_bound(l); if(z == occ[v].end() || *z >= r) { // cerr << "#\n"; // cerr << "oiv = "; // for(int o: occ_i[v]) cerr << o << ' '; // cerr << '\n'; auto z2 = occ_i[v].lower_bound(l); if(z2 == occ_i[v].end() || *z2 > r) cout << "-1 -1\n"; else cout << *z2 << ' ' << *z2 << '\n'; } else cout << *z << ' ' << (*z)+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...