제출 #1218523

#제출 시각아이디문제언어결과실행 시간메모리
1218523M_SH_OBirthday gift (IZhO18_treearray)C++20
100 / 100
744 ms109332 KiB
#include <bits/stdc++.h> //#include "grader.h" /*#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp>*/ #define ll long long #define ll1 long long #define ull unsigned long long #define dou long double #define str string #define vll vector<ll> #define vi vector<int> #define pll pair<ll, ll> #define vpll vector<pll> #define vbool vector<bool> #define vstr vector<str> #define vvll vector<vll> #define pb push_back #define pf push_front //#define endl "\n" #define fr first #define se second // #define sortcmp(a) sort(a.begin(), a.end(), cmp) #define sort(a) sort(a.begin(), a.end()) #define all(a) a.begin(), a.end() #define reverse(a) reverse(a.begin(), a.end()) #define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define INF 1000000000000000000 #define ordered_set tree<pll, null_type, less<pll>, rb_tree_tag, tree_order_statistics_node_update> using namespace std; //using namespace __gnu_pbds; mt19937 rng(1488); ll randll(ll l, ll r){ return uniform_int_distribution<ll>(l, r)(rng); } /*vvll s1; vll p; ll find(ll v) { if (p[v] == v) return v; return p[v] = find(p[v]); } vll unite(ll a, ll b) { a = find(a); b = find(b); if (a == b) return {}; if (s1[a].size() < s1[b].size()) { p[a] = b; for (auto i : s1[a]) { s1[b].pb(i); } return s1[a]; } else { p[b] = a; for (auto i : s1[b]) { s1[a].pb(i); } return s1[b]; } }*/ vvll g; vll d; vector<multiset<ll>> tree1; /*void push(ll v, ll tl, ll tr) { if (tl == tr) return; tree1[v*2] += d[v]; tree1[v*2+1] += d[v]; d[v*2] += d[v]; d[v*2+1] += d[v]; d[v] = 0; tree1[v] = tree1[v*2]+tree1[v*2+1]; }*/ ll t = 0; vll tin, tout, c; vvll pr; void dfs(ll v, ll p = -1) { c.pb(v); for (int i = 0; i < 20; i ++) { if ((1 << i) < c.size()) pr[v][i] = c[c.size()-1-(1 << i)]; } tin[v] = t ++; for (int i : g[v]) { if (i == p) continue; dfs(i, v); } tout[v] = t ++; c.pop_back(); } bool f(ll a, ll b) { if (tin[a] <= tin[b] && tout[a] >= tout[b]) return 1; return 0; } ll lca(ll a, ll b) { if (f(a, b)) return a; if (f(b, a)) return b; ll x = a; for (int i = 19; i >= 0; i --) { if (!f(pr[x][i], b)) { x = pr[x][i]; } //cout << i << ' ' << x << endl; } return pr[x][0]; }/* void bt(ll v, ll tl, ll tr, vll& a, vll& b) { if (tl == tr) { tree1[v].insert(a[tl]); tree1[v].insert(b[tl]); return; } ll tm = (tl+tr)/2; bt(v*2, tl, tm, a, b); bt(v*2, tm+1, tr, a, b); for (int i : tree1[v*2]) { tree1[v].insert(i); } for (int i : tree1[v*2+1]) { tree1[v].insert(i); } } ll get(ll l, ll r, ll k, ll v, ll tl, ll tr) { if (l <= tl && tr <= r) { if (tree1[v].find(k) != tree1[v].end()) return 1; return 0; } if (tl > r || tr < l) return 0; ll tm = (tl+tr)/2; //push(v, tl, tr); return max(get(l, r, k, v*2, tl, tm), get(l, r, k, v*2+1, tm+1, tr)); } void update(ll idx, ll val, ll newval, ll val_a, ll newval_a, ll v, ll tl, ll tr) { tree1[v].erase(tree1[v].find(val)); tree1[v].insert(newval); tree1[v].erase(tree1[v].find(val_a)); tree1[v].insert(newval_a); if (tl == tr) { //tree1[v] += val; //d[v] += val; //push(v, tl, tr); return; } ll tm = (tl+tr)/2; //push(v, tl, tr); if (idx <= tm)update(idx, val, newval, val_a, newval_a, v*2, tl, tm); else update(idx, val, newval, val_a, newval_a, v*2+1, tm+1, tr); }*/ int main() { ll n, m, q; cin >> n >> m >> q; g.resize(n+7); pr.resize(n+7, vll(20, 1)); vll a(m); tin.resize(n+7); tout.resize(n+7); for (int i = 0; i < n-1; i ++) { ll a, b; cin >> a >> b; g[a].pb(b); g[b].pb(a); } dfs(1); vector<set<ll>> s(n+7), s1(n+7); /*cout << lca(5, 4) << endl; return 0;*/ for (int i = 0; i < m; i ++) { cin >> a[i]; s[a[i]].insert(i); } vll b(m-1); for (int i =0 ; i < m-1; i ++) { b[i] = lca(a[i], a[i+1]); s1[b[i]].insert(i); //cout << b[i] << ' '; } //cout << endl; while (q --) { ll x; cin >> x; if (x == 2) { ll l, r, v; cin >> l >> r >> v; l --; r --; if (s[v].lower_bound(l) != s[v].end() && *s[v].lower_bound(l) <= r) { ll x = *s[v].lower_bound(l); cout << x+1 << ' ' << x+1 << endl; } else if (s1[v].lower_bound(l) != s1[v].end() && *s1[v].lower_bound(l) < r) { ll x = *s1[v].lower_bound(l); cout << x+1 << ' ' << x+2 << endl; } else cout << -1 << ' ' << -1 << endl; } else { ll pos, val; cin >> pos >> val; pos --; s[a[pos]].erase(pos); if (pos != 0) s1[b[pos-1]].erase(pos-1); if (pos != m-1) s1[b[pos]].erase(pos); a[pos] = val; s[val].insert(pos); if (pos != 0) { b[pos-1] = lca(a[pos-1], a[pos]); s1[b[pos-1]].insert(pos-1); } if (pos != m-1){ b[pos] = lca(a[pos], a[pos+1]); s1[b[pos]].insert(pos); } } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...