Submission #167514

#TimeUsernameProblemLanguageResultExecution timeMemory
167514manh9203Birthday gift (IZhO18_treearray)C++17
100 / 100
1341 ms84296 KiB
#include<bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n,m,q,u,v,a[N],par[N][20],h[N],type,l,r,id; vector<int> adj[N]; set<int> pos1[N],pos2[N]; void dfs(int u,int p){ for(int v: adj[u]){ if(v != p){ h[v] = h[u] + 1; par[v][0] = u; dfs(v, u); } } } int lca(int u,int v){ if(h[u] < h[v]) swap(u, v); for(int i=18;i>=0;i--){ if(h[u] - (1<<i) >= h[v]){ u = par[u][i]; } } if(u == v){ return u; }else{ for(int i=18;i>=0;i--){ if(par[u][i] != -1 && par[v][i] != -1 && par[u][i] != par[v][i]){ u = par[u][i]; v = par[v][i]; } } return par[u][0]; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> q; for(int i=1;i<n;i++){ cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } h[1] = 1; memset(par, -1, sizeof par); dfs(1, 1); for(int j=1;(1<<j)<=n;j++){ for(int i=1;i<=n;i++){ if(par[i][j-1] != -1){ par[i][j] = par[par[i][j-1]][j-1]; } } } for(int i=1;i<=m;i++){ cin >> a[i]; pos1[a[i]].insert(i); } for(int i=1;i<m;i++){ u = lca(a[i], a[i+1]); pos2[u].insert(i); } while(q--){ cin >> type; if(type == 1){ cin >> id >> v; pos1[a[id]].erase(id); if(id > 1){ u = lca(a[id-1], a[id]); pos2[u].erase(id-1); } if(id < m){ u = lca(a[id], a[id+1]); pos2[u].erase(id); } a[id] = v; pos1[a[id]].insert(id); if(id > 1){ u = lca(a[id-1], a[id]); pos2[u].insert(id-1); } if(id < m){ u = lca(a[id], a[id+1]); pos2[u].insert(id); } } if(type == 2){ cin >> l >> r >> v; if(pos1[v].size() > 0){ auto it = pos1[v].lower_bound(l); if(it != pos1[v].end() && *it <= r){ cout << *it << " " << *it << "\n"; continue; } } if(pos2[v].size() > 0){ auto it1 = pos2[v].lower_bound(l); if(it1 != pos2[v].end() && *it1 < r){ cout << *it1 << " " << (*it1) + 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...