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 <bits/stdc++.h>
#include "plants.h"
using namespace std;
const int nax=4e5+5;
#define fi first
#define se second
int n,k;
pair<int,int> segtree[4*nax];
int lazy[nax*4];
int ans[nax];
int tab[nax];
void build(int pos,int l,int r){
if(l==r){
segtree[pos]={tab[l],l};
return;
}
int mid=(r+l)/2;
build(pos*2+1,l,mid);
build(pos*2+2,mid+1,r);
segtree[pos]=min(segtree[pos*2+1],segtree[pos*2+2]);
return;
}
void propagate(int pos){
if(lazy[pos]){
segtree[pos*2+1].fi+=lazy[pos];
segtree[pos*2+2].fi+=lazy[pos];
lazy[pos*2+1]+=lazy[pos];
lazy[pos*2+2]+=lazy[pos];
}
lazy[pos]=0;
}
void update(int pos,int l,int r,int left,int right,int value){
if(l>r||l>right||r<left) return;
if(l>=left&&r<=right){
segtree[pos].fi+=value;
lazy[pos]+=value;
return;
}
int mid=(r+l)/2;
propagate(pos);
update(pos*2+1,l,mid,left,right,value);
update(pos*2+2,mid+1,r,left,right,value);
segtree[pos]=min(segtree[pos*2+1],segtree[pos*2+2]);
}
pair<int,int> query(int pos,int l,int r,int left,int right){
if(l>r||l>right||r<left) return {1e9,1e9};
if(l>=left&&r<=right){
return segtree[pos];
}
int mid=(r+l)/2;
propagate(pos);
return min(query(pos*2+1,l,mid,left,right),query(pos*2+2,mid+1,r,left,right));
}
pair<int,int> q(int l,int r){
if(l>r){
return min(query(0,0,n-1,0,r),query(0,0,n-1,l,n-1));
}else return query(0,0,n-1,l,r);
}
int jump(){
int x=query(0,0,n-1,0,n-1).se;
while(q((x-k+1+n)%n,(x-1+n)%n).fi==0){
x=q((x-k+1+n)%n,(x-1+n)%n).se;
}
return x;
}
void upd(int l,int r,int value){
if(l>r){
update(0,0,n-1,0,r,value);
update(0,0,n-1,l,n-1,value);
}else update(0,0,n-1,l,r,value);
}
void init(int K, std::vector<int> r) {
n=r.size();
k=K;
for (int i = 0; i < n; ++i) tab[i]=r[i];
build(0,0,n-1);
for (int i = n; i >= 1; --i)
{
int x=jump();
ans[x]=i;
upd((x-k+1+n)%n,(x-1+n)%n,-1);
upd(x,x,1e9);
}
return;
}
int compare_plants(int x, int y) {
return ( ans[x]>ans[y] ? 1 : -1);
}
# | 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... |