Submission #851022

#TimeUsernameProblemLanguageResultExecution timeMemory
851022wakandaaaBirthday gift (IZhO18_treearray)C++17
100 / 100
1142 ms191336 KiB
#include <bits/stdc++.h> using namespace std; #define file "" #define mp make_pair #define fi first #define se second #define all(x) x.begin(), x.end() #define getbit(x, i) (((x) >> (i)) & 1) #define bit(x) (1LL << (x)) #define popcount __builtin_popcountll mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count()); int rand(int l, int r) { return l + rd() % (r - l + 1); } const int N = 1e6 + 5; const int mod = (int)1e9 + 7; // 998244353; const int lg = 25; // lg + 1 const int oo = 1e9; const long long ooo = 1e18; template<class X, class Y> bool mini(X &a, Y b) { return a > b ? (a = b, true) : false; } template<class X, class Y> bool maxi(X &a, Y b) { return a < b ? (a = b, true) : false; } void add(int &a, int b) { a += b; if (a >= mod) a -= mod; if (a < 0) a += mod; } int n, m, q; vector<int> adj[N]; int h[N], par[N][lg + 1]; int a[N]; set<int> pos[N], f[N]; void dfs(int u, int p) { par[u][0] = p; for (int i = 1; i <= lg; ++i) par[u][i] = par[par[u][i - 1]][i - 1]; for (int v : adj[u]) if (v != p) { h[v] = h[u] + 1; dfs(v, u); } } int lca(int u, int v) { if (h[u] < h[v]) swap(u, v); for (int i = lg; i >= 0; --i) if (h[par[u][i]] >= h[v]) u = par[u][i]; if (u == v) return u; for (int i = lg; i >= 0; --i) if (par[u][i] != par[v][i]) { u = par[u][i]; v = par[v][i]; } return par[u][0]; } int main() { ios::sync_with_stdio(false); cin.tie(0); // freopen(file".inp", "r", stdin); // freopen(file".out", "w", stdout); cin >> n >> m >> q; for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } h[0] = -1; dfs(1, 0); for (int i = 1; i <= m; ++i) { cin >> a[i]; pos[a[i]].insert(i); } for (int i = 2; i <= m; ++i) { f[lca(a[i - 1], a[i])].insert(i - 1); } while (q--) { int op; cin >> op; if (op == 1) { int id, v; cin >> id >> v; if (id > 1) f[lca(a[id - 1], a[id])].erase(id - 1); if (id < m) f[lca(a[id], a[id + 1])].erase(id); pos[a[id]].erase(id); a[id] = v; if (id > 1) f[lca(a[id - 1], a[id])].insert(id - 1); if (id < m) f[lca(a[id], a[id + 1])].insert(id); pos[a[id]].insert(id); } else { int l, r, v; cin >> l >> r >> v; auto it = pos[v].lower_bound(l); if (it != pos[v].end() && *it <= r) { cout << *it << ' ' << *it << '\n'; } else { it = f[v].lower_bound(l); if (it != f[v].end() && *it + 1 <= r) { cout << *it << ' ' << *it + 1 << '\n'; } else { cout << -1 << ' ' << -1 << '\n'; } } } } 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...