Submission #636076

#TimeUsernameProblemLanguageResultExecution timeMemory
636076pedroslreyBirthday gift (IZhO18_treearray)C++17
100 / 100
925 ms80564 KiB
#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 10; const int MAXK = 19; vector<int> graph[MAXN]; int dp[MAXN][MAXK]; int as[MAXN], prof[MAXN]; set<int> pos[MAXN], pos_lca[MAXN]; void dfs(int u, int p) { for (int v: graph[u]) { if (v == p) continue; prof[v] = prof[u] + 1; dp[v][0] = u; for (int k = 1; k < MAXK; ++k) dp[v][k] = dp[dp[v][k-1]][k-1]; dfs(v, u); } } int lca(int a, int b) { if (prof[a] > prof[b]) swap(a, b); int d = prof[b] - prof[a]; for (int k = 0; k < MAXK; ++k) if (d & (1 << k)) b = dp[b][k]; if (a == b) return a; for (int k = MAXK - 1; k >= 0; --k) if (dp[a][k] != dp[b][k]) { a = dp[a][k]; b = dp[b][k]; } return dp[a][0]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, q; cin >> n >> m >> q; for (int i = 0; i < n-1; ++i) { int u, v; cin >> u >> v; graph[u].push_back(v); graph[v].push_back(u); } dfs(1, 1); for (int i = 1; i <= m; ++i) cin >> as[i]; for (int i = 1; i <= m; ++i) { pos[as[i]].insert(i); if (i < m) pos_lca[lca(as[i], as[i+1])].insert(i); } for (int qq = 0; qq < q; ++qq) { int t; cin >> t; if (t == 1) { int i, v; cin >> i >> v; pos[as[i]].erase(i); if (i > 1) pos_lca[lca(as[i-1], as[i])].erase(i-1); if (i < m) pos_lca[lca(as[i], as[i+1])].erase(i); as[i] = v; pos[as[i]].insert(i); if (i > 1) pos_lca[lca(as[i-1], as[i])].insert(i-1); if (i < m) pos_lca[lca(as[i], as[i+1])].insert(i); } else { int l, r, v; cin >> l >> r >> v; auto b = pos[v].lower_bound(l); if (b != pos[v].end() && *b <= r) { cout << *b << " " << *b << "\n"; continue; } auto blca = pos_lca[v].lower_bound(l); if (blca != pos_lca[v].end() && *blca + 1 <= r) { cout << *blca << " " << *blca + 1 << "\n"; continue; } 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...