Submission #1095137

#TimeUsernameProblemLanguageResultExecution timeMemory
1095137NurislamBirthday gift (IZhO18_treearray)C++17
0 / 100
0 ms348 KiB
#include <bits/stdc++.h> using namespace std; #define pb push_back #define ff first #define ss second #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define int long long template <class F, class _S> bool chmin(F &u, const _S &v){ bool flag = false; if ( u > v ){ u = v; flag |= true; } return flag; } template <class F, class _S> bool chmax(F &u, const _S &v){ bool flag = false; if ( u < v ){ u = v; flag |= true; } return flag; } const int N = (1<<18) +1, inf = 1e18+200; //int mod = 998244353; //int mod = 1000000007; //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //#define rnd(l, r) uniform_int_distribution <int> (l, r)(rng) void solve(){ int n, m, q; cin >> n >> m >> q; vector<vector<int>> g(n+1); for(int i = 1; i < n; i++){ int u, v; cin >> u >> v; g[u].pb(v); g[v].pb(u); } vector<int> tin(n+1), tout(n+1); vector<vector<int>> ln(22, vector<int> (n+1, 1)); int timer = 0; auto dfs = [&](auto dfs, int ps, int pr) -> void{ ln[0][ps] = pr; tin[ps] = timer++; for(int i = 1; i < 20; i++) ln[i][ps] = ln[i-1][ln[i-1][ps]]; for(int to:g[ps]){ if(to == pr)continue; dfs(dfs, to, ps); } tout[ps] = timer++; }; dfs(dfs, 1, 1); auto lca = [&](int a, int b) -> int { if(tin[b] <= tin[a] && tout[a] <= tout[b])return b; if(tin[a] <= tin[b] && tout[b] <= tout[a])return a; for(int i = 19; i >= 0; i--){ int na = ln[i][a]; if(tin[na] <= tin[b] && tout[b] <= tout[na])continue; a = na; } return ln[0][a]; }; vector<multiset<int>> st(n+1); vector<multiset<int>> sv(n+1); vector<int> a(m+1); for(int i = 1; i <= m; i++){ cin >> a[i]; st[a[i]].insert(i); if(i > 1)sv[lca(a[i], a[i-1])].insert(i); } while(q--){ int ty; cin >> ty; if(ty == 1){ int ps, va; cin >> ps >> va; st[a[ps]].erase(st[a[ps]].find(ps)); if(ps > 1){ int x = lca(a[ps], a[ps-1]); sv[x].erase(sv[x].find(ps)); } if(ps < m){ int x = lca(a[ps] , a[ps+1]); sv[x].erase(sv[x].find(ps+1)); } a[ps] = va; st[va].insert(ps); if(ps > 1){ int x = lca(a[ps], a[ps-1]); sv[x].insert(ps); } if(ps < m){ int x = lca(a[ps] , a[ps+1]); sv[x].insert(ps+1); } }else{ int l, r, v; cin >> l >> r >> v; if(!st[v].empty()){ //cout << *(--st[v].end()) << '\n'; auto x = st[v].lower_bound(l); if(l <= *x && *x <= r){ cout << *x << ' ' << *x << '\n'; continue; } if(*x != *(--st[v].end())){ x++; if(l <= *x && *x <= r){ cout << *x << ' ' << *x << '\n'; continue; } } } if(!sv[v].empty()){ //cout << *(--sv[v].end()) << '\n'; auto y = sv[v].lower_bound(l); if(l <= *y && *y <= r){ cout << *y-1 << ' ' << *y << '\n'; continue; } if(*y != *(--sv[v].end())){ y++; if(l <= *y && *y <= r){ cout << *y-1 << ' ' << *y << '\n'; continue; } } } cout << -1 << ' ' << -1 << '\n'; } } } signed main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int tt = 1; //cin >> tt; while(tt--){ solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...