#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define fs first
#define sc second
const int B = 20;
const int mxn = 4e5+10;
int arr[mxn];
int N;
int K;
int perm[mxn];
int dp[mxn];
set<int> st;
set<pii> rng;
struct SEG{
#define ls now*2+1
#define rs now*2+2
#define mid ((l+r)>>1)
pii seg[mxn*4];
void modify(int now,int l,int r,int s,int e,int v){
if(l>=s&&e>=r){
seg[now].sc += v;
return;
}
if(mid>=s)modify(ls,l,mid,s,e,v);
if(mid<e)modify(rs,mid+1,r,s,e,v);
seg[now].fs = min(seg[ls].fs+seg[ls].sc,seg[rs].fs+seg[rs].sc);
return;
}
int get_mn(int now,int l,int r){
if(l == r){
return l;
}
if(seg[ls].fs+seg[ls].sc<seg[rs].fs+seg[rs].sc)return get_mn(ls,l,mid);
else return get_mn(rs,mid+1,r);
}
#undef ls
#undef rs
#undef mid
}seg;
void add(int p){
int nxt = *st.upper_bound(p);
int pre = *(--st.upper_bound(p));
rng.erase(pii(nxt-pre,nxt));
rng.insert(pii(nxt-p,nxt));
rng.insert(pii(p-pre,p));
st.insert(p);
return;
}
void del(int p){
auto it = st.find(p);
int pre = *prev(it),nxt = *next(it);
rng.erase(pii(p-pre,p));
rng.erase(pii(nxt-p,nxt));
rng.insert(pii(nxt-pre,nxt));
st.erase(p);
return;
}
struct DB{
int dp[mxn][B];
DB(){
memset(dp,-1,sizeof(dp));
}
void build(int s,int e){
if(e<s){
for(int i = s;i>=e;i--){
for(int j = 1;j<B;j++){
int pre = dp[i][j-1];
if(pre == -1)continue;
dp[i][j] = dp[pre][j-1];
}
}
}
else{
for(int i = s;i<=e;i++){
for(int j = 1;j<B;j++){
int pre = dp[i][j-1];
if(pre == -1)continue;
dp[i][j] = dp[pre][j-1];
}
}
}
}
};
DB lmx,rmx,lmn,rmn;
void calc(int id){
auto it = rng.rbegin();
if(it->sc >= N*2)it++;
auto pos = it->sc;
pos %= N;
//cerr<<id<<":"<<pos<<','<<it->fs<<endl;
assert(it->fs>=K);
del(pos);
del(pos+N);
perm[pos] = id;
seg.modify(0,0,N*2-1,pos+N-K+1,pos+N,-1);
seg.modify(0,0,N*2-1,max(pos-K+1,0),pos,-1);
if(pos-K+1<0)seg.modify(0,0,N*2-1,N*2-1+(pos-K+1)+1,N*2-1,-1);
while(seg.seg[0].fs+seg.seg[0].sc == 0){
int p = seg.get_mn(0,0,N*2-1);
p %= N;
add(p);
seg.modify(0,0,N*2-1,p,p,mxn*4);
add(p+N);
seg.modify(0,0,N*2-1,p+N,p+N,mxn*4);
}
return;
}
void init(int k, std::vector<int> r) {
K = k;
N = r.size();
for(int i = 0;i<N;i++)arr[i] = arr[i+N] = r[i];
st.insert(-1);
st.insert(N*2);
rng.insert(pii(N*2+1,N*2));
for(int i = 0;i<N*2;i++){
if(!arr[i]){
add(i);
}
if(arr[i])seg.modify(0,0,N*2-1,i,i,arr[i]);
else seg.modify(0,0,N*2-1,i,i,mxn*4);
}
//cerr<<"INIT!: "<<rng.rbegin()->fs<<endl;
//for(auto &i:rng)cerr<<i.fs<<','<<i.sc<<endl;
for(int i = N-1;i>=0;i--)calc(i);
//for(int i = 0;i<N;i++)cerr<<perm[i]<<',';cerr<<endl;
for(int i = N;i<N*2;i++)perm[i] = perm[i-N];
set<pii> st;
for(int i = 0;i<K-1;i++){
auto it = st.lower_bound(pii(perm[i],-1));
if(it != st.end())lmx.dp[i][0] = it->sc;
if(it != st.begin()){
it--;
lmn.dp[i][0] = it->sc;
}
st.insert(pii(perm[i],i));
}
for(int i = K-1;i<N*2;i++){
auto it = st.lower_bound(pii(perm[i],-1));
if(it != st.end())lmx.dp[i][0] = it->sc;
if(it != st.begin()){
it--;
lmn.dp[i][0] = it->sc;
}
st.erase(pii(perm[i-K+1],i-K+1));
st.insert(pii(perm[i],i));
}
st.clear();
for(int i = 0;i<K-1;i++){
auto it = st.lower_bound(pii(perm[N*2-1-i],-1));
if(it != st.end())rmx.dp[N*2-1-i][0] = it->sc;
if(it != st.begin()){
it--;
rmn.dp[N*2-1-i][0] = it->sc;
}
st.insert(pii(perm[N*2-1-i],N*2-1-i));
}
for(int i = K-1;i<N*2;i++){
auto it = st.lower_bound(pii(perm[N*2-1-i],-1));
if(it != st.end())rmx.dp[N*2-1-i][0] = it->sc;
if(it != st.begin()){
it--;
rmn.dp[N*2-1-i][0] = it->sc;
}
st.erase(pii(perm[N*2-1-i+K-1],N*2-1-i+K-1));
st.insert(pii(perm[N*2-1-i],N*2-1-i));
}
rmn.build(N*2-1,0);
rmx.build(N*2-1,0);
lmn.build(0,N*2-1);
lmx.build(0,N*2-1);
/*
cerr<<"PERM: "<<endl;for(int i = 0;i<N*2;i++)cerr<<perm[i]<<' ';cerr<<endl;
cerr<<"LMX: "<<endl;for(int i = 0;i<N*2;i++)cerr<<lmx.dp[i][0]<<' ';cerr<<endl;
cerr<<"LMN: "<<endl;for(int i = 0;i<N*2;i++)cerr<<lmn.dp[i][0]<<' ';cerr<<endl;
cerr<<"RMX: "<<endl;for(int i = 0;i<N*2;i++)cerr<<rmx.dp[i][0]<<' ';cerr<<endl;
cerr<<"RMN: "<<endl;for(int i = 0;i<N*2;i++)cerr<<rmn.dp[i][0]<<' ';cerr<<endl;
*/
return;
}
int compare_plants(int x, int y) {
int ans = 0;
/*
int s = x,e = y;
if(e<s)e += N;
while(e-s>=K){
if(rmx.dp[s][0] != -1&&rmx.dp[s][0]<e)s = rmx.dp[s][0];
else break;
}
if(e-s<K&&perm[e]>perm[s])ans = -1;
s = x,e = y;
if(e>s)s += N;
while(s-e>=K){
if(lmx.dp[s][0] != -1&&lmx.dp[s][0]>e)s = lmx.dp[s][0];
else break;
}
if(s-e<K&&perm[e]>perm[s])ans = -1;
s = x,e = y;
if(e<s)e += N;
while(e-s>=K){
if(rmn.dp[s][0] != -1&&rmn.dp[s][0]<e)s = rmn.dp[s][0];
else break;
}
if(e-s<K&&perm[e]<perm[s])ans = 1;
s = x,e = y;
if(e>s)s += N;
while(s-e>=K){
if(lmn.dp[s][0] != -1&&lmn.dp[s][0]>e)s = lmn.dp[s][0];
else break;
}
if(s-e<K&&perm[e]<perm[s])ans = 1;
*/
int s = x,e = y;
if(s>e)e += N;
for(int i = B-1;i>=0;i--){
if(e-s<K)break;
if(rmx.dp[s][i] != -1&&rmx.dp[s][i]<e)s = rmx.dp[s][i];
}
if(e-s<K&&perm[e]>perm[s])ans = -1;
s = x,e = y;
if(e>s)s += N;
for(int i = B-1;i>=0;i--){
if(s-e<K)break;
if(lmx.dp[s][i] != -1&&lmx.dp[s][i]>e)s = lmx.dp[s][i];
}
if(s-e<K&&perm[e]>perm[s])ans = -1;
s = x,e = y;
if(e<s)e += N;
for(int i = B-1;i>=0;i--){
if(e-s<K)break;
if(rmn.dp[s][i] != -1&&rmn.dp[s][i]<e)s = rmn.dp[s][i];
}
if(e-s<K&&perm[e]<perm[s])ans = 1;
s = x,e = y;
if(e>s)s += N;
for(int i = B-1;i>=0;i--){
if(s-e<K)break;
if(lmn.dp[s][i] != -1&&lmn.dp[s][i]>e)s = lmn.dp[s][i];
}
if(s-e<K&&perm[e]<perm[s])ans = 1;
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
129884 KB |
Output is correct |
2 |
Correct |
11 ms |
129628 KB |
Output is correct |
3 |
Correct |
12 ms |
129804 KB |
Output is correct |
4 |
Correct |
11 ms |
129628 KB |
Output is correct |
5 |
Correct |
11 ms |
129640 KB |
Output is correct |
6 |
Correct |
47 ms |
132436 KB |
Output is correct |
7 |
Correct |
105 ms |
135068 KB |
Output is correct |
8 |
Correct |
482 ms |
161168 KB |
Output is correct |
9 |
Correct |
510 ms |
160596 KB |
Output is correct |
10 |
Correct |
513 ms |
160852 KB |
Output is correct |
11 |
Correct |
611 ms |
162972 KB |
Output is correct |
12 |
Correct |
592 ms |
165200 KB |
Output is correct |
13 |
Correct |
692 ms |
182864 KB |
Output is correct |
14 |
Correct |
348 ms |
145488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
129812 KB |
Output is correct |
2 |
Correct |
11 ms |
129884 KB |
Output is correct |
3 |
Correct |
11 ms |
129756 KB |
Output is correct |
4 |
Correct |
13 ms |
129624 KB |
Output is correct |
5 |
Correct |
11 ms |
129828 KB |
Output is correct |
6 |
Correct |
14 ms |
129884 KB |
Output is correct |
7 |
Correct |
51 ms |
132876 KB |
Output is correct |
8 |
Correct |
12 ms |
129884 KB |
Output is correct |
9 |
Correct |
14 ms |
129964 KB |
Output is correct |
10 |
Correct |
52 ms |
132948 KB |
Output is correct |
11 |
Correct |
58 ms |
133108 KB |
Output is correct |
12 |
Correct |
53 ms |
132948 KB |
Output is correct |
13 |
Correct |
47 ms |
132952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
129812 KB |
Output is correct |
2 |
Correct |
11 ms |
129884 KB |
Output is correct |
3 |
Correct |
11 ms |
129756 KB |
Output is correct |
4 |
Correct |
13 ms |
129624 KB |
Output is correct |
5 |
Correct |
11 ms |
129828 KB |
Output is correct |
6 |
Correct |
14 ms |
129884 KB |
Output is correct |
7 |
Correct |
51 ms |
132876 KB |
Output is correct |
8 |
Correct |
12 ms |
129884 KB |
Output is correct |
9 |
Correct |
14 ms |
129964 KB |
Output is correct |
10 |
Correct |
52 ms |
132948 KB |
Output is correct |
11 |
Correct |
58 ms |
133108 KB |
Output is correct |
12 |
Correct |
53 ms |
132948 KB |
Output is correct |
13 |
Correct |
47 ms |
132952 KB |
Output is correct |
14 |
Correct |
101 ms |
134020 KB |
Output is correct |
15 |
Correct |
794 ms |
148308 KB |
Output is correct |
16 |
Correct |
103 ms |
134152 KB |
Output is correct |
17 |
Correct |
786 ms |
148424 KB |
Output is correct |
18 |
Correct |
814 ms |
161360 KB |
Output is correct |
19 |
Correct |
445 ms |
147316 KB |
Output is correct |
20 |
Correct |
692 ms |
151928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
129624 KB |
Output is correct |
2 |
Correct |
11 ms |
129628 KB |
Output is correct |
3 |
Correct |
74 ms |
132752 KB |
Output is correct |
4 |
Correct |
632 ms |
154960 KB |
Output is correct |
5 |
Correct |
594 ms |
145308 KB |
Output is correct |
6 |
Correct |
705 ms |
142672 KB |
Output is correct |
7 |
Correct |
777 ms |
142932 KB |
Output is correct |
8 |
Correct |
794 ms |
146756 KB |
Output is correct |
9 |
Correct |
613 ms |
151948 KB |
Output is correct |
10 |
Correct |
583 ms |
145500 KB |
Output is correct |
11 |
Correct |
694 ms |
180052 KB |
Output is correct |
12 |
Correct |
391 ms |
142620 KB |
Output is correct |
13 |
Correct |
800 ms |
168784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
129624 KB |
Output is correct |
2 |
Correct |
11 ms |
129628 KB |
Output is correct |
3 |
Correct |
11 ms |
129624 KB |
Output is correct |
4 |
Correct |
11 ms |
129864 KB |
Output is correct |
5 |
Correct |
11 ms |
129712 KB |
Output is correct |
6 |
Correct |
12 ms |
129796 KB |
Output is correct |
7 |
Correct |
22 ms |
130396 KB |
Output is correct |
8 |
Correct |
20 ms |
130380 KB |
Output is correct |
9 |
Correct |
23 ms |
130392 KB |
Output is correct |
10 |
Correct |
20 ms |
130392 KB |
Output is correct |
11 |
Correct |
22 ms |
130412 KB |
Output is correct |
12 |
Correct |
24 ms |
130648 KB |
Output is correct |
13 |
Correct |
17 ms |
130396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
129624 KB |
Output is correct |
2 |
Correct |
11 ms |
129764 KB |
Output is correct |
3 |
Correct |
11 ms |
129836 KB |
Output is correct |
4 |
Correct |
13 ms |
129624 KB |
Output is correct |
5 |
Correct |
14 ms |
129876 KB |
Output is correct |
6 |
Correct |
559 ms |
142936 KB |
Output is correct |
7 |
Correct |
616 ms |
142676 KB |
Output is correct |
8 |
Correct |
720 ms |
142932 KB |
Output is correct |
9 |
Correct |
772 ms |
146768 KB |
Output is correct |
10 |
Correct |
540 ms |
151784 KB |
Output is correct |
11 |
Correct |
734 ms |
149076 KB |
Output is correct |
12 |
Correct |
477 ms |
157168 KB |
Output is correct |
13 |
Correct |
573 ms |
147540 KB |
Output is correct |
14 |
Correct |
656 ms |
145336 KB |
Output is correct |
15 |
Correct |
769 ms |
145640 KB |
Output is correct |
16 |
Correct |
502 ms |
150868 KB |
Output is correct |
17 |
Correct |
513 ms |
145636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
129884 KB |
Output is correct |
2 |
Correct |
11 ms |
129628 KB |
Output is correct |
3 |
Correct |
12 ms |
129804 KB |
Output is correct |
4 |
Correct |
11 ms |
129628 KB |
Output is correct |
5 |
Correct |
11 ms |
129640 KB |
Output is correct |
6 |
Correct |
47 ms |
132436 KB |
Output is correct |
7 |
Correct |
105 ms |
135068 KB |
Output is correct |
8 |
Correct |
482 ms |
161168 KB |
Output is correct |
9 |
Correct |
510 ms |
160596 KB |
Output is correct |
10 |
Correct |
513 ms |
160852 KB |
Output is correct |
11 |
Correct |
611 ms |
162972 KB |
Output is correct |
12 |
Correct |
592 ms |
165200 KB |
Output is correct |
13 |
Correct |
692 ms |
182864 KB |
Output is correct |
14 |
Correct |
348 ms |
145488 KB |
Output is correct |
15 |
Correct |
12 ms |
129812 KB |
Output is correct |
16 |
Correct |
11 ms |
129884 KB |
Output is correct |
17 |
Correct |
11 ms |
129756 KB |
Output is correct |
18 |
Correct |
13 ms |
129624 KB |
Output is correct |
19 |
Correct |
11 ms |
129828 KB |
Output is correct |
20 |
Correct |
14 ms |
129884 KB |
Output is correct |
21 |
Correct |
51 ms |
132876 KB |
Output is correct |
22 |
Correct |
12 ms |
129884 KB |
Output is correct |
23 |
Correct |
14 ms |
129964 KB |
Output is correct |
24 |
Correct |
52 ms |
132948 KB |
Output is correct |
25 |
Correct |
58 ms |
133108 KB |
Output is correct |
26 |
Correct |
53 ms |
132948 KB |
Output is correct |
27 |
Correct |
47 ms |
132952 KB |
Output is correct |
28 |
Correct |
101 ms |
134020 KB |
Output is correct |
29 |
Correct |
794 ms |
148308 KB |
Output is correct |
30 |
Correct |
103 ms |
134152 KB |
Output is correct |
31 |
Correct |
786 ms |
148424 KB |
Output is correct |
32 |
Correct |
814 ms |
161360 KB |
Output is correct |
33 |
Correct |
445 ms |
147316 KB |
Output is correct |
34 |
Correct |
692 ms |
151928 KB |
Output is correct |
35 |
Correct |
11 ms |
129624 KB |
Output is correct |
36 |
Correct |
11 ms |
129628 KB |
Output is correct |
37 |
Correct |
74 ms |
132752 KB |
Output is correct |
38 |
Correct |
632 ms |
154960 KB |
Output is correct |
39 |
Correct |
594 ms |
145308 KB |
Output is correct |
40 |
Correct |
705 ms |
142672 KB |
Output is correct |
41 |
Correct |
777 ms |
142932 KB |
Output is correct |
42 |
Correct |
794 ms |
146756 KB |
Output is correct |
43 |
Correct |
613 ms |
151948 KB |
Output is correct |
44 |
Correct |
583 ms |
145500 KB |
Output is correct |
45 |
Correct |
694 ms |
180052 KB |
Output is correct |
46 |
Correct |
391 ms |
142620 KB |
Output is correct |
47 |
Correct |
800 ms |
168784 KB |
Output is correct |
48 |
Correct |
12 ms |
129624 KB |
Output is correct |
49 |
Correct |
11 ms |
129628 KB |
Output is correct |
50 |
Correct |
11 ms |
129624 KB |
Output is correct |
51 |
Correct |
11 ms |
129864 KB |
Output is correct |
52 |
Correct |
11 ms |
129712 KB |
Output is correct |
53 |
Correct |
12 ms |
129796 KB |
Output is correct |
54 |
Correct |
22 ms |
130396 KB |
Output is correct |
55 |
Correct |
20 ms |
130380 KB |
Output is correct |
56 |
Correct |
23 ms |
130392 KB |
Output is correct |
57 |
Correct |
20 ms |
130392 KB |
Output is correct |
58 |
Correct |
22 ms |
130412 KB |
Output is correct |
59 |
Correct |
24 ms |
130648 KB |
Output is correct |
60 |
Correct |
17 ms |
130396 KB |
Output is correct |
61 |
Correct |
61 ms |
134224 KB |
Output is correct |
62 |
Correct |
104 ms |
136260 KB |
Output is correct |
63 |
Correct |
501 ms |
149844 KB |
Output is correct |
64 |
Correct |
613 ms |
146004 KB |
Output is correct |
65 |
Correct |
703 ms |
145936 KB |
Output is correct |
66 |
Correct |
783 ms |
146516 KB |
Output is correct |
67 |
Correct |
796 ms |
150408 KB |
Output is correct |
68 |
Correct |
625 ms |
154708 KB |
Output is correct |
69 |
Correct |
751 ms |
149992 KB |
Output is correct |
70 |
Correct |
581 ms |
158032 KB |
Output is correct |
71 |
Correct |
623 ms |
148384 KB |
Output is correct |
72 |
Correct |
723 ms |
146000 KB |
Output is correct |
73 |
Correct |
777 ms |
146516 KB |
Output is correct |
74 |
Correct |
502 ms |
148304 KB |
Output is correct |
75 |
Correct |
591 ms |
146796 KB |
Output is correct |