#include "plants.h"
#include<bits/stdc++.h>
using namespace std;
#define sz(x) (int)x.size()
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define pb push_back
#define lowb(x) (x&(-x))
#define ALL(_x) _x.begin(),_x.end()
#define pii pair<int,int>
#define f first
#define s second
#define SORT_UNIQUE(x) sort(ALL(x)),x.erase(unique(ALL(x)),x.end())
#define ll long long
#define MNTO(x,y) x=min(x,y)
#define MXTO(x,y) x=max(x,y)
const int maxn=2e5+5;
const int INF=0x3f3f3f3f;
namespace {
int seg[4*maxn],pos[4*maxn];
int arr[maxn];
int lazy[4*maxn];
int ans[maxn];
int cur;
int k;
int n;
}
void pushdown(int idx,int l,int r){
if(!lazy[idx]){
return;
}
seg[idx]+=lazy[idx];
if(l==r){
lazy[idx]=0;
return;
}
lazy[idx*2]+=lazy[idx];
lazy[idx*2+1]+=lazy[idx];
lazy[idx]=0;
}
void build(int idx,int l,int r){
if(l==r){
seg[idx]=arr[l];
pos[idx]=l;
return;
}
int mid=(l+r)/2;
build(idx*2,l,mid),build(idx*2+1,mid+1,r);
seg[idx]=min(seg[idx*2],seg[idx*2+1]);
if(seg[idx]==seg[idx*2]) pos[idx]=pos[idx*2];
else pos[idx]=pos[idx*2+1];
}
void upd(int idx,int l,int r,int ql,int qr,int x){
if(ql>qr) return;
pushdown(idx,l,r);
if(r<ql or l>qr) return;
if(ql<=l and r<=qr){
lazy[idx]+=x;
pushdown(idx,l,r);
return;
}
int mid=(l+r)/2;
upd(idx*2,l,mid,ql,qr,x);
upd(idx*2+1,mid+1,r,ql,qr,x);
seg[idx]=min(seg[idx*2],seg[idx*2+1]);
if(seg[idx]==seg[idx*2]) pos[idx]=pos[idx*2];
else pos[idx]=pos[idx*2+1];
}
pii query(int idx,int l,int r,int ql,int qr){
if(ql>qr) return {INF,-1};
pushdown(idx,l,r);
if(r<ql or l>qr) return {INF,-1};
if(ql<=l and r<=qr){
return {seg[idx],pos[idx]};
}
int mid=(l+r)/2;
return min(query(idx*2,l,mid,ql,qr),query(idx*2+1,mid+1,r,ql,qr));
}
void get(int z){
//cout<<z<<'\n';
pii mn;
while(true){
if(z-k<0){
mn=min(query(1,0,n-1,0,z-1),query(1,0,n-1,n+(z-k),n-1));
}
else{
mn=query(1,0,n-1,z-k,z-1);
}
if(mn.f==0){
get(mn.s);
}
else{
break;
}
}
ans[z]=cur--;
if(z-k<0){
upd(1,0,n-1,0,z-1,-1);
upd(1,0,n-1,n+(z-k),n-1,-1);
}
else{
upd(1,0,n-1,z-k,z-1,-1);
}
upd(1,0,n-1,z,z,INF);
}
void init(int K, std::vector<int> r) {
k=K;
--k;
n=sz(r);
REP(i,n){
arr[i]=r[i];
}
cur=n;
build(1,0,n-1);
while(cur>0){
int z=query(1,0,n-1,0,n-1).s;
assert(query(1,0,n-1,0,n-1).f==0);
get(z);
}
}
int compare_plants(int x, int y) {
if(ans[x]>ans[y]){
return 1;
}
return -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
53 ms |
3460 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
340 KB |
Output is correct |
10 |
Correct |
54 ms |
3436 KB |
Output is correct |
11 |
Correct |
47 ms |
3324 KB |
Output is correct |
12 |
Correct |
48 ms |
3500 KB |
Output is correct |
13 |
Correct |
52 ms |
3448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
53 ms |
3460 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
340 KB |
Output is correct |
10 |
Correct |
54 ms |
3436 KB |
Output is correct |
11 |
Correct |
47 ms |
3324 KB |
Output is correct |
12 |
Correct |
48 ms |
3500 KB |
Output is correct |
13 |
Correct |
52 ms |
3448 KB |
Output is correct |
14 |
Correct |
79 ms |
4288 KB |
Output is correct |
15 |
Correct |
457 ms |
15724 KB |
Output is correct |
16 |
Correct |
80 ms |
6440 KB |
Output is correct |
17 |
Correct |
444 ms |
15728 KB |
Output is correct |
18 |
Correct |
305 ms |
15444 KB |
Output is correct |
19 |
Correct |
303 ms |
15696 KB |
Output is correct |
20 |
Correct |
408 ms |
15748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
49 ms |
3176 KB |
Output is correct |
4 |
Correct |
261 ms |
16348 KB |
Output is correct |
5 |
Correct |
306 ms |
15132 KB |
Output is correct |
6 |
Correct |
406 ms |
15312 KB |
Output is correct |
7 |
Correct |
498 ms |
15456 KB |
Output is correct |
8 |
Correct |
450 ms |
15592 KB |
Output is correct |
9 |
Correct |
275 ms |
15144 KB |
Output is correct |
10 |
Correct |
267 ms |
15052 KB |
Output is correct |
11 |
Correct |
215 ms |
14308 KB |
Output is correct |
12 |
Correct |
262 ms |
15056 KB |
Output is correct |
13 |
Correct |
300 ms |
15052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |