Submission #315620

#TimeUsernameProblemLanguageResultExecution timeMemory
315620spike1236Birthday gift (IZhO18_treearray)C++14
56 / 100
1214 ms76664 KiB
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 10; int n, m, q; int tin[MAXN], tout[MAXN]; int up[MAXN][19]; vector <int> g[MAXN]; set <int> st[MAXN], poses[MAXN]; int a[MAXN]; int tmr; void dfs(int v = 1, int p = 0) { tin[v] = ++tmr; up[v][0] = p; for(int i = 1; i < 18; ++i) if(up[v][i - 1]) up[v][i] = up[up[v][i - 1]][i - 1]; for(auto to : g[v]) if(to != p) dfs(to, v); tout[v] = ++tmr; } bool upper(int v, int u) { return (tin[v] <= tin[u] && tout[v] >= tout[u]); } int lca(int a, int b) { if(upper(a, b)) return a; if(upper(b, a)) return b; for(int i = 17; i >= 0; --i) if(up[a][i] && !upper(up[a][i], b)) a = up[a][i]; return up[a][0]; } void solve() { cin >> n >> m >> q; for(int i = 1; i < n; ++i) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } dfs(); for(int i = 1; i <= m; ++i) cin >> a[i], poses[a[i]].insert(i); for(int i = 1; i < m; ++i) st[lca(a[i + 1], a[i])].insert(i); for(int i = 1; i <= q; ++i) { int type; cin >> type; if(type == 1) { int pos, v; cin >> pos >> v; poses[a[pos]].erase(pos); if(pos > 1) st[lca(a[pos], a[pos - 1])].erase(pos - 1); if(pos < n) st[lca(a[pos], a[pos + 1])].erase(pos); a[pos] = v; poses[a[pos]].insert(pos); if(pos > 1) st[lca(a[pos], a[pos - 1])].insert(pos - 1); if(pos < n) st[lca(a[pos], a[pos + 1])].insert(pos); } else { int l, r, v; cin >> l >> r >> v; if(poses[v].size()) { auto it = poses[v].lower_bound(l); if(it != poses[v].end()) { int k = *it; if(k <= r) { cout << k << ' ' << k << '\n'; continue; } } } if(st[v].size()) { auto it = st[v].lower_bound(l); if(it != st[v].end()) { int cur = *it; if(cur < r) { cout << cur << ' ' << cur + 1 << '\n'; continue; } } } cout << "-1 -1\n"; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T = 1; ///cin >> T; while(T--) solve(), 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...