This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 | }
      | ^| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |