Submission #1042697

#TimeUsernameProblemLanguageResultExecution timeMemory
1042697Soumya1Comparing Plants (IOI20_plants)C++17
100 / 100
653 ms110928 KiB
#include "plants.h" #include <bits/stdc++.h> using namespace std; void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif const int mxN = 2e5 + 5; int t[4 * mxN], lazy[4 * mxN]; void apply(int x, int v) { t[x] += v; lazy[x] += v; } void push(int x, int lx, int rx) { if (lx == rx) return; apply(x << 1, lazy[x]); apply(x << 1 | 1, lazy[x]); lazy[x] = 0; } void update(int x, int lx, int rx, int l, int r, int v) { if (lazy[x]) push(x, lx, rx); if (l > rx || lx > r) return; if (l <= lx && r >= rx) { apply(x, v); return; } int mx = (lx + rx) >> 1; update(x << 1, lx, mx, l, r, v); update(x << 1 | 1, mx + 1, rx, l, r, v); t[x] = min(t[x << 1], t[x << 1 | 1]); } int query(int x, int lx, int rx, int l, int r) { if (lazy[x]) push(x, lx, rx); if (l > rx || lx > r || t[x]) return -1; if (lx == rx) return lx; int mx = (lx + rx) >> 1; int y = query(x << 1, lx, mx, l, r); if (y != -1) return y; return query(x << 1 | 1, mx + 1, rx, l, r); } int min_query(int x, int lx, int rx, int l, int r) { if (lazy[x]) push(x, lx, rx); if (l > rx || lx > r) return 1e9; if (l <= lx && r >= rx) return t[x]; int mx = (lx + rx) >> 1; return min(min_query(x << 1, lx, mx, l, r), min_query(x << 1 | 1, mx + 1, rx, l, r)); } int val[mxN], pos[mxN], cur; int n, k; void dfs(int u) { vector<pair<int, int>> ranges; if (u >= k + 1) ranges = {{u - k, u - 1}}; else if (u == 1) ranges = {{n - k + 1, n}}; else ranges = {{max(1, u - k), u - 1}, {n - k + u, n}}; for (auto [l, r] : ranges) { int idx = query(1, 1, n, l, r); while (idx != -1) { dfs(idx); idx = query(1, 1, n, l, r); } } for (auto [l, r] : ranges) { update(1, 1, n, l, r, -1); } pos[cur] = u; val[u] = cur--; update(1, 1, n, u, u, 1e9); } const int lg = 20; int L[mxN][lg], R[mxN][lg]; long long sl[mxN][lg], sr[mxN][lg]; void init(int K, vector<int> r) { k = K - 1, n = r.size(); cur = n; for (int i = 0; i < n; i++) { update(1, 1, n, i + 1, i + 1, r[i]); } while (true) { int i = query(1, 1, n, 1, n); if (i != -1) dfs(i); else break; } for (int i = 1; i <= n; i++) { update(1, 1, n, i, i, -min_query(1, 1, n, i, i) + 1e9); } val[0] = n + 1, pos[n + 1] = 0; for (int i = n; i >= 1; i--) { int p = pos[i]; int mn = min(min_query(1, 1, n, max(p - k, 1), p - 1), min_query(1, 1, n, min(n - k + p, n + 1), n)); L[pos[i]][0] = (mn == 1e9 ? 0 : pos[mn]); mn = min(min_query(1, 1, n, p + 1, min(n, p + k)), min_query(1, 1, n, 1, p + k - n)); R[pos[i]][0] = (mn == 1e9 ? 0 : pos[mn]); update(1, 1, n, pos[i], pos[i], -1e9 + i); sl[pos[i]][0] = (pos[i] - L[pos[i]][0] + n) % n; sr[pos[i]][0] = (R[pos[i]][0] - pos[i] + n) % n; } val[n + 1] = n + 1; R[n + 1][0] = n + 1; for (int i = 1; i <= n; i++) { if (!R[i][0]) R[i][0] = i; if (!L[i][0]) L[i][0] = i; } for (int j = 1; j < lg; j++) { for (int i = 0; i <= n + 1; i++) { L[i][j] = L[L[i][j - 1]][j - 1]; R[i][j] = R[R[i][j - 1]][j - 1]; sl[i][j] = sl[i][j - 1] + sl[L[i][j - 1]][j - 1]; sr[i][j] = sr[i][j - 1] + sr[R[i][j - 1]][j - 1]; } } } bool Lpath(int x, int y) { int tot = (x - y + n) % n; for (int j = lg - 1; j >= 0; j--) { if (sl[x][j] <= tot) { tot -= sl[x][j]; x = L[x][j]; } } return (val[x] <= val[y] && (x - y + n) % n <= k); } bool Rpath(int x, int y) { int tot = (y - x + n) % n; for (int j = lg - 1; j >= 0; j--) { if (sr[x][j] <= tot) { tot -= sr[x][j]; x = R[x][j]; } } return (val[x] <= val[y] && (y - x + n) % n <= k); } int compare_plants(int x, int y) { x++, y++; if (Lpath(x, y) || Rpath(x, y)) return -1; if (Lpath(y, x) || Rpath(y, x)) return 1; 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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...