Submission #315603

#TimeUsernameProblemLanguageResultExecution timeMemory
315603spike1236Birthday gift (IZhO18_treearray)C++14
56 / 100
1229 ms84348 KiB
#pragma GCC target("avx2") #pragma GCC optimize("O3") #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 = 2; i <= m; ++i) st[lca(a[i], a[i - 1])].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); if(pos < n) st[lca(a[pos], a[pos + 1])].erase(pos + 1); a[pos] = v; poses[a[pos]].insert(pos); if(pos > 1) st[lca(a[pos], a[pos - 1])].insert(pos); if(pos < n) st[lca(a[pos], a[pos + 1])].insert(pos + 1); } 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(l <= k && k <= r) { cout << k << ' ' << k << '\n'; continue; } } } auto it = st[v].upper_bound(l); int ans1 = -1, ans2 = -1; if(it != st[v].end()) { int cur = *it; if(l < cur && cur <= r) { ans1 = cur - 1; ans2 = cur; } } cout << ans1 << ' ' << ans2 << '\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...