Submission #671863

#TimeUsernameProblemLanguageResultExecution timeMemory
671863Alihan_8Birthday gift (IZhO18_treearray)C++17
100 / 100
1395 ms95184 KiB
#include <bits/stdc++.h> // include <ext/pb_ds/assoc_container.hpp> // include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; using namespace std; #define all(x) x.begin(), x.end() #define pb push_back // define ordered_set tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> #define mpr make_pair #define ln '\n' void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);} #define int long long const int N = 2e5+1; vector <int> g[N]; int val[N], lift[N][20], depth[N]; void dfs(int n, int p){ depth[n] = depth[p]+1; lift[n][0] = p; for ( int i = 1; i < 20; i++ ){ lift[n][i] = lift[lift[n][i-1]][i-1]; } for ( auto to: g[n] ){ if ( to == p ) continue; dfs(to, n); } } int _lca(int l, int r){ if ( depth[l] < depth[r] ) swap(l, r); int dif = depth[l]-depth[r]; for ( int i = 19; i >= 0; i-- ){ if ( dif >> i & 1 ) l = lift[l][i]; } if ( l == r ) return l; for ( int i = 19; i >= 0; i-- ){ if ( lift[l][i] == lift[r][i] ) continue; l = lift[l][i], r = lift[r][i]; } return lift[l][0]; } signed main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, q; cin >> n >> m >> q; vector <set<int>> st(N); set <pair<int,int>> lca_list; for ( int i = 1; i < n; i++ ){ int x, y; cin >> x >> y; g[x].pb(y), g[y].pb(x); } dfs(1, 0); for ( int i = 1; i <= m; i++ ){ cin >> val[i]; st[val[i]].insert(i); if ( i > 1 ) lca_list.insert({_lca(val[i-1], val[i]), i-1}); } while ( q-- ){ int t; cin >> t; if ( t == 1 ){ int pos, v; cin >> pos >> v; st[val[pos]].erase(pos); st[v].insert(pos); if ( pos > 1 ){ lca_list.erase({_lca(val[pos-1], val[pos]), pos-1}); lca_list.insert({_lca(val[pos-1], v), pos-1}); } if ( pos+1 <= m ){ lca_list.erase({_lca(val[pos+1], val[pos]), pos}); lca_list.insert({_lca(val[pos+1], v), pos}); } val[pos] = v; } else{ int l, r, x; cin >> l >> r >> x; auto f = st[x].lower_bound(l); if ( f != st[x].end() and *f <= r ){ cout << *f << ' ' << *f << ln; continue; } auto it = lca_list.lower_bound({x, l}); if ( it->first == x and it->second < r ) cout << it->second << ' ' << it->second+1 << ln; else cout << "-1 -1\n"; } } cout << '\n'; }

Compilation message (stderr)

treearray.cpp: In function 'void IO(std::string)':
treearray.cpp:11:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:11:70: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                                                               ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...