Submission #921159

#TimeUsernameProblemLanguageResultExecution timeMemory
921159phongBirthday gift (IZhO18_treearray)C++17
100 / 100
911 ms73888 KiB
#pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma") #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #define ll long long const int nmax = 2e5 + 100; const ll oo = 1e18, base = 311; const int lg = 19, M =2e2 + 5; const ll mod = 1e9 + 7; #define pii pair<ll, int> #define fi first #define se second #define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' '; using namespace std; int n, m, q, a[nmax], h[nmax], up[nmax][lg + 1]; set<int> mt[nmax][2]; vector<int> adj[nmax]; void dfs(int u, int p){ for(auto v : adj[u]){ if(v == p) continue; h[v] = h[u] + 1; up[v][0] = u; for(int j = 1; j <= lg; ++j) up[v][j] = up[up[v][j - 1]][j - 1]; dfs(v, u); } } int lca(int u, int v){ if(h[u] != h[v]){ if(h[u] < h[v]) swap(u, v); int k = h[u] - h[v]; for(int j = 0; j <= __lg(k); ++j){ if(k >> j & 1){ u = up[u][j]; } } } if(u == v) return u; int k = h[u]; for(int j = __lg(k); j >= 0; --j){ if(up[u][j] != up[v][j]){ u = up[u][j]; v = up[v][j]; } } return up[u][0]; } void update(int i, int val){ int x = a[i]; mt[x][0].erase(i); mt[val][0].insert(i); if(i > 1){ x = lca(a[i], a[i - 1]); mt[x][1].erase(i); x = lca(val, a[i - 1]); mt[x][1].insert(i); } if(i < m){ x = lca(a[i + 1], a[i]); mt[x][1].erase(i + 1); x = lca(val, a[i + 1]); mt[x][1].insert(i + 1); } a[i] = val; } pii get(int l, int r, int v){ if(mt[v][0].size()){ auto x = mt[v][0].lower_bound(l); auto y = *x; if(x != mt[v][0].end() && y <= r) return {y, y}; } if(mt[v][1].size()){ auto x = mt[v][1].upper_bound(l); auto y = *x; if(x != mt[v][1].end() && y <= r) return {y - 1, y}; } // cout << y << ' '; return {-1, -1}; } main(){ ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); // freopen("code.inp", "r", stdin); // freopen("code.out", "w", stdout); cin >> n >> m >> q; for(int i = 1, u, v; i < n; ++i){ cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } dfs(1, -1); for(int i = 1; i <= m; ++i){ cin >> a[i]; mt[a[i]][0].insert(i); if(i > 1) mt[lca(a[i], a[i - 1])][1].insert(i); } // cout << *mt[1][1].lower_bound(1); int t, u, x, l, r; while(q--){ cin >> t; if(t == 1){ cin >> u >> x; update(u, x); } else{ cin >> l >> r >> x; pii tmp = get(l, r, x); cout << tmp.fi << ' ' << tmp.se << "\n"; } } } /* 5 4 4 1 2 3 1 3 4 5 3 4 5 2 3 2 1 3 1 1 3 5 2 3 4 5 2 1 3 1 */

Compilation message (stderr)

treearray.cpp:84:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   84 | main(){
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...