#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
#define X first
#define Y second
#define lc id << 1
#define rc lc | 1
const int MAXN = 2e5 + 10;
const int MOD = 1e9 + 7;
int n , k , A[MAXN] , val[MAXN] , lz[MAXN << 2];
pii seg[MAXN << 2];
void build(int id = 1 , int l = 0 , int r = n){
if(r - l == 1){
seg[id] = {A[l] , l};
return;
}
int mid = l + r >> 1;
build(lc , l , mid);
build(rc , mid , r);
seg[id] = min(seg[lc] , seg[rc]);
}
void shift(int id){
lz[lc] += lz[id]; seg[lc].X += lz[id];
lz[rc] += lz[id]; seg[rc].X += lz[id];
lz[id] = 0;
}
void add(int ql , int qr , int val , int id = 1 , int l = 0 , int r = n){
if(qr <= l || r <= ql) return;
if(ql <= l && r <= qr){
lz[id] += val;
seg[id].X += val;
return;
}
shift(id);
int mid = l + r >> 1;
add(ql , qr , val , lc , l , mid);
add(ql , qr , val , rc , mid , r);
seg[id] = min(seg[lc] , seg[rc]);
}
pii get(int ql , int qr , int id = 1 , int l = 0 , int r = n){
if(qr <= l || r <= ql) return {MOD , MOD};
if(ql <= l && r <= qr) return seg[id];
shift(id);
int mid = l + r >> 1;
return min(get(ql , qr , lc , l , mid) , get(ql , qr , rc , mid , r));
}
void Add(int l , int r , int val){
if(l < r){
add(l , r , val);
return;
}
add(0 , r , val); add(l , n , val);
return;
}
pii Get(int l , int r){
if(l < r) return get(l , r);
pii A = get(0 , r) , B = get(l , n);
if(A.X < B.X) return A;
return B;
}
void init(int K, vector<int> r) {
k = K; n = r.size();
for(int i = 0 ; i < n ; i++) A[i] = r[i];
build();
for(int i = 0 ; i < n ; i++){
int pos = Get(0 , n).Y;
while(1){
pii A = Get((pos + n - k + 1) % n , pos);
if(A.X == 0) pos = A.Y;
else break;
}
Add((pos + n - k + 1) % n , pos , -1);
Add(pos , pos + 1 , MOD);
val[pos] = n - i;
}
return;
}
int compare_plants(int x, int y) {
if(val[x] < val[y]) return -1;
if(val[x] > val[y]) return 1;
return 0;
}
Compilation message
plants.cpp: In function 'void build(int, int, int)':
plants.cpp:23:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
23 | int mid = l + r >> 1;
| ~~^~~
plants.cpp: In function 'void add(int, int, int, int, int, int)':
plants.cpp:43:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
43 | int mid = l + r >> 1;
| ~~^~~
plants.cpp: In function 'pii get(int, int, int, int, int)':
plants.cpp:53:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
53 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
332 KB |
Output is correct |
7 |
Correct |
95 ms |
3392 KB |
Output is correct |
8 |
Correct |
4 ms |
332 KB |
Output is correct |
9 |
Correct |
3 ms |
332 KB |
Output is correct |
10 |
Correct |
81 ms |
3332 KB |
Output is correct |
11 |
Correct |
69 ms |
3296 KB |
Output is correct |
12 |
Correct |
69 ms |
3504 KB |
Output is correct |
13 |
Correct |
88 ms |
3444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
332 KB |
Output is correct |
7 |
Correct |
95 ms |
3392 KB |
Output is correct |
8 |
Correct |
4 ms |
332 KB |
Output is correct |
9 |
Correct |
3 ms |
332 KB |
Output is correct |
10 |
Correct |
81 ms |
3332 KB |
Output is correct |
11 |
Correct |
69 ms |
3296 KB |
Output is correct |
12 |
Correct |
69 ms |
3504 KB |
Output is correct |
13 |
Correct |
88 ms |
3444 KB |
Output is correct |
14 |
Correct |
103 ms |
4160 KB |
Output is correct |
15 |
Correct |
754 ms |
11948 KB |
Output is correct |
16 |
Correct |
105 ms |
4164 KB |
Output is correct |
17 |
Correct |
631 ms |
11848 KB |
Output is correct |
18 |
Correct |
420 ms |
11836 KB |
Output is correct |
19 |
Correct |
417 ms |
11856 KB |
Output is correct |
20 |
Correct |
573 ms |
11972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
81 ms |
3180 KB |
Output is correct |
4 |
Execution timed out |
4067 ms |
10780 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |