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 <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
int r[222222], lz[888888], rnk[222222], k;
pair<int, int> t[888888];
void scl(int l, int r, int n){
t[n]={0, l};
lz[n]=0;
if(l==r) return;
int m=l+r>>1;
scl(l, m, n*2); scl(m+1, r, n*2+1);
}
void lzup(int l, int r, int n){
t[n].first+=lz[n];
if(l^r) lz[n*2]+=lz[n], lz[n*2+1]+=lz[n];
lz[n]=0;
}
void upd(int l, int r, int n, int s, int e, int x){
lzup(l, r, n);
if(r<s || e<l) return;
if(s<=l && r<=e){
lz[n]+=x;
lzup(l, r, n);
return;
}
int m=l+r>>1;
upd(l, m, n*2, s, e, x); upd(m+1, r, n*2+1, s, e, x);
t[n]=min(t[n*2], t[n*2+1]);
}
pair<int, int> mn(int l, int r, int n, int s, int e){
lzup(l, r, n);
if(r<s || e<l) return {1e9, 1e9};
if(s<=l && r<=e) return t[n];
int m=l+r>>1;
return min(mn(l, m, n*2, s, e), mn(m+1, r, n*2+1, s, e));
}
pair<int, int> fmn(int l, int r, int n, int s, int e){
if(e<1) return mn(l, r, n, r+s, r+e);
if(s>r) return mn(l, r, n, s-r, e-r);
if(e>r){
pair<int, int> x=mn(l, r, n, s, r);
if(!x.first) return x;
return min(x, mn(l, r, n, 1, e-r));
}
if(s<1){
pair<int, int> x=mn(l, r, n, r+s, r);
if(!x.first) return x;
return min(mn(l, r, n, 1, e), x);
}
return mn(l, r, n, s, e);
}
void fupd(int l, int r, int n, int s, int e, int x){
if(s>0 && e<=r) return upd(l, r, n, s, e, x);
if(e<1) return upd(l, r, n, r+s, r+e, x);
if(s>r) return upd(l, r, n, s-r, e-r, x);
if(e>r) return upd(l, r, n, s, r, x), upd(l, r, n, 1, e-r, x);
if(s<1) return upd(l, r, n, 1, e, x), upd(l, r, n, r+s, r, x);
}
void init(int _k, vector<int> _r){
int n=_r.size(), k=_k;
scl(1, n, 1);
vector<int> v;
for(int i=1;i<=n;i++) r[i]=_r[i-1], upd(1, n, 1, i, i, r[i]);
for(int i=1;i<=n;i++) if(!r[i] && fmn(1, n, 1, i-k+1, i-1).first) rnk[i]=1, v.push_back(i);
for(int i=0;i<v.size();i++){
fupd(1, n, 1, v[i]-k+1, v[i]-1, -1); fupd(1, n, 1, v[i], v[i], 1e9);
pair<int, int> lx=fmn(1, n, 1, v[i]-k+1, v[i]-1), rx=fmn(1, n, 1, v[i]+1, v[i]+k-1);
if(!lx.first && fmn(1, n, 1, lx.second-k+1, i-1).first) rnk[lx.second]=rnk[v[i]]+1, v.push_back(lx.second);
if(!rx.first && fmn(1, n, 1, rx.second-k+1, i-1).first) rnk[rx.second]=rnk[v[i]]+1, v.push_back(rx.second);
}
return;
}
int compare_plants(int x, int y){
x++; y++;
return (rnk[x]>rnk[y])?-1:1;
}
Compilation message (stderr)
plants.cpp: In function 'void scl(int, int, int)':
plants.cpp:14:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
14 | int m=l+r>>1;
| ~^~
plants.cpp: In function 'void upd(int, int, int, int, int, int)':
plants.cpp:32:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
32 | int m=l+r>>1;
| ~^~
plants.cpp: In function 'std::pair<int, int> mn(int, int, int, int, int)':
plants.cpp:41:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
41 | int m=l+r>>1;
| ~^~
plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:75:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for(int i=0;i<v.size();i++){
| ~^~~~~~~~~
# | 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... |