#include "plants.h"
#include "bits/stdc++.h"
using namespace std;
#define ar array
typedef long long ll;
//~ #define int ll
const int inf = 1e9;
const int N = 2e5 + 5;
struct ST{
ar<ll, 2> tree[N << 2];
ll f[N << 2];
void build(int lx, int rx, int x){
if(lx == rx) { tree[x][1] = lx; return; };
int m = (lx + rx) >> 1;
build(lx, m, x << 1), build(m + 1, rx, x << 1 | 1);
tree[x][1] = lx;
}
ST(){
build(0, N, 1);
}
void push(int x, int lx, int rx){
if(lx == rx) return;
tree[x << 1][0] += f[x];
tree[x << 1 | 1][0] += f[x];
f[x << 1] += f[x], f[x << 1 | 1] += f[x];
f[x] = 0;
}
void add(int l, int r, int v, int lx, int rx, int x){
if(lx > r || rx < l) return;
if(lx >= l && rx <= r){
tree[x][0] += v;
f[x] += v;
return;
}
push(x, lx, rx);
int m = (lx + rx) >> 1;
add(l, r, v, lx, m, x << 1), add(l, r, v, m + 1, rx, x << 1 | 1);
tree[x] = min(tree[x << 1], tree[x << 1 | 1]);
}
void add(int l, int r, int v) { return add(l, r, v, 0, N, 1); }
ar<ll, 2> get(int l, int r, int lx, int rx, int x){
if(lx > r || rx < l) return {inf, inf};
if(lx >= l && rx <= r) return tree[x];
push(x, lx, rx);
int m = (lx + rx) >> 1;
return min(get(l, r, lx, m, x << 1), get(l, r, m + 1, rx, x << 1 | 1));
}
ar<ll, 2> get(int l, int r){
return get(l, r, 0, N, 1);
}
}tree, R;
vector<int> a;
vector<vector<int>> is;
int n;
vector<int> pref;
void init(int k, vector<int> r) {
n = r.size();
a.resize(n);
if(k == 2){
pref.resize(n);
for(int i=0;i<n;i++){
if(i) pref[i] = pref[i - 1];
pref[i] += r[i];
}
return;
}
if(n <= 300){
is.resize(n, vector<int>(n, 0));
for(int j=n;j>0;j--){
int cnt = 0;
for(int i=0;i<k - 1;i++) cnt += (r[i] == 0);
int p = -1;
for(int i=k - 1;;){
if(!cnt && r[i] == 0){
p = i;
break;
}
cnt -= (r[(i + n - k + 1) % n] == 0);
cnt += (r[i] == 0);
i = (i + 1) % n;
if(i == k - 1) break;
}
assert(a[p] == 0);
r[p] = inf;
a[p] = 1;
for(int i=0;i<n;i++){
if((i <= p && p < i + k) || (i <= p + n && p + n < i + k)){
if(!a[i]) is[p][i] = 1;
r[i]--;
}
if((p <= i && i < p + k) || (p <= i + n && i + n < p + k)){
if(!a[i]) is[p][i] = 1;
}
}
}
//~ for(int i=0;i<n;i++){
//~ for(int j=0;j<n;j++){
//~ cout<<is[i][j]<<" ";
//~ } cout<<"\n";
//~ } cout<<"\n";
for(int k=0;k<n;k++){
for(int j=0;j<n;j++){
for(int i=0;i<n;i++){
is[i][j] |= (is[i][k] & is[k][j]);
}
}
}
return;
}
{
int cnt = 0;
for(int i=0;i<k - 1;i++) cnt += (r[i] == 0);
for(int i=k - 1;;){
tree.add(i, i, r[i] + cnt);
if(r[i]){
R.add(i, i, r[i]);
} else {
R.add(i, i, inf);
}
cnt -= (r[(i + n - k + 1) % n] == 0);
cnt += (r[i] == 0);
i = (i + 1) % n;
if(i == k - 1) break;
}
}
for(int j=n;j>0;j--){
//~ for(int i=0;i<n;i++) cout<<tree.get(i, i)[0]<<" ";
//~ cout<<"\n";
auto [v, p] = tree.get(0, n - 1);
assert(v == 0);
a[p] = j;
R.add(p, p, inf);
tree.add(p, p, inf);
{
int l = p - k + 1, r = p;
tree.add(max(l, 0), r, -1);
R.add(max(l, 0), r, -1);
if(l < 0) tree.add(n + l, n - 1, -1);
if(l < 0) R.add(n + l, n - 1, -1);
ar<ll, 2> pp = R.get(0, n - 1);
while(pp[0] == 0){
R.add(pp[1], pp[1], inf);
{
int l = pp[1] + 1, r = pp[1] + k - 1;
tree.add(l, min(r, n - 1), 1);
if(n <= r) tree.add(0, r - n, 1);
}
pp = R.get(0, n - 1);
}
}
{
int l = p, r = p + k - 1;
tree.add(l, min(r, n - 1), -1);
if(n <= r) tree.add(0, r - n, -1);
}
}
return;
}
int get(int x, int y){
return pref[y] - (x ? pref[x - 1] : 0);
}
int compare_plants(int x, int y) {
if(!pref.empty()){
if(get(x, y - 1) == 0){
return 1;
}
if(get(x, y - 1) == y - x){
return -1;
}
int cnt = get(y, n - 1);
cnt += (x ? get(0, x - 1) : 0);
if(cnt == n - y + x){
return 1;
} if(cnt == 0) {
return -1;
}
return 0;
}
if(n <= 300){
if(is[x][y]) return 1;
if(is[y][x]) return -1;
return 0;
}
if(a[x] > a[y]) return 1;
if(a[x] < a[y]) return -1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
16724 KB |
Output is correct |
2 |
Correct |
7 ms |
16724 KB |
Output is correct |
3 |
Correct |
7 ms |
16724 KB |
Output is correct |
4 |
Correct |
7 ms |
16724 KB |
Output is correct |
5 |
Correct |
7 ms |
16724 KB |
Output is correct |
6 |
Correct |
45 ms |
19460 KB |
Output is correct |
7 |
Correct |
48 ms |
19788 KB |
Output is correct |
8 |
Correct |
62 ms |
25008 KB |
Output is correct |
9 |
Correct |
63 ms |
25036 KB |
Output is correct |
10 |
Correct |
79 ms |
25052 KB |
Output is correct |
11 |
Correct |
62 ms |
25040 KB |
Output is correct |
12 |
Correct |
62 ms |
25092 KB |
Output is correct |
13 |
Correct |
59 ms |
25072 KB |
Output is correct |
14 |
Correct |
60 ms |
25052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
16656 KB |
Output is correct |
2 |
Correct |
7 ms |
16724 KB |
Output is correct |
3 |
Correct |
8 ms |
16724 KB |
Output is correct |
4 |
Correct |
7 ms |
16724 KB |
Output is correct |
5 |
Correct |
8 ms |
16724 KB |
Output is correct |
6 |
Correct |
12 ms |
16936 KB |
Output is correct |
7 |
Correct |
71 ms |
19868 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
15 ms |
16852 KB |
Output is correct |
10 |
Correct |
65 ms |
19808 KB |
Output is correct |
11 |
Correct |
58 ms |
19772 KB |
Output is correct |
12 |
Correct |
60 ms |
19932 KB |
Output is correct |
13 |
Correct |
83 ms |
19964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
16656 KB |
Output is correct |
2 |
Correct |
7 ms |
16724 KB |
Output is correct |
3 |
Correct |
8 ms |
16724 KB |
Output is correct |
4 |
Correct |
7 ms |
16724 KB |
Output is correct |
5 |
Correct |
8 ms |
16724 KB |
Output is correct |
6 |
Correct |
12 ms |
16936 KB |
Output is correct |
7 |
Correct |
71 ms |
19868 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
15 ms |
16852 KB |
Output is correct |
10 |
Correct |
65 ms |
19808 KB |
Output is correct |
11 |
Correct |
58 ms |
19772 KB |
Output is correct |
12 |
Correct |
60 ms |
19932 KB |
Output is correct |
13 |
Correct |
83 ms |
19964 KB |
Output is correct |
14 |
Correct |
127 ms |
20624 KB |
Output is correct |
15 |
Correct |
1099 ms |
29648 KB |
Output is correct |
16 |
Correct |
133 ms |
20596 KB |
Output is correct |
17 |
Correct |
1086 ms |
29652 KB |
Output is correct |
18 |
Correct |
681 ms |
29656 KB |
Output is correct |
19 |
Correct |
748 ms |
29780 KB |
Output is correct |
20 |
Correct |
1010 ms |
29568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
16724 KB |
Output is correct |
2 |
Correct |
9 ms |
16616 KB |
Output is correct |
3 |
Correct |
50 ms |
19680 KB |
Output is correct |
4 |
Correct |
539 ms |
29656 KB |
Output is correct |
5 |
Correct |
686 ms |
29680 KB |
Output is correct |
6 |
Correct |
826 ms |
29648 KB |
Output is correct |
7 |
Correct |
946 ms |
29720 KB |
Output is correct |
8 |
Correct |
1096 ms |
29652 KB |
Output is correct |
9 |
Correct |
590 ms |
29732 KB |
Output is correct |
10 |
Correct |
621 ms |
29656 KB |
Output is correct |
11 |
Correct |
69 ms |
22088 KB |
Output is correct |
12 |
Correct |
578 ms |
29600 KB |
Output is correct |
13 |
Correct |
618 ms |
29656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
16596 KB |
Output is correct |
2 |
Correct |
7 ms |
16724 KB |
Output is correct |
3 |
Correct |
7 ms |
16724 KB |
Output is correct |
4 |
Correct |
7 ms |
16724 KB |
Output is correct |
5 |
Correct |
8 ms |
16724 KB |
Output is correct |
6 |
Correct |
9 ms |
16724 KB |
Output is correct |
7 |
Correct |
57 ms |
17640 KB |
Output is correct |
8 |
Correct |
58 ms |
17620 KB |
Output is correct |
9 |
Correct |
63 ms |
17620 KB |
Output is correct |
10 |
Correct |
58 ms |
17620 KB |
Output is correct |
11 |
Correct |
57 ms |
17688 KB |
Output is correct |
12 |
Correct |
59 ms |
17688 KB |
Output is correct |
13 |
Correct |
59 ms |
17684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
16724 KB |
Output is correct |
2 |
Correct |
8 ms |
16724 KB |
Output is correct |
3 |
Correct |
7 ms |
16724 KB |
Output is correct |
4 |
Correct |
8 ms |
16724 KB |
Output is correct |
5 |
Incorrect |
10 ms |
16872 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
16724 KB |
Output is correct |
2 |
Correct |
7 ms |
16724 KB |
Output is correct |
3 |
Correct |
7 ms |
16724 KB |
Output is correct |
4 |
Correct |
7 ms |
16724 KB |
Output is correct |
5 |
Correct |
7 ms |
16724 KB |
Output is correct |
6 |
Correct |
45 ms |
19460 KB |
Output is correct |
7 |
Correct |
48 ms |
19788 KB |
Output is correct |
8 |
Correct |
62 ms |
25008 KB |
Output is correct |
9 |
Correct |
63 ms |
25036 KB |
Output is correct |
10 |
Correct |
79 ms |
25052 KB |
Output is correct |
11 |
Correct |
62 ms |
25040 KB |
Output is correct |
12 |
Correct |
62 ms |
25092 KB |
Output is correct |
13 |
Correct |
59 ms |
25072 KB |
Output is correct |
14 |
Correct |
60 ms |
25052 KB |
Output is correct |
15 |
Correct |
7 ms |
16656 KB |
Output is correct |
16 |
Correct |
7 ms |
16724 KB |
Output is correct |
17 |
Correct |
8 ms |
16724 KB |
Output is correct |
18 |
Correct |
7 ms |
16724 KB |
Output is correct |
19 |
Correct |
8 ms |
16724 KB |
Output is correct |
20 |
Correct |
12 ms |
16936 KB |
Output is correct |
21 |
Correct |
71 ms |
19868 KB |
Output is correct |
22 |
Correct |
9 ms |
16724 KB |
Output is correct |
23 |
Correct |
15 ms |
16852 KB |
Output is correct |
24 |
Correct |
65 ms |
19808 KB |
Output is correct |
25 |
Correct |
58 ms |
19772 KB |
Output is correct |
26 |
Correct |
60 ms |
19932 KB |
Output is correct |
27 |
Correct |
83 ms |
19964 KB |
Output is correct |
28 |
Correct |
127 ms |
20624 KB |
Output is correct |
29 |
Correct |
1099 ms |
29648 KB |
Output is correct |
30 |
Correct |
133 ms |
20596 KB |
Output is correct |
31 |
Correct |
1086 ms |
29652 KB |
Output is correct |
32 |
Correct |
681 ms |
29656 KB |
Output is correct |
33 |
Correct |
748 ms |
29780 KB |
Output is correct |
34 |
Correct |
1010 ms |
29568 KB |
Output is correct |
35 |
Correct |
7 ms |
16724 KB |
Output is correct |
36 |
Correct |
9 ms |
16616 KB |
Output is correct |
37 |
Correct |
50 ms |
19680 KB |
Output is correct |
38 |
Correct |
539 ms |
29656 KB |
Output is correct |
39 |
Correct |
686 ms |
29680 KB |
Output is correct |
40 |
Correct |
826 ms |
29648 KB |
Output is correct |
41 |
Correct |
946 ms |
29720 KB |
Output is correct |
42 |
Correct |
1096 ms |
29652 KB |
Output is correct |
43 |
Correct |
590 ms |
29732 KB |
Output is correct |
44 |
Correct |
621 ms |
29656 KB |
Output is correct |
45 |
Correct |
69 ms |
22088 KB |
Output is correct |
46 |
Correct |
578 ms |
29600 KB |
Output is correct |
47 |
Correct |
618 ms |
29656 KB |
Output is correct |
48 |
Correct |
7 ms |
16596 KB |
Output is correct |
49 |
Correct |
7 ms |
16724 KB |
Output is correct |
50 |
Correct |
7 ms |
16724 KB |
Output is correct |
51 |
Correct |
7 ms |
16724 KB |
Output is correct |
52 |
Correct |
8 ms |
16724 KB |
Output is correct |
53 |
Correct |
9 ms |
16724 KB |
Output is correct |
54 |
Correct |
57 ms |
17640 KB |
Output is correct |
55 |
Correct |
58 ms |
17620 KB |
Output is correct |
56 |
Correct |
63 ms |
17620 KB |
Output is correct |
57 |
Correct |
58 ms |
17620 KB |
Output is correct |
58 |
Correct |
57 ms |
17688 KB |
Output is correct |
59 |
Correct |
59 ms |
17688 KB |
Output is correct |
60 |
Correct |
59 ms |
17684 KB |
Output is correct |
61 |
Incorrect |
50 ms |
21380 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |