Submission #752327

#TimeUsernameProblemLanguageResultExecution timeMemory
752327iulia13Birthday gift (IZhO18_treearray)C++14
100 / 100
1549 ms87068 KiB
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; const int LG = 20; vector <int> g[N]; set <int> L[N]; set <int> V[N]; int dad[N][LG]; int lvl[N], a[N], n; void dfs(int nod) { for (auto x : g[nod]) { if (x == dad[nod][0]) continue; dad[x][0] = nod; lvl[x] = lvl[nod] + 1; dfs(x); } } void lifting() { for (int j = 1; j < LG; j++) for (int i = 1; i <= n; i++) dad[i][j] = dad[dad[i][j - 1]][j - 1]; } int lca(int x, int y) { if (lvl[x] < lvl[y]) swap(x, y); int lg = LG - 1; while (lvl[x] > lvl[y] && lg != -1) { if (lvl[dad[x][lg]] >= lvl[y]) x = dad[x][lg]; lg--; } lg = LG - 1; while (x != y && lg != -1) { if (dad[x][lg] != dad[y][lg]) { x = dad[x][lg]; y = dad[y][lg]; } lg--; } if (x == y) return x; return dad[x][0]; } int main() { int m, q; cin >> n >> m >> q; for (int i = 1; i < n; i++) { int x, y; cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } dfs(1); lifting(); for (int i = 1; i <= m; i++) { cin >> a[i]; V[a[i]].insert(i); if (1 < i) L[lca(a[i], a[i - 1])].insert(i); } while(q--) { int tip; cin >> tip; if (tip == 1) { int pos, v; cin >> pos >> v; V[a[pos]].erase(pos); V[v].insert(pos); if (pos != 1) { L[lca(a[pos], a[pos - 1])].erase(pos); L[lca(v, a[pos - 1])].insert(pos); } if (pos != m) { L[lca(a[pos], a[pos + 1])].erase(pos + 1); L[lca(v, a[pos + 1])].insert(pos + 1); } a[pos] = v; continue; } int l, r,val; cin >> l >> r >> val; auto it = V[val].lower_bound(l); if (it != V[val].end()) { int ll = *it; if (ll <= r) { cout << ll << " " << ll << '\n'; continue; } } it = L[val].lower_bound(l + 1); if (it != L[val].end()) { int lll = *it; if (lll <= r) cout << lll - 1 << " " << lll; else cout << -1 << " " << -1; cout << '\n'; continue; } cout << -1 << " " << -1; cout << '\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...