제출 #1034473

#제출 시각아이디문제언어결과실행 시간메모리
1034473peraBirthday gift (IZhO18_treearray)C++17
100 / 100
664 ms75740 KiB
#include<bits/stdc++.h> using namespace std; const int N = 2e5 + 1 , LOG = 18; int n , m , q , a[N] , d[N] , LCA[N] , up[N][LOG]; vector<int> g[N]; set<int> E[N] , O[N]; void dfs(int u , int p = 1){ up[u][0] = p; d[u] = d[p] + 1; for(int x = 0;x < (int)g[u].size();x ++){ if(g[u][x] != p){ dfs(g[u][x] , u); } } } int lca(int u , int v){ if(d[u] < d[v]){ swap(u , v); } int s = d[u] - d[v]; for(int bit = 0;bit < LOG;bit ++){ if(s >> bit & 1){ u = up[u][bit]; } } if(u == v){ return u; } for(int bit = LOG - 1;bit >= 0;bit --){ if(up[u][bit] != up[v][bit]){ u = up[u][bit]; v = up[v][bit]; } } return up[u][0]; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m >> q; for(int x = 1;x < n;x ++){ int u , v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } dfs(1); for(int bit = 1;bit < LOG;bit ++){ for(int x = 1;x <= n;x ++){ up[x][bit] = up[up[x][bit - 1]][bit - 1]; } } for(int x = 1;x <= m;x ++){ cin >> a[x]; E[a[x]].insert(x); if(x > 1){ LCA[x - 1] = lca(a[x] , a[x - 1]); O[LCA[x - 1]].insert(x - 1); } } for(int i = 1;i <= q;i ++){ int type; cin >> type; if(type == 1){ int pos , v; cin >> pos >> v; E[a[pos]].erase(pos); if(pos < m){ O[LCA[pos]].erase(pos); } if(pos > 1){ O[LCA[pos - 1]].erase(pos - 1); } a[pos] = v; E[a[pos]].insert(pos); if(pos < m){ LCA[pos] = lca(a[pos] , a[pos + 1]); O[LCA[pos]].insert(pos); } if(pos > 1){ LCA[pos - 1] = lca(a[pos - 1] , a[pos]); O[LCA[pos - 1]].insert(pos - 1); } }else{ int l , r , v; cin >> l >> r >> v; auto it = E[v].lower_bound(l); if(it != E[v].end() && *it <= r){ cout << *it << " " << *it << endl; continue; } it = O[v].lower_bound(l); if(it != O[v].end() && *it < r){ cout << *it << " " << *it + 1 << endl; continue; } cout << -1 << " " << -1 << endl; } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...