Submission #167570

#TimeUsernameProblemLanguageResultExecution timeMemory
167570DrumpfTheGodEmperorBirthday gift (IZhO18_treearray)C++14
0 / 100
82 ms80376 KiB
#include <bits/stdc++.h> #define bp __builtin_popcountll #define pb push_back #define in(s) freopen(s, "r", stdin); #define out(s) freopen(s, "w", stdout); #define inout(s, end1, end2) freopen((string(s) + "." + end1).c_str(), "r", stdin),\ freopen((string(s) + "." + end2).c_str(), "w", stdout); #define fi first #define se second #define bw(i, r, l) for (int i = r - 1; i >= l; i--) #define fw(i, l, r) for (int i = l; i < r; i++) #define fa(i, x) for (auto i: x) using namespace std; const int mod = 1e9 + 7, inf = 1061109567; const long long infll = 4557430888798830399; typedef pair<int, int> ii; const int N = 2e5 + 5; vector<int> g[N]; int n, m, q, a[N], dep[N], par[N][20], b[N]; void dfs(int u, int p) { par[u][0] = p; fa (v, g[u]) if (v != p) { dep[v] = dep[u] + 1; dfs(v, u); } } void sparse() { fw (j, 1, 20) fw (i, 0, n) if (par[i][j - 1] != -1) { par[i][j] = par[par[i][j - 1]][j - 1]; } } int getLCA(int u, int v) { if (dep[u] < dep[v]) swap(u, v); int diff = dep[u] - dep[v]; fw (i, 0, 20) if (diff & (1 << i)) u = par[u][i]; if (u == v) return u; bw (i, 20, 0) if (par[u][i] != par[v][i]) { u = par[u][i], v = par[v][i]; } return par[u][0]; } class SegTree { private: int type; set<ii> t[N << 2]; void build(int l, int r, int x) { if (l != r) { int m = (l + r) >> 1; build(l, m, x << 1), build(m + 1, r, x << 1 | 1); } fw (i, l, r + 1) { if (!type) t[x].insert(ii(a[i], i)); else t[x].insert(ii(b[i], i)); } } void rem(int l, int r, int pos, int val, int x) { if (l != r) { int m = (l + r) >> 1; if (pos <= m) rem(l, m, pos, val, x << 1); else rem(m + 1, r, pos, val, x << 1 | 1); } t[x].erase(ii(val, pos)); } void ins(int l, int r, int pos, int val, int x) { if (l != r) { int m = (l + r) >> 1; if (pos <= m) ins(l, m, pos, val, x << 1); else ins(m + 1, r, pos, val, x << 1 | 1); } t[x].insert(ii(val, pos)); } int check(int l, int r, int s, int e, int val, int x) { if (l > e || r < s) return -1; if (s <= l && r <= e) { auto tmp = t[x].lower_bound(ii(val, -1)); if (tmp == t[x].end()) return -1; // cout << "check " << val << " in range " << l << " " << r << "\n"; ii lol = *tmp; // cout << lol.fi << " " << lol.se << "\n"; if (lol.fi == val) return lol.se; return -1; } int m = (l + r) >> 1; return max(check(l, m, s, e, val, x << 1), check(m + 1, r, s, e, val, x << 1 | 1)); } public: int n; void init(int _n, int _type) { n = _n; type = _type; build(0, n - 1, 1); } void rem(int pos, int val) { rem(0, n - 1, pos, val, 1); } void ins(int pos, int val) { ins(0, n - 1, pos, val, 1); } int check(int l, int r, int val) { if (l > r) return -1; return check(0, n - 1, l, r, val, 1); } } st, st2; signed main() { #ifdef BLU in("blu.inp"); #endif ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m >> q; fw (i, 1, n) { int u, v; cin >> u >> v; u--, v--; g[u].pb(v), g[v].pb(u); } fw (i, 0, m) cin >> a[i], a[i]--; dfs(0, -1); sparse(); //b[i] = lca(a[i], a[i + 1]) fw (i, 0, n - 1) b[i] = getLCA(a[i], a[i + 1]); st.init(n, 0); st2.init(n - 1, 1); while (q--) { int type; cin >> type; if (type == 1) { int pos, v; cin >> pos >> v; pos--, v--; st.rem(pos, a[pos]); if (pos) st2.rem(pos - 1, b[pos - 1]); if (pos < n - 1) st2.rem(pos, b[pos]); a[pos] = v; st.ins(pos, a[pos]); if (pos) { b[pos - 1] = getLCA(a[pos - 1], a[pos]); st2.ins(pos - 1, b[pos - 1]); } if (pos < n - 1) { b[pos] = getLCA(a[pos], a[pos + 1]); st2.ins(pos, b[pos]); } } else { int l, r, v; cin >> l >> r >> v; l--, r--, v--; int x = st.check(l, r, v); if (x != -1) { // cout << "what's cookin'\n"; cout << x + 1 << " " << x + 1 << "\n"; } else { x = st2.check(l, r - 1, v); if (x != -1) cout << x + 1 << " " << x + 2 << "\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...