제출 #503459

#제출 시각아이디문제언어결과실행 시간메모리
503459sumit_kk10Birthday gift (IZhO18_treearray)C++17
100 / 100
1541 ms85408 KiB
#include <bits/stdc++.h> #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL) #define ll long long int #define ld long double using namespace std; const int N = 2e5 + 5; const int MOD = 1e9 + 7; int n, m, q, lvl[N], tin[N], tout[N], timer; vector<int> graph[N]; vector<vector<int> > dp(N, vector<int>(20, -1)); void dfs(int source, int par, int level){ dp[source][0] = par; lvl[source] = level; tin[source] = ++timer; for(auto k : graph[source]) if(k != par) dfs(k, source, level + 1); tout[source] = ++timer; } void init(){ dfs(1, -1, 0); for(int i = 1; i <= (log2(n)); ++i) for(int j = 1; j <= n; ++j) if(dp[j][i - 1] != -1) dp[j][i] = dp[dp[j][i - 1]][i - 1]; } int LCA(int u, int v){ if(lvl[u] > lvl[v]) swap(u, v); ll d = lvl[v] - lvl[u]; while(d){ int jump = log2(d); v = dp[v][jump]; d -= pow(2, jump); } if(v == u) return v; for(int i = log2(n); i >= 0; --i){ if(dp[v][i] != -1 && dp[v][i] != dp[u][i]){ u = dp[u][i]; v = dp[v][i]; } } return dp[v][0]; } void solve(){ cin >> n >> m >> q; vector<int> seq; for(int i = 0; i < n - 1; ++i){ int u, v; cin >> u >> v; graph[u].push_back(v); graph[v].push_back(u); } for(int i = 0; i < m; ++i){ int x; cin >> x; seq.push_back(x); } init(); set<int> res[n + 1], val[n + 1]; for(int i = 0; i < m - 1; ++i) res[LCA(seq[i], seq[i + 1])].insert(i); for(int i = 0; i < m; ++i) val[seq[i]].insert(i); while(q--){ int query; cin >> query; if(query == 1){ int x, v; cin >> x >> v; --x; auto k = val[seq[x]].find(x); if(k != val[seq[x]].end()) val[seq[x]].erase(k); val[v].insert(x); if(x != 0){ k = res[LCA(seq[x - 1], seq[x])].find(x - 1); if(k != res[LCA(seq[x - 1], seq[x])].end()) res[LCA(seq[x - 1], seq[x])].erase(k); res[LCA(seq[x - 1], v)].insert(x - 1); } if(x != m - 1){ k = res[LCA(seq[x], seq[x + 1])].find(x); if(k != res[LCA(seq[x], seq[x + 1])].end()) res[LCA(seq[x], seq[x + 1])].erase(k); res[LCA(v, seq[x + 1])].insert(x); } seq[x] = v; } else{ int l, r, v; cin >> l >> r >> v; --l, --r; auto x = val[v].lower_bound(l); auto k = res[v].lower_bound(l); if(*x <= r and x != val[v].end()) cout << *x + 1 << ' ' << *x + 1 << '\n'; else if(k != res[v].end() and *k < r) cout << *k + 1 << ' ' << *k + 2 << '\n'; else cout << "-1 -1\n"; } } } int main(){ fast; int t = 1; // cin >> t; while(t--) solve(); 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...