Submission #526669

#TimeUsernameProblemLanguageResultExecution timeMemory
526669PurpleCrayonComparing Plants (IOI20_plants)C++17
100 / 100
1706 ms145012 KiB
#include "plants.h" #include <bits/stdc++.h> using namespace std; #define sz(v) int(v.size()) const int INF = 1e9+10; struct S { int n; vector<pair<int, int>> t; vector<int> lzy; void build(int v, int tl, int tr, const vector<int>& a) { if (tl == tr) { t[v] = make_pair(a[tl], tl); } else { int tm = (tl + tr) / 2; build(2*v, tl, tm, a), build(2*v+1, tm+1, tr, a); t[v] = min(t[2*v], t[2*v+1]); } } S() {} S(vector<int> a) { n = sz(a); t.resize(4 * n); lzy.assign(4 * n, 0); build(1, 0, n-1, a); } S(int _n): n(_n) { vector<int> a(n, INF); t.resize(4 * n); lzy.assign(4 * n, 0); build(1, 0, n-1, a); } void push(int v, int tl, int tr, int x) { if (!x) return; t[v].first += x; if (tl != tr) { lzy[2*v] += x; lzy[2*v+1] += x; } } void app(int v, int tl, int tr) { push(v, tl, tr, lzy[v]); lzy[v] = 0; } void upd(int v, int tl, int tr, int l, int r, int x) { app(v, tl, tr); if (l > r || l > tr || r < tl) return; if (l <= tl && tr <= r) { push(v, tl, tr, x); return; } int tm = (tl + tr) / 2; upd(2*v, tl, tm, l, r, x), upd(2*v+1, tm+1, tr, l, r, x); t[v] = min(t[2*v], t[2*v+1]); } void upd(int l, int r, int x) { if (l > r) { upd(1, 0, n-1, 0, r, +x); upd(1, 0, n-1, l, n-1, +x); } else { upd(1, 0, n-1, l, r, +x); } } pair<int, int> qry(int v, int tl, int tr, int l, int r) { app(v, tl, tr); if (l > r || l > tr || r < tl) return {INF, INF}; if (l <= tl && tr <= r) return t[v]; int tm = (tl + tr) / 2; return min(qry(2*v, tl, tm, l, r), qry(2*v+1, tm+1, tr, l, r)); } pair<int, int> qry(int l, int r) { if (l > r) { return min(qry(1, 0, n-1, 0, r), qry(1, 0, n-1, l, n-1)); } else { return qry(1, 0, n-1, l, r); } } void set(int pos, int x) { upd(pos, pos, -qry(pos, pos).first + x); } }; const int L = 19; vector<vector<int>> build_lift(vector<int> par) { int n = sz(par); vector<vector<int>> up(L, vector<int>(n, -1)); up[0] = par; for (int k = 1; k < L; k++) { for (int i = 0; i < n; i++) { up[k][i] = up[k-1][i] == -1 ? -1 : up[k-1][up[k-1][i]]; } } return up; } int search_lift(const vector<vector<int>>& up, int c, const auto& f) { for (int k = L-1; k >= 0; k--) { if (f(up[k][c])) { c = up[k][c]; } } return c; } int n, k; vector<int> a, b; vector<int> nxt, prv; vector<vector<int>> up_nxt, up_prv; void init(int K, vector<int> r) { n = sz(r), k = K; vector<bool> done(n); a.resize(n); nxt.assign(2*n, -1), prv.assign(2*n, -1); set<int> cand; set<int> bad_cand; auto prv_cand = [&](int x, bool which) { const auto& use = (which ? cand : bad_cand); if (!sz(use)) return INF; auto it = use.lower_bound(x); if (it != use.begin()) return *prev(it); return *use.rbegin(); }; auto nxt_cand = [&](int x, bool which) { const auto& use = (which ? cand : bad_cand); if (!sz(use)) return INF; auto it = use.upper_bound(x); if (it == use.end()) return *use.begin(); return *it; }; auto forward_dist = [&](int a, int b) { if (a == INF || b == INF) return INF; if (a < b) return b - a; return n - a + b; }; S seg(r); vector<bool> has_add(n); for (int i = 0; i < n; i++) if (!r[i]) { cand.insert(i), has_add[i] = 1; seg.upd(i, i, INF); } vector<set<int>> stop(n); vector<int> stopped(n, -1); auto add_stop = [&](int x, int y) { if (stopped[y] != -1) { stop[stopped[y]].erase(y); } stop[x].insert(y); stopped[y] = x; }; for (int i = 0; i < n; i++) if (!r[i]) { int p = prv_cand(i, true); if (forward_dist(p, i) < k) { add_stop(p, i); // stop[p].insert(i); bad_cand.insert(i); } } for (int x : bad_cand) cand.erase(x); auto prv_cand_both = [&](int x) { int one = prv_cand(x, true), two = prv_cand(x, false); if (forward_dist(one, x) < forward_dist(two, x)) return one; return two; }; auto nxt_cand_both = [&](int x) { int one = nxt_cand(x, true), two = nxt_cand(x, false); if (forward_dist(x, one) < forward_dist(x, two)) return one; return two; }; auto add_cand = [&](int x) { int p = prv_cand_both(x); if (p != INF && forward_dist(p, x) < k) { add_stop(p, x); // stop[p].push_back(x); cand.erase(x); bad_cand.insert(x); } else { bad_cand.erase(x); cand.insert(x); } int q = nxt_cand_both(x); if (q != INF && forward_dist(x, q) < k) { add_stop(x, q); // stop[x].push_back(q); cand.erase(q); bad_cand.insert(q); } }; for (int rep = 0; rep < n; rep++) { int me = *cand.begin(); a[me] = n - 1 - rep; done[me] = 1; cand.erase(me); for (int x : stop[me]) if (!done[x]) { add_cand(x); } int L = ((me - k + 1) + n) % n, R = (me - 1 + n) % n; seg.upd(L, R, -1); for (auto C = seg.qry(L, R); C.first == 0; C = seg.qry(L, R)) { int c = C.second; add_cand(c); seg.upd(c, c, +INF); } } for (int rep : {0, 1}) for (int x : a) b.push_back(x); assert(sz(b) == 2*n); vector<vector<int>> loc(n); for (int i = 0; i < 2*n; i++) loc[b[i]].push_back(i); S b_seg(2*n); for (int l = n-1; l >= 0; l--) for (int i : loc[l]) { { int L = i+1, R = min(2*n, i+k)-1; if (L <= R) { auto c = b_seg.qry(L, R); if (c.first != INF) nxt[i] = c.second; } } { int L = max(0, i-k+1), R = i-1; if (L <= R) { auto c = b_seg.qry(L, R); if (c.first != INF) prv[i] = c.second; } } if (nxt[i] == INF) nxt[i] = -1; if (prv[i] == INF) prv[i] = -1; b_seg.set(i, b[i]); } // for (int x : b) cerr << x << ' '; cerr << endl; // for (int x : nxt) cerr << x << ' '; cerr << endl; // for (int x : prv) cerr << x << ' '; cerr << endl; /* for (int i = 0; i < 2*n; i++) { int c_nxt = -1; for (int j = i+1; j < min(2*n, i+k); j++) { // range min if (b[j] > b[i] && (c_nxt == -1 || b[c_nxt] > b[j])) c_nxt = j; } // cerr << "nxt: " << i << ' ' << c_nxt << ' ' << nxt[i] << endl; assert(c_nxt == nxt[i]); int c_prv = -1; for (int j = max(0, i-k+1); j < i; j++) { // range min if (b[j] > b[i] && (c_prv == -1 || b[c_prv] > b[j])) c_prv = j; } // cerr << "prv: " << i << ' ' << c_prv << ' ' << prv[i] << endl; assert(c_prv == prv[i]); } */ up_nxt = build_lift(nxt); up_prv = build_lift(prv); } bool can_nxt(int x, int y) { // slow int c = x; int need = y < x ? y + n : y; c = search_lift(up_nxt, c, [&](int v){ return v != -1 && v <= need; }); /* while (nxt[c] != -1 && nxt[c] <= need) { assert(b[nxt[c]] > b[c]); c = nxt[c]; } */ assert(b[need] == a[y]); return need - c < k && b[need] >= b[c]; } bool can_prv(int x, int y) { // slow int c = x > y ? x : x + n; int need = y; c = search_lift(up_prv, c, [&](int v){ return v != -1 && v >= need; }); /* while (prv[c] != -1 && prv[c] >= need) { assert(b[prv[c]] > b[c]); c = prv[c]; } */ assert(b[need] == a[y]); return c - need < k && b[need] >= b[c]; } bool can_reach(int x, int y) { return can_nxt(x, y) || can_prv(x, y); } int compare_plants(int x, int y) { if (can_reach(x, y)) { // y is greater than x assert(a[y] > a[x]); return -1; } if (can_reach(y, x)) { // x is greater than y assert(a[x] > a[y]); return 1; } return 0; }

Compilation message (stderr)

plants.cpp:98:61: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   98 | int search_lift(const vector<vector<int>>& up, int c, const auto& f) {
      |                                                             ^~~~
plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:220:14: warning: unused variable 'rep' [-Wunused-variable]
  220 |     for (int rep : {0, 1}) for (int x : a) b.push_back(x);
      |              ^~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...