#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];
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);
}
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] = n + 1;
}
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];
}
}
}
bool Lpath(int x, int y) {
if (x > y) {
for (int j = lg - 1; j >= 0; j--) {
if (L[x][j] >= y && R[x][j] <= x) x = L[x][j];
}
} else {
for (int j = lg - 1; j >= 0; j--) {
if (L[x][j] >= 1 && R[x][j] <= x) x = L[x][j];
}
x = L[x][0];
for (int j = lg - 1; j >= 0; j--) {
if (L[x][j] >= y && R[x][j] <= x) x = L[x][j];
}
}
return (val[x] <= val[y] && (x - y + n) % n <= k);
}
bool Rpath(int x, int y) {
if (x < y) {
for (int j = lg - 1; j >= 0; j--) {
if (R[x][j] <= y && R[x][j] >= x) x = R[x][j];
}
} else {
for (int j = lg - 1; j >= 0; j--) {
if (R[x][j] <= n && R[x][j] >= x) x = R[x][j];
}
x = R[x][0];
for (int j = lg - 1; j >= 0; j--) {
if (R[x][j] <= y && R[x][j] >= x) 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6584 KB |
Output is correct |
6 |
Incorrect |
43 ms |
10176 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6588 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6588 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Incorrect |
57 ms |
11220 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
0 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6540 KB |
Output is correct |
4 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6584 KB |
Output is correct |
6 |
Incorrect |
43 ms |
10176 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |