#include "plants.h"
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define rank fuck
using namespace std;
using lint = long long;
using llf = long double;
using pi = pair<lint, lint>;
const int MAXN = 200005;
const int MAXT = 2100005;
int n, rank[MAXN];
lint DL[18][MAXN], DR[18][MAXN];
int L[18][MAXN], R[18][MAXN];
vector<int> lev[MAXN];
struct node{
int minv;
int lidx, ridx;
node(){
minv = 1e9;
}
node(int x, int v){
lidx = ridx = x;
minv = v;
}
node operator+(const node &n)const{
node x = *this;
node y = n;
if(x.minv < y.minv) return x;
if(x.minv > y.minv) return y;
x.lidx = min(x.lidx, y.lidx);
x.ridx = max(x.ridx, y.ridx);
return x;
}
};
struct seg{
node tree[MAXT];
int lim;
void init(int n){
for(lim = 1; lim <= 3 * n; lim <<= 1);
for(int i=0; i<3*n; i++) tree[i + lim] = node(i-n, 1e9);
for(int i=lim-1; i; i--) tree[i] = tree[2*i] + tree[2*i+1];
}
void upd(int x, int v){
node val(x, v);
x += n + lim;
tree[x] = val;
while(x > 1){
x >>= 1;
tree[x] = tree[2*x] + tree[2*x+1];
}
}
node query(int s, int e){
s += lim + n;
e += lim + n;
node ret;
while(s < e){
if(s%2 == 1) ret = ret + tree[s++];
if(e%2 == 0) ret = ret + tree[e--];
s >>= 1;
e >>= 1;
}
if(s == e) ret = ret + tree[s];
return ret;
}
}seg;
struct sex{
int tree[530005], lazy[530005];
void init(int s, int e, vector<int> &v, int p){
if(s == e){
tree[p] = v[s];
return;
}
int m = (s+e)/2;
init(s, m, v, 2*p);
init(m+1, e, v, 2*p+1);
tree[p] = min(tree[2*p], tree[2*p+1]);
}
void lazydown(int p){
for(int i=2*p; i<2*p+2; i++){
tree[i] += lazy[p];
lazy[i] += lazy[p];
}
lazy[p] = 0;
}
void add(int s, int e, int ps, int pe, int p, int v){
if(e < ps || pe < s) return;
if(s <= ps && pe <= e){
tree[p] += v;
lazy[p] += v;
return;
}
int pm = (ps+pe)/2;
lazydown(p);
add(s, e, ps, pm, 2*p, v);
add(s, e, pm+1, pe, 2*p+1, v);
tree[p] = min(tree[2*p], tree[2*p+1]);
}
int query(int s, int e, int ps, int pe, int p){
if(e < ps || pe < s) return 1e9;
if(s <= ps && pe <= e) return tree[p];
int pm = (ps+pe)/2;
lazydown(p);
return min(query(s, e, ps, pm, 2*p), query(s, e, pm+1, pe, 2*p+1));
}
int find_first(int s, int e, int p){
if(tree[1] > 0) return 1e9;
if(s == e) return s;
lazydown(p);
int m = (s+e)/2;
if(!tree[2*p]) return find_first(s, m, 2*p);
return find_first(m+1, e, 2*p+1);
}
}sex;
void init(int k, std::vector<int> r) {
n = sz(r);
int layer = 0;
set<int> unused;
for(int i=0; i<n; i++) unused.insert(i);
sex.init(0, n-1, r, 1);
auto QUERY = [&](int st, int ed){
int ret = 1e9;
if(st < n) ret = min(ret, sex.query(st, min(ed, n - 1), 0, n - 1, 1));
if(ed >= n) ret = min(ret, sex.query(max(st - n, 0), ed - n, 0, n - 1, 1));
return ret;
};
auto cond = [&](int j){
if(QUERY(j, j)) return false;
return QUERY(j + n - (k - 1), j + n - 1) > 0;
};
auto ADD = [&](int st, int ed, int x){
if(st < n) sex.add(st, min(ed, n - 1), 0, n - 1, 1, x);
if(ed >= n) sex.add(max(st - n, 0), ed - n, 0, n - 1, 1, x);
};
auto FIND_FIRST = [&](int s, int k){
ADD(0, s - 1, 1);
int Q = sex.find_first(0, n - 1, 1);
ADD(0, s - 1, -1);
int R = sex.find_first(0, n - 1, 1);
if(Q <= s + k - 1) return Q;
if(R < n && (R - s + n) % n < k) return R;
return -1;
};
vector<int> cur;
for(int i=0; i<n; i++){
if(cond(i)) cur.push_back(i);
}
while(sz(unused)){
vector<int> nxt;
layer++;
for(auto &j : cur){
if(unused.find(j) == unused.end()) continue;
unused.erase(j);
rank[j] = layer;
ADD(j, j, k-1);
ADD(j-k+1+n, j-1+n, -1);
int p1 = (j - k + 1 + n) % n;
int p2 = (j + 1) % n;
int pos1 = FIND_FIRST(p1, k - 1);
int pos2 = FIND_FIRST(p2, k - 1);
if(~pos1 && cond(pos1)) nxt.push_back(pos1);
if(~pos2 && cond(pos2)) nxt.push_back(pos2);
}
cur = nxt;
}
for(int i=0; i<n; i++){
lev[rank[i]].push_back(i);
}
seg.init(n);
for(int i=layer; i; i--){
for(auto &j : lev[i]){
L[0][j] = j;
auto qr = seg.query(j-k+1, j-1);
if(qr.minv > 1e8) continue;
if(qr.lidx < j) L[0][j] = (qr.lidx + n) % n;
DL[0][j] = (j - L[0][j] + n) % n;
}
for(auto &j : lev[i]){
R[0][j] = j;
auto qr = seg.query(j+1, j+k-1);
if(qr.minv > 1e8) continue;
if(qr.ridx > j) R[0][j] = (qr.ridx + n) % n;
DR[0][j] = (R[0][j] - j + n) % n;
}
for(auto &j : lev[i]){
seg.upd(j - n, i);
seg.upd(j , i);
seg.upd(j + n, i);
}
}
for(int i=1; i<18; i++){
for(int j=0; j<n; j++){
L[i][j] = L[i-1][L[i-1][j]];
R[i][j] = R[i-1][R[i-1][j]];
DL[i][j] = DL[i-1][j] + DL[i-1][L[i-1][j]];
DR[i][j] = DR[i-1][j] + DR[i-1][R[i-1][j]];
}
}
}
int compare_plants(int x, int y) {
if(rank[x] == rank[y]) return 0;
if(rank[x] > rank[y]) return -compare_plants(y, x);
lint st = x, ed = x;
{
lint dist = 0;
int p = x;
for(int i=17; i>=0; i--){
if(rank[L[i][p]] <= rank[y]){
dist += DL[i][p];
p = L[i][p];
}
}
st = x - dist;
}
{
lint dist = 0;
int p = x;
for(int i=17; i>=0; i--){
if(rank[R[i][p]] <= rank[y]){
dist += DR[i][p];
p = R[i][p];
}
}
ed = x + dist;
}
lint Q = ed - ed % n + y;
if(Q > ed) Q -= n;
return Q >= st;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
30080 KB |
Output is correct |
2 |
Correct |
18 ms |
30080 KB |
Output is correct |
3 |
Correct |
18 ms |
30080 KB |
Output is correct |
4 |
Correct |
18 ms |
30208 KB |
Output is correct |
5 |
Correct |
18 ms |
30080 KB |
Output is correct |
6 |
Correct |
103 ms |
33012 KB |
Output is correct |
7 |
Correct |
348 ms |
43128 KB |
Output is correct |
8 |
Correct |
1625 ms |
132780 KB |
Output is correct |
9 |
Correct |
1727 ms |
132472 KB |
Output is correct |
10 |
Correct |
1720 ms |
131516 KB |
Output is correct |
11 |
Correct |
1626 ms |
130948 KB |
Output is correct |
12 |
Correct |
1608 ms |
131064 KB |
Output is correct |
13 |
Correct |
1663 ms |
131064 KB |
Output is correct |
14 |
Correct |
1582 ms |
131064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
30208 KB |
Output is correct |
2 |
Correct |
18 ms |
30080 KB |
Output is correct |
3 |
Correct |
18 ms |
30080 KB |
Output is correct |
4 |
Correct |
17 ms |
30080 KB |
Output is correct |
5 |
Correct |
18 ms |
30208 KB |
Output is correct |
6 |
Correct |
26 ms |
30720 KB |
Output is correct |
7 |
Correct |
200 ms |
35576 KB |
Output is correct |
8 |
Correct |
20 ms |
30208 KB |
Output is correct |
9 |
Correct |
24 ms |
30720 KB |
Output is correct |
10 |
Correct |
204 ms |
35576 KB |
Output is correct |
11 |
Correct |
173 ms |
35448 KB |
Output is correct |
12 |
Correct |
179 ms |
35704 KB |
Output is correct |
13 |
Correct |
223 ms |
35704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
30208 KB |
Output is correct |
2 |
Correct |
18 ms |
30080 KB |
Output is correct |
3 |
Correct |
18 ms |
30080 KB |
Output is correct |
4 |
Correct |
17 ms |
30080 KB |
Output is correct |
5 |
Correct |
18 ms |
30208 KB |
Output is correct |
6 |
Correct |
26 ms |
30720 KB |
Output is correct |
7 |
Correct |
200 ms |
35576 KB |
Output is correct |
8 |
Correct |
20 ms |
30208 KB |
Output is correct |
9 |
Correct |
24 ms |
30720 KB |
Output is correct |
10 |
Correct |
204 ms |
35576 KB |
Output is correct |
11 |
Correct |
173 ms |
35448 KB |
Output is correct |
12 |
Correct |
179 ms |
35704 KB |
Output is correct |
13 |
Correct |
223 ms |
35704 KB |
Output is correct |
14 |
Correct |
435 ms |
43212 KB |
Output is correct |
15 |
Correct |
2637 ms |
132520 KB |
Output is correct |
16 |
Correct |
411 ms |
43128 KB |
Output is correct |
17 |
Correct |
2569 ms |
132480 KB |
Output is correct |
18 |
Correct |
1784 ms |
131704 KB |
Output is correct |
19 |
Correct |
1645 ms |
131796 KB |
Output is correct |
20 |
Correct |
2644 ms |
132464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
30208 KB |
Output is correct |
2 |
Correct |
18 ms |
30200 KB |
Output is correct |
3 |
Correct |
169 ms |
34032 KB |
Output is correct |
4 |
Correct |
1499 ms |
132472 KB |
Output is correct |
5 |
Correct |
1648 ms |
132472 KB |
Output is correct |
6 |
Correct |
2155 ms |
132556 KB |
Output is correct |
7 |
Correct |
2434 ms |
132508 KB |
Output is correct |
8 |
Correct |
2613 ms |
132464 KB |
Output is correct |
9 |
Correct |
1785 ms |
132496 KB |
Output is correct |
10 |
Correct |
1639 ms |
132476 KB |
Output is correct |
11 |
Correct |
1696 ms |
130880 KB |
Output is correct |
12 |
Correct |
1579 ms |
130936 KB |
Output is correct |
13 |
Correct |
1769 ms |
131588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
30208 KB |
Output is correct |
2 |
Correct |
18 ms |
30080 KB |
Output is correct |
3 |
Correct |
17 ms |
30208 KB |
Output is correct |
4 |
Correct |
18 ms |
30080 KB |
Output is correct |
5 |
Correct |
18 ms |
30208 KB |
Output is correct |
6 |
Correct |
20 ms |
30208 KB |
Output is correct |
7 |
Correct |
39 ms |
30840 KB |
Output is correct |
8 |
Correct |
41 ms |
30968 KB |
Output is correct |
9 |
Correct |
40 ms |
30972 KB |
Output is correct |
10 |
Correct |
41 ms |
30968 KB |
Output is correct |
11 |
Correct |
43 ms |
30968 KB |
Output is correct |
12 |
Correct |
42 ms |
30968 KB |
Output is correct |
13 |
Correct |
42 ms |
30976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
30080 KB |
Output is correct |
2 |
Correct |
18 ms |
30080 KB |
Output is correct |
3 |
Correct |
18 ms |
30080 KB |
Output is correct |
4 |
Correct |
17 ms |
30080 KB |
Output is correct |
5 |
Correct |
23 ms |
30592 KB |
Output is correct |
6 |
Correct |
1733 ms |
132472 KB |
Output is correct |
7 |
Correct |
1941 ms |
132600 KB |
Output is correct |
8 |
Correct |
2001 ms |
132472 KB |
Output is correct |
9 |
Correct |
2320 ms |
132636 KB |
Output is correct |
10 |
Correct |
1277 ms |
132564 KB |
Output is correct |
11 |
Correct |
1678 ms |
132432 KB |
Output is correct |
12 |
Correct |
1383 ms |
132420 KB |
Output is correct |
13 |
Correct |
1555 ms |
132472 KB |
Output is correct |
14 |
Correct |
1948 ms |
132472 KB |
Output is correct |
15 |
Correct |
2257 ms |
132428 KB |
Output is correct |
16 |
Correct |
1382 ms |
132472 KB |
Output is correct |
17 |
Correct |
1396 ms |
132524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
30080 KB |
Output is correct |
2 |
Correct |
18 ms |
30080 KB |
Output is correct |
3 |
Correct |
18 ms |
30080 KB |
Output is correct |
4 |
Correct |
18 ms |
30208 KB |
Output is correct |
5 |
Correct |
18 ms |
30080 KB |
Output is correct |
6 |
Correct |
103 ms |
33012 KB |
Output is correct |
7 |
Correct |
348 ms |
43128 KB |
Output is correct |
8 |
Correct |
1625 ms |
132780 KB |
Output is correct |
9 |
Correct |
1727 ms |
132472 KB |
Output is correct |
10 |
Correct |
1720 ms |
131516 KB |
Output is correct |
11 |
Correct |
1626 ms |
130948 KB |
Output is correct |
12 |
Correct |
1608 ms |
131064 KB |
Output is correct |
13 |
Correct |
1663 ms |
131064 KB |
Output is correct |
14 |
Correct |
1582 ms |
131064 KB |
Output is correct |
15 |
Correct |
18 ms |
30208 KB |
Output is correct |
16 |
Correct |
18 ms |
30080 KB |
Output is correct |
17 |
Correct |
18 ms |
30080 KB |
Output is correct |
18 |
Correct |
17 ms |
30080 KB |
Output is correct |
19 |
Correct |
18 ms |
30208 KB |
Output is correct |
20 |
Correct |
26 ms |
30720 KB |
Output is correct |
21 |
Correct |
200 ms |
35576 KB |
Output is correct |
22 |
Correct |
20 ms |
30208 KB |
Output is correct |
23 |
Correct |
24 ms |
30720 KB |
Output is correct |
24 |
Correct |
204 ms |
35576 KB |
Output is correct |
25 |
Correct |
173 ms |
35448 KB |
Output is correct |
26 |
Correct |
179 ms |
35704 KB |
Output is correct |
27 |
Correct |
223 ms |
35704 KB |
Output is correct |
28 |
Correct |
435 ms |
43212 KB |
Output is correct |
29 |
Correct |
2637 ms |
132520 KB |
Output is correct |
30 |
Correct |
411 ms |
43128 KB |
Output is correct |
31 |
Correct |
2569 ms |
132480 KB |
Output is correct |
32 |
Correct |
1784 ms |
131704 KB |
Output is correct |
33 |
Correct |
1645 ms |
131796 KB |
Output is correct |
34 |
Correct |
2644 ms |
132464 KB |
Output is correct |
35 |
Correct |
17 ms |
30208 KB |
Output is correct |
36 |
Correct |
18 ms |
30200 KB |
Output is correct |
37 |
Correct |
169 ms |
34032 KB |
Output is correct |
38 |
Correct |
1499 ms |
132472 KB |
Output is correct |
39 |
Correct |
1648 ms |
132472 KB |
Output is correct |
40 |
Correct |
2155 ms |
132556 KB |
Output is correct |
41 |
Correct |
2434 ms |
132508 KB |
Output is correct |
42 |
Correct |
2613 ms |
132464 KB |
Output is correct |
43 |
Correct |
1785 ms |
132496 KB |
Output is correct |
44 |
Correct |
1639 ms |
132476 KB |
Output is correct |
45 |
Correct |
1696 ms |
130880 KB |
Output is correct |
46 |
Correct |
1579 ms |
130936 KB |
Output is correct |
47 |
Correct |
1769 ms |
131588 KB |
Output is correct |
48 |
Correct |
18 ms |
30208 KB |
Output is correct |
49 |
Correct |
18 ms |
30080 KB |
Output is correct |
50 |
Correct |
17 ms |
30208 KB |
Output is correct |
51 |
Correct |
18 ms |
30080 KB |
Output is correct |
52 |
Correct |
18 ms |
30208 KB |
Output is correct |
53 |
Correct |
20 ms |
30208 KB |
Output is correct |
54 |
Correct |
39 ms |
30840 KB |
Output is correct |
55 |
Correct |
41 ms |
30968 KB |
Output is correct |
56 |
Correct |
40 ms |
30972 KB |
Output is correct |
57 |
Correct |
41 ms |
30968 KB |
Output is correct |
58 |
Correct |
43 ms |
30968 KB |
Output is correct |
59 |
Correct |
42 ms |
30968 KB |
Output is correct |
60 |
Correct |
42 ms |
30976 KB |
Output is correct |
61 |
Correct |
130 ms |
34040 KB |
Output is correct |
62 |
Correct |
286 ms |
43028 KB |
Output is correct |
63 |
Correct |
1483 ms |
132600 KB |
Output is correct |
64 |
Correct |
1954 ms |
132544 KB |
Output is correct |
65 |
Correct |
2238 ms |
132464 KB |
Output is correct |
66 |
Correct |
2509 ms |
132556 KB |
Output is correct |
67 |
Correct |
2767 ms |
132432 KB |
Output is correct |
68 |
Correct |
1725 ms |
132432 KB |
Output is correct |
69 |
Correct |
1994 ms |
132472 KB |
Output is correct |
70 |
Correct |
1579 ms |
132432 KB |
Output is correct |
71 |
Correct |
1658 ms |
132448 KB |
Output is correct |
72 |
Correct |
2075 ms |
132568 KB |
Output is correct |
73 |
Correct |
2415 ms |
132448 KB |
Output is correct |
74 |
Correct |
1672 ms |
132508 KB |
Output is correct |
75 |
Correct |
1536 ms |
132444 KB |
Output is correct |