Submission #946195

#TimeUsernameProblemLanguageResultExecution timeMemory
946195MinaRagy06Prize (CEOI22_prize)C++17
0 / 100
2598 ms459408 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define SZ(x) (int) x.size() const int N = 1'000'005; struct gradingtree { int n, k, root; vector<int> P, W; int t; int st[N], en[N], anc[N][20]; vector<array<int, 2>> adj[N]; int d[N]; void dfs(int i, int p, int dep) { anc[i][0] = p; d[i] = dep; for (int j = 1; j < 20; j++) { anc[i][j] = anc[anc[i][j - 1]][j - 1]; } st[i] = t++; for (auto [nxt, w] : adj[i]) { dfs(nxt, i, dep + w); } en[i] = t - 1; } void init(int _n, int _k, vector<int> _p, vector<int> _w) { n = _n, k = _k, P = _p, W = _w; for (int i = 1; i <= n; i++) { if (P[i] == -1) { root = i; } else { adj[P[i]].push_back({i, W[i]}); } } dfs(root, root, 0); // cout << "> "; // for (int i = 1; i <= n; i++) { // cout << d[i] << ' '; // } // cout << '\n'; } int lca(int a, int b) { if (st[a] <= st[b] && en[b] <= en[a]) { return a; } for (int j = 19; j >= 0; j--) { int c = anc[a][j]; if (!(st[c] <= st[b] && en[b] <= en[c])) { a = c; } } return anc[a][0]; } int dist(int a, int b) { int l = lca(a, b); // cout << "> " << a << ' ' << b << ": " << d[a] + d[b] - 2 * d[l] << '\n'; return d[a] + d[b] - 2 * d[l]; } } gt[2]; struct tree { int n, k, root; vector<int> P, adj[N], gud; bool vis[N]; int t; int st[N], en[N], anc[N][20]; void dfs(int i, int p) { anc[i][0] = p; for (int j = 1; j < 20; j++) { anc[i][j] = anc[anc[i][j - 1]][j - 1]; } st[i] = t++; for (auto nxt : adj[i]) { dfs(nxt, i); } en[i] = t - 1; } void init(int _n, int _k, vector<int> _p, vector<int> _gud) { n = _n, k = _k, P = _p, gud = _gud; for (int i = 1; i <= n; i++) { vis[i] = 0; if (P[i] == -1) { root = i; } else { adj[P[i]].push_back(i); } } dfs(root, root); } int lca(int a, int b) { if (st[a] <= st[b] && en[b] <= en[a]) { return a; } for (int j = 19; j >= 0; j--) { int c = anc[a][j]; if (!(st[c] <= st[b] && en[b] <= en[c])) { a = c; } } return anc[a][0]; } vector<array<int, 2>> adj2[N]; int d[N]; void dfs2(int i, int c) { d[i] = c; vis[i] = 1; // cout << i << ' ' << d[i] << '\n'; for (auto [nxt, w] : adj2[i]) { if (vis[nxt]) continue; dfs2(nxt, c + w); } } void process2() { sort(gud.begin(), gud.end(), [&] (int x, int y) {return st[x] < st[y];}); vector<int> v = gud; for (int i = 1; i < SZ(gud); i++) { v.push_back(lca(gud[i - 1], gud[i])); } sort(v.begin(), v.end(), [&] (int x, int y) {return st[x] < st[y];}); dfs2(v[0], 0); } int dist(int a, int b) { int l = lca(a, b); return d[a] + d[b] - 2 * d[l]; } } t[2]; vector<array<int, 2>> toask; vector<int> V; void choose(vector<int> gud) { #ifndef MINA for (auto i : gud) { cout << i << ' '; } cout << endl; #else V = gud; #endif } void ask(int a, int b) { toask.push_back({a, b}); } vector<array<int, 6>> get() { #ifndef MINA for (auto [a, b] : toask) { cout << "? " << a << ' ' << b << '\n'; } cout << "!" << endl; #endif vector<array<int, 6>> ret; for (auto [a, b] : toask) { int al1, bl1, al2, bl2; #ifndef MINA cin >> al1 >> bl1 >> al2 >> bl2; #else int l1 = gt[0].lca(a, b), l2 = gt[1].lca(a, b); al1 = gt[0].dist(a, l1); bl1 = gt[0].dist(b, l1); al2 = gt[1].dist(a, l2); bl2 = gt[1].dist(b, l2); cout << "? " << a << ' ' << b << "(" << l1 << ", " << l2 << "): " << al1 << ' ' << bl1 << ' ' << al2 << ' ' << bl2 << '\n'; #endif ret.push_back({a, b, al1, bl1, al2, bl2}); } return ret; } int n, m; void init(int _n, int _m, vector<int> p1, vector<int> p2) { n = _n, m = _m; vector<int> gud; for (int i = 1; i <= m; i++) { gud.push_back(i); } choose(gud); t[0].init(n, m, p1, gud); t[1].init(n, m, p2, gud); for (int i = 1; i < SZ(gud); i++) { ask(gud[i - 1], gud[i]); } vector<array<int, 6>> v = get(); for (auto [a, b, al1, bl1, al2, bl2] : v) { int al[2] = {al1, al2}; int bl[2] = {bl1, bl2}; for (int k = 0; k < 2; k++) { int l = t[k].lca(a, b); t[k].adj2[l].push_back({a, al[k]}); t[k].adj2[a].push_back({l, -al[k]}); t[k].adj2[l].push_back({b, bl[k]}); t[k].adj2[b].push_back({l, -bl[k]}); } } for (int k = 0; k < 2; k++) { t[k].process2(); // cout << "=========\n"; } } array<int, 2> query(int x, int y) { return {t[0].dist(x, y), t[1].dist(x, y)}; } void solve() { int _n, _k, _q, _t; cin >> _n >> _k >> _q >> _t; vector<int> p1(_n + 1), p2(_n + 1); for (int i = 1; i <= _n; i++) { cin >> p1[i]; } for (int i = 1; i <= _n; i++) { cin >> p2[i]; } init(_n, _k, p1, p2); vector<array<int, 2>> ans; while (_t--) { int a, b; cin >> a >> b; ans.push_back(query(a, b)); } for (auto [d1, d2] : ans) { cout << d1 << ' ' << d2 << '\n'; } cout << endl; } void grader() { int _n, _k, _q, _t; cin >> _n >> _k >> _q >> _t; vector<int> p1(_n + 1), w1(_n + 1), p2(_n + 1), w2(_n + 1); for (int i = 1; i <= _n; i++) { cin >> p1[i]; } for (int i = 1; i <= _n; i++) { cin >> p2[i]; } for (int i = 1; i <= _n; i++) { cin >> w1[i]; } for (int i = 1; i <= _n; i++) { cin >> w2[i]; } gt[0].init(_n, _k, p1, w1); gt[1].init(_n, _k, p2, w2); init(_n, _k, p1, p2); vector<array<int, 2>> ans; while (_t--) { int a, b; cin >> a >> b; ans.push_back(query(a, b)); } for (auto [d1, d2] : ans) { cout << d1 << ' ' << d2 << '\n'; } cout << endl; } int main() { ios_base::sync_with_stdio(0), cin.tie(0); #ifdef MINA grader(); #else solve(); #endif 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...
#Verdict Execution timeMemoryGrader output
Fetching results...