#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define pb push_back
#define eb emplace_back
#define f first
#define s second
const int maxn = 4e5 + 5;
const int INF = 1e9;
#include "plants.h"
int a[maxn], rk[maxn];
int n, k;
vector<int> r;
pll st[maxn * 4];
ll tag[maxn * 4];
void cast(int v, int val){
st[v].f += val, tag[v] += val;
}
void push(int v){
if(tag[v]){
cast(v * 2, tag[v]);
cast(v * 2 + 1, tag[v]);
tag[v] = 0;
}
}
void upd(int l, int r, int val, int v = 1, int L = 0, int R = 2 * n - 1){
if(l > R || r < L) return;
if(L == R) st[v].s = L;
if(l <= L && r >= R){
st[v].f += val;
tag[v] += val;
return;
}
push(v);
int m = (L + R) / 2;
upd(l, r, val, v * 2, L, m);
upd(l, r, val, v * 2 + 1, m + 1, R);
st[v] = min(st[v * 2], st[v * 2 + 1]);
}
pll qry(int l, int r, int v = 1, int L = 0, int R = 2 * n - 1){
if(l > R || r < L) return {INF, 0};
if(l <= L && r >= R) return st[v];
push(v);
int m = (L + R) / 2;
return min(qry(l, r, v * 2, L, m), qry(l, r, v * 2 + 1, m + 1, R));
}
void init(int _k, std::vector<int> _r) {
r = _r, k = _k;
n = r.size();
for(auto x : _r) r.pb(x);
{
for(int i = 0; i < 2 * n; i++) a[i] = 1 - r[i] * 2;
for(int i = 1; i < 2 * n; i++) a[i] += a[i - 1];
}
for(int i = 0; i < 2 * n; i++) upd(i, i, r[i]);
stack<int> stk;
vector<int> used(2 * n + 1);
for(int rd = 0; rd < n; rd++){
if(stk.empty()) stk.push(qry(0, 2 * n - 1).s);
while(true){
int pos = stk.top();
if(pos < n) pos += n;
pll tmp = qry(pos - k + 1, pos - 1);
if(!tmp.f){
assert(!used[tmp.s]);
pos = tmp.s, stk.push(pos);
used[tmp.s] = 1;
}
else break;
}
int pos = stk.top(); stk.pop();
pos %= n;
rk[pos] = rd;
upd(pos, pos, INF), upd(pos + n, pos + n, INF);
upd(max(0, pos - k + 1), pos - 1, -1);
pos += n;
upd(pos - k + 1, pos - 1, -1);
}
}
int compare_plants(int x, int y) {
if(k <= 2){
int m = a[y - 1] - (x ? a[x - 1] : 0);
if(m == y - x) return 1;
if(m == x - y) return -1;
x += n;
m = a[x - 1] - a[y - 1];
if(m == x - y) return -1;
if(m == y - x) return 1;
return 0;
}
return rk[x] < rk[y] ? 1 : -1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4544 KB |
Output is correct |
6 |
Correct |
42 ms |
7260 KB |
Output is correct |
7 |
Correct |
69 ms |
10512 KB |
Output is correct |
8 |
Correct |
328 ms |
35888 KB |
Output is correct |
9 |
Correct |
331 ms |
35908 KB |
Output is correct |
10 |
Correct |
318 ms |
35652 KB |
Output is correct |
11 |
Correct |
316 ms |
35652 KB |
Output is correct |
12 |
Correct |
315 ms |
35820 KB |
Output is correct |
13 |
Correct |
325 ms |
35840 KB |
Output is correct |
14 |
Correct |
309 ms |
35644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
3 ms |
4444 KB |
Output is correct |
7 |
Correct |
53 ms |
9740 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4444 KB |
Output is correct |
10 |
Correct |
52 ms |
9644 KB |
Output is correct |
11 |
Correct |
49 ms |
9552 KB |
Output is correct |
12 |
Correct |
49 ms |
9656 KB |
Output is correct |
13 |
Correct |
58 ms |
9552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
3 ms |
4444 KB |
Output is correct |
7 |
Correct |
53 ms |
9740 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4444 KB |
Output is correct |
10 |
Correct |
52 ms |
9644 KB |
Output is correct |
11 |
Correct |
49 ms |
9552 KB |
Output is correct |
12 |
Correct |
49 ms |
9656 KB |
Output is correct |
13 |
Correct |
58 ms |
9552 KB |
Output is correct |
14 |
Correct |
90 ms |
10904 KB |
Output is correct |
15 |
Correct |
661 ms |
37700 KB |
Output is correct |
16 |
Correct |
91 ms |
10836 KB |
Output is correct |
17 |
Correct |
682 ms |
38148 KB |
Output is correct |
18 |
Correct |
432 ms |
37784 KB |
Output is correct |
19 |
Correct |
414 ms |
37992 KB |
Output is correct |
20 |
Correct |
599 ms |
37696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
45 ms |
7428 KB |
Output is correct |
4 |
Correct |
380 ms |
37420 KB |
Output is correct |
5 |
Correct |
469 ms |
37700 KB |
Output is correct |
6 |
Incorrect |
642 ms |
37936 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4688 KB |
Output is correct |
5 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4852 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4544 KB |
Output is correct |
6 |
Correct |
42 ms |
7260 KB |
Output is correct |
7 |
Correct |
69 ms |
10512 KB |
Output is correct |
8 |
Correct |
328 ms |
35888 KB |
Output is correct |
9 |
Correct |
331 ms |
35908 KB |
Output is correct |
10 |
Correct |
318 ms |
35652 KB |
Output is correct |
11 |
Correct |
316 ms |
35652 KB |
Output is correct |
12 |
Correct |
315 ms |
35820 KB |
Output is correct |
13 |
Correct |
325 ms |
35840 KB |
Output is correct |
14 |
Correct |
309 ms |
35644 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
3 ms |
4444 KB |
Output is correct |
21 |
Correct |
53 ms |
9740 KB |
Output is correct |
22 |
Correct |
2 ms |
4444 KB |
Output is correct |
23 |
Correct |
3 ms |
4444 KB |
Output is correct |
24 |
Correct |
52 ms |
9644 KB |
Output is correct |
25 |
Correct |
49 ms |
9552 KB |
Output is correct |
26 |
Correct |
49 ms |
9656 KB |
Output is correct |
27 |
Correct |
58 ms |
9552 KB |
Output is correct |
28 |
Correct |
90 ms |
10904 KB |
Output is correct |
29 |
Correct |
661 ms |
37700 KB |
Output is correct |
30 |
Correct |
91 ms |
10836 KB |
Output is correct |
31 |
Correct |
682 ms |
38148 KB |
Output is correct |
32 |
Correct |
432 ms |
37784 KB |
Output is correct |
33 |
Correct |
414 ms |
37992 KB |
Output is correct |
34 |
Correct |
599 ms |
37696 KB |
Output is correct |
35 |
Correct |
1 ms |
4440 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
45 ms |
7428 KB |
Output is correct |
38 |
Correct |
380 ms |
37420 KB |
Output is correct |
39 |
Correct |
469 ms |
37700 KB |
Output is correct |
40 |
Incorrect |
642 ms |
37936 KB |
Output isn't correct |
41 |
Halted |
0 ms |
0 KB |
- |