#include "plants.h"
#include<bits/stdc++.h>
using namespace std;
typedef int ll;
#define pll pair<int,int>
#define f first
#define s second
pll ST[808080];
int lz[808080],inf = 1e8,num[202020];
pll hap(pll a,pll b) {
if(a.f <= b.f) return a;
if(a.f > b.f) return b;
}
void busy(ll id) {
lz[id*2] += lz[id]; lz[id*2+1] += lz[id];
ST[id*2].f += lz[id], ST[id*2+1].f += lz[id];
lz[id] = 0;
}
void upd(ll id,ll s,ll e,ll qs,ll qe,ll v) {
if(s > qe || e < qs) return;
if(s == e) { ST[id].f += v; ST[id].s = s; return; }
if(qs <= s && e <= qe) { ST[id].f += v; lz[id] += v; return; }
ll m = s+e>>1;
busy(id);
upd(id*2,s,m,qs,qe,v); upd(id*2+1,m+1,e,qs,qe,v);
ST[id] = hap(ST[id*2],ST[id*2+1]);
}
pll query(ll id,ll s,ll e,ll qs,ll qe) {
if(s > qe || e < qs) return {inf,-1};
if(qs <= s && e <= qe) return ST[id];
ll m = s+e>>1;
busy(id);
return hap(query(id*2,s,m,qs,qe),query(id*2+1,m+1,e,qs,qe));
}
int n,hp;
void init(int k, vector<int> r) {
n = r.size();
for(int i = 0 ; i < n ; i++) upd(1,0,n-1,i,i,r[i]);
for(int i = n-k+1 ; i < n ; i++) {
hp += (r[i]>0?1:0);
}
int l = n-k+1,R = 0;
while(R < n) {
if(r[R] == 0 && hp == k-1) break;
hp += (r[R]>0?1:0);
hp -= (r[l]>0?1:0);
l = (l+1)%n; R = (R+1)%n;
}
for(int i = 1 ; i < n ; i++) {
num[R] = i;
upd(1,0,n-1,R,R,inf);
if(R+1 >= k) upd(1,0,n-1,R+1-k,R,-1);
else upd(1,0,n-1,0,R,-1),upd(1,0,n-1,R+1-k+n,n-1,-1);
///R의 왼쪽 편에 다음 R이 있는지 확인
pll t;
if(R+1 >= k) t = query(1,0,n-1,R+1-k,R);
else t = hap(query(1,0,n-1,R+1-k+n,n-1),query(1,0,n-1,0,R));
if(t.f == 0) {
pll t2; ll R2 = t.s;
if(R2 == 0) t2 = query(1,0,n-1,n-k+1,n-1);
else if(R2+1 >= k) t2 = query(1,0,n-1,R2+1-k,R2-1);
else t2 = hap(query(1,0,n-1,R2+1-k+n,n-1),query(1,0,n-1,0,R2-1));
if(t2.f != 0) { R = R2; continue; }
}
///R의 오른 편에 다음 R이 있는지 확인, 반드시 있어야 함
if(R+k-1 < n) t = query(1,0,n-1,R,R+k-1);
else t = hap(query(1,0,n-1,R,n-1),query(1,0,n-1,0,R+k-1-n));
R = t.s;
}
num[R] = n;
}
int compare_plants(int x, int y) {
if(num[x] < num[y]) return 1;
return -1;
}
Compilation message
plants.cpp: In function 'void upd(ll, ll, ll, ll, ll, ll)':
plants.cpp:25:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
25 | ll m = s+e>>1;
| ^
plants.cpp: In function 'std::pair<int, int> query(ll, ll, ll, ll, ll)':
plants.cpp:33:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
33 | ll m = s+e>>1;
| ^
plants.cpp: In function 'std::pair<int, int> hap(std::pair<int, int>, std::pair<int, int>)':
plants.cpp:15:1: warning: control reaches end of non-void function [-Wreturn-type]
15 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
296 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 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 |
5 ms |
460 KB |
Output is correct |
7 |
Correct |
69 ms |
4960 KB |
Output is correct |
8 |
Correct |
3 ms |
332 KB |
Output is correct |
9 |
Correct |
4 ms |
460 KB |
Output is correct |
10 |
Correct |
78 ms |
4968 KB |
Output is correct |
11 |
Correct |
69 ms |
4824 KB |
Output is correct |
12 |
Correct |
76 ms |
5120 KB |
Output is correct |
13 |
Correct |
81 ms |
4972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 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 |
5 ms |
460 KB |
Output is correct |
7 |
Correct |
69 ms |
4960 KB |
Output is correct |
8 |
Correct |
3 ms |
332 KB |
Output is correct |
9 |
Correct |
4 ms |
460 KB |
Output is correct |
10 |
Correct |
78 ms |
4968 KB |
Output is correct |
11 |
Correct |
69 ms |
4824 KB |
Output is correct |
12 |
Correct |
76 ms |
5120 KB |
Output is correct |
13 |
Correct |
81 ms |
4972 KB |
Output is correct |
14 |
Correct |
122 ms |
5748 KB |
Output is correct |
15 |
Correct |
876 ms |
12560 KB |
Output is correct |
16 |
Correct |
134 ms |
5676 KB |
Output is correct |
17 |
Correct |
828 ms |
12672 KB |
Output is correct |
18 |
Correct |
645 ms |
12768 KB |
Output is correct |
19 |
Correct |
583 ms |
12580 KB |
Output is correct |
20 |
Correct |
814 ms |
12612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
296 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |