#include "plants.h"
#include<bits/stdc++.h>
using namespace std;
int K, i, j, N, R[200010], X[200010], lazy[800010], A[200010], B[200010], sparse1[200010][20], sparse2[200010][20];
long long sparse3[200010][20], sparse4[200010][20];
struct st
{
int x, y;
const bool operator<(st a)const
{
if(x==a.x)return y<a.y;
return x>a.x;
}
};
set<st>s;
struct node
{
int x, y;
}seg[800010];
node merge(node a, node b)
{
if(a.x<b.x)return a;
if(a.x>b.x)return b;
return {a.x, min(a.y, b.y)};
}
void busy(int id)
{
seg[id*2].x+=lazy[id];
seg[id*2+1].x+=lazy[id];
lazy[id*2]+=lazy[id];
lazy[id*2+1]+=lazy[id];
lazy[id]=0;
}
void init(int id, int s, int e)
{
if(s==e)
{
seg[id].x=R[s];
seg[id].y=s;
return;
}
int m=s+e>>1;
init(id*2, s, m);init(id*2+1, m+1, e);
seg[id]=merge(seg[id*2], seg[id*2+1]);
}
void update(int id, int s, int e, int l, int r, int v)
{
if(e<l||r<s)return;
if(l<=s&&e<=r)
{
lazy[id]+=v;
seg[id].x+=v;
return;
}
busy(id);int m=s+e>>1;
update(id*2, s, m, l, r, v);update(id*2+1, m+1, e, l, r, v);
seg[id]=merge(seg[id*2], seg[id*2+1]);
}
node get(int id, int s, int e, int l, int r)
{
if(e<l||r<s)return {(int)1e9, 0};
if(l<=s&&e<=r)return seg[id];
busy(id);int m=s+e>>1;
return merge(get(id*2, s, m, l, r), get(id*2+1, m+1, e, l, r));
}
int f(int a)
{
a%=N;
if(a<=0)a+=N;
return a;
}
void init(int k, vector<int>r)
{
K=k;N=r.size();for(i=0;i++<N;)X[i]=-1;
for(i=0;i++<N;)R[i]=r[i-1];
init(1, 1, N);
vector<int>v;
for(i=0;i++<N;)
{
if(R[i]==0)
{
if(i>=K)
{
node a=get(1, 1, N, i-K+1, i-1);
if(a.x)v.push_back(i);
}
else
{
node a=merge(get(1, 1, N, 1, i-1), get(1, 1, N, N-K+i+1, N));
if(a.x)v.push_back(i);
}
}
}
int group=0;
while(v.size())
{
vector<int>V;
for(auto i:v)
{
if(i>=K)update(1, 1, N, i-K+1, i-1, -1);
else update(1, 1, N, 1, i-1, -1),update(1, 1, N, N-k+i+1, N, -1);
X[i]=group;update(1, 1, N, i, i, 1e9);
}
for(auto i:v)
{
if(i>=K)
{
node a=get(1, 1, N, i-K+1, i-1);
if(a.x==0)V.push_back(a.y);
}
else
{
node a=get(1, 1, N, 1, i-1);
node b=get(1, 1, N, N-k+i+1, N);
if(b.x==0)V.push_back(b.y);
else if(a.x==0)V.push_back(a.y);
}
if(i<=N-K+1)
{
node a=get(1, 1, N, i+1, i+K-1);
if(a.x==0)V.push_back(a.y);
}
else
{
node a=get(1, 1, N, i+1, N);
node b=get(1, 1, N, 1, i+K-1-N);
if(a.x==0)V.push_back(a.y);
else if(b.x==0)V.push_back(b.y);
}
}
sort(V.begin(), V.end());
v.clear();
for(auto i:V)
{
node a;
if(i>=K)a=get(1, 1, N, i-K+1, i-1);
else a=merge(get(1, 1, N, 1, i-1), get(1, 1, N, i-K+1+N, N));
if(a.x==0)continue;
if(!v.size()||v.back()!=i)v.push_back(i);
}
group++;
}
for(i=0;i++<N;)X[i]=N-X[i];
for(j=-K+2;j<=0;j++)s.insert({X[f(j)], j});
for(i=0;i++<N;)
{
if((*s.rbegin()).x>=X[i])A[i]=i;
else A[i]=f((*s.lower_bound({X[i]-1, (int)-1e9})).y);
s.erase({X[f(i-K+1)], i-K+1});
s.insert({X[i], i});
}
s.clear();
for(i=1;i<=N/2;i++)swap(X[i], X[N+1-i]);
for(j=-K+2;j<=0;j++)s.insert({X[f(j)], j});
for(i=0;i++<N;)
{
if((*s.rbegin()).x>=X[i])B[i]=i;
else B[i]=f((*s.lower_bound({X[i]-1, (int)-1e9})).y);
s.erase({X[f(i-K+1)], i-K+1});
s.insert({X[i], i});
}
for(i=1;i<=N/2;i++)swap(X[i], X[N+1-i]);
for(i=0;i++<N;)B[i]=N+1-B[i];
for(i=1;i<=N/2;i++)swap(B[i], B[N+1-i]);
for(i=0;i++<N;)sparse1[i][0]=A[i],sparse2[i][0]=B[i];
for(i=0;i++<N;)sparse3[i][0]=f(i-A[i])%N,sparse4[i][0]=f(B[i]-i)%N;
for(j=1;j<20;j++)
{
for(i=0;i++<N;)
{
sparse1[i][j]=sparse1[sparse1[i][j-1]][j-1];
sparse2[i][j]=sparse2[sparse2[i][j-1]][j-1];
sparse3[i][j]=sparse3[sparse1[i][j-1]][j-1]+sparse3[i][j-1];
sparse4[i][j]=sparse4[sparse2[i][j-1]][j-1]+sparse4[i][j-1];
}
}
return;
}
int compare_plants(int x, int y)
{
if(X[x+1]<X[y+1])return -compare_plants(y, x);
x++;y++;
int a=x, b=x;long long dist=0, dist2=0;
for(j=19;j>=0;j--)
{
if(X[sparse1[a][j]]>=X[y])
{
dist+=sparse3[a][j];
a=sparse1[a][j];
}
if(X[sparse2[b][j]]>=X[y])
{
dist2+=sparse4[b][j];
b=sparse2[b][j];
}
}
long long p=x-dist, q=x+dist2;
if((p<=y-N&&y-N<=q)||(p<=y&&y<=q)||(p<=y+N&&y+N<=q))return 1;
return 0;
}
Compilation message
plants.cpp: In function 'void init(int, int, int)':
plants.cpp:42:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
42 | int m=s+e>>1;
| ~^~
plants.cpp: In function 'void update(int, int, int, int, int, int)':
plants.cpp:55:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
55 | busy(id);int m=s+e>>1;
| ~^~
plants.cpp: In function 'node get(int, int, int, int, int)':
plants.cpp:63:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
63 | busy(id);int m=s+e>>1;
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
3 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
80 ms |
4156 KB |
Output is correct |
7 |
Correct |
207 ms |
15856 KB |
Output is correct |
8 |
Correct |
775 ms |
109632 KB |
Output is correct |
9 |
Correct |
826 ms |
109476 KB |
Output is correct |
10 |
Correct |
859 ms |
109440 KB |
Output is correct |
11 |
Correct |
878 ms |
109444 KB |
Output is correct |
12 |
Correct |
899 ms |
109444 KB |
Output is correct |
13 |
Correct |
929 ms |
109544 KB |
Output is correct |
14 |
Correct |
873 ms |
109444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
8 ms |
1012 KB |
Output is correct |
7 |
Correct |
159 ms |
6472 KB |
Output is correct |
8 |
Correct |
3 ms |
460 KB |
Output is correct |
9 |
Correct |
7 ms |
972 KB |
Output is correct |
10 |
Correct |
144 ms |
6576 KB |
Output is correct |
11 |
Correct |
134 ms |
6396 KB |
Output is correct |
12 |
Correct |
128 ms |
6576 KB |
Output is correct |
13 |
Correct |
175 ms |
6572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
8 ms |
1012 KB |
Output is correct |
7 |
Correct |
159 ms |
6472 KB |
Output is correct |
8 |
Correct |
3 ms |
460 KB |
Output is correct |
9 |
Correct |
7 ms |
972 KB |
Output is correct |
10 |
Correct |
144 ms |
6576 KB |
Output is correct |
11 |
Correct |
134 ms |
6396 KB |
Output is correct |
12 |
Correct |
128 ms |
6576 KB |
Output is correct |
13 |
Correct |
175 ms |
6572 KB |
Output is correct |
14 |
Correct |
301 ms |
15108 KB |
Output is correct |
15 |
Correct |
2029 ms |
113856 KB |
Output is correct |
16 |
Correct |
303 ms |
15088 KB |
Output is correct |
17 |
Correct |
2454 ms |
113944 KB |
Output is correct |
18 |
Correct |
1486 ms |
112820 KB |
Output is correct |
19 |
Correct |
1287 ms |
112808 KB |
Output is correct |
20 |
Correct |
2262 ms |
117532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
110 ms |
4676 KB |
Output is correct |
4 |
Correct |
1044 ms |
109444 KB |
Output is correct |
5 |
Correct |
1132 ms |
109444 KB |
Output is correct |
6 |
Correct |
1376 ms |
109440 KB |
Output is correct |
7 |
Correct |
1650 ms |
111388 KB |
Output is correct |
8 |
Correct |
1914 ms |
115340 KB |
Output is correct |
9 |
Correct |
1044 ms |
110460 KB |
Output is correct |
10 |
Correct |
971 ms |
110544 KB |
Output is correct |
11 |
Correct |
902 ms |
110456 KB |
Output is correct |
12 |
Correct |
953 ms |
110628 KB |
Output is correct |
13 |
Correct |
1354 ms |
113512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
460 KB |
Output is correct |
7 |
Correct |
19 ms |
1376 KB |
Output is correct |
8 |
Correct |
22 ms |
1484 KB |
Output is correct |
9 |
Correct |
20 ms |
1484 KB |
Output is correct |
10 |
Correct |
21 ms |
1440 KB |
Output is correct |
11 |
Correct |
21 ms |
1380 KB |
Output is correct |
12 |
Correct |
20 ms |
1384 KB |
Output is correct |
13 |
Correct |
26 ms |
1476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
4 ms |
844 KB |
Output is correct |
6 |
Correct |
1081 ms |
109548 KB |
Output is correct |
7 |
Correct |
1296 ms |
110004 KB |
Output is correct |
8 |
Correct |
1365 ms |
110496 KB |
Output is correct |
9 |
Correct |
1715 ms |
114480 KB |
Output is correct |
10 |
Correct |
871 ms |
109604 KB |
Output is correct |
11 |
Correct |
1492 ms |
113968 KB |
Output is correct |
12 |
Correct |
887 ms |
109484 KB |
Output is correct |
13 |
Correct |
1073 ms |
109664 KB |
Output is correct |
14 |
Correct |
1228 ms |
109904 KB |
Output is correct |
15 |
Correct |
1538 ms |
110432 KB |
Output is correct |
16 |
Correct |
1006 ms |
109564 KB |
Output is correct |
17 |
Correct |
999 ms |
109672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
3 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
80 ms |
4156 KB |
Output is correct |
7 |
Correct |
207 ms |
15856 KB |
Output is correct |
8 |
Correct |
775 ms |
109632 KB |
Output is correct |
9 |
Correct |
826 ms |
109476 KB |
Output is correct |
10 |
Correct |
859 ms |
109440 KB |
Output is correct |
11 |
Correct |
878 ms |
109444 KB |
Output is correct |
12 |
Correct |
899 ms |
109444 KB |
Output is correct |
13 |
Correct |
929 ms |
109544 KB |
Output is correct |
14 |
Correct |
873 ms |
109444 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
308 KB |
Output is correct |
20 |
Correct |
8 ms |
1012 KB |
Output is correct |
21 |
Correct |
159 ms |
6472 KB |
Output is correct |
22 |
Correct |
3 ms |
460 KB |
Output is correct |
23 |
Correct |
7 ms |
972 KB |
Output is correct |
24 |
Correct |
144 ms |
6576 KB |
Output is correct |
25 |
Correct |
134 ms |
6396 KB |
Output is correct |
26 |
Correct |
128 ms |
6576 KB |
Output is correct |
27 |
Correct |
175 ms |
6572 KB |
Output is correct |
28 |
Correct |
301 ms |
15108 KB |
Output is correct |
29 |
Correct |
2029 ms |
113856 KB |
Output is correct |
30 |
Correct |
303 ms |
15088 KB |
Output is correct |
31 |
Correct |
2454 ms |
113944 KB |
Output is correct |
32 |
Correct |
1486 ms |
112820 KB |
Output is correct |
33 |
Correct |
1287 ms |
112808 KB |
Output is correct |
34 |
Correct |
2262 ms |
117532 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
110 ms |
4676 KB |
Output is correct |
38 |
Correct |
1044 ms |
109444 KB |
Output is correct |
39 |
Correct |
1132 ms |
109444 KB |
Output is correct |
40 |
Correct |
1376 ms |
109440 KB |
Output is correct |
41 |
Correct |
1650 ms |
111388 KB |
Output is correct |
42 |
Correct |
1914 ms |
115340 KB |
Output is correct |
43 |
Correct |
1044 ms |
110460 KB |
Output is correct |
44 |
Correct |
971 ms |
110544 KB |
Output is correct |
45 |
Correct |
902 ms |
110456 KB |
Output is correct |
46 |
Correct |
953 ms |
110628 KB |
Output is correct |
47 |
Correct |
1354 ms |
113512 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
51 |
Correct |
1 ms |
304 KB |
Output is correct |
52 |
Correct |
1 ms |
332 KB |
Output is correct |
53 |
Correct |
3 ms |
460 KB |
Output is correct |
54 |
Correct |
19 ms |
1376 KB |
Output is correct |
55 |
Correct |
22 ms |
1484 KB |
Output is correct |
56 |
Correct |
20 ms |
1484 KB |
Output is correct |
57 |
Correct |
21 ms |
1440 KB |
Output is correct |
58 |
Correct |
21 ms |
1380 KB |
Output is correct |
59 |
Correct |
20 ms |
1384 KB |
Output is correct |
60 |
Correct |
26 ms |
1476 KB |
Output is correct |
61 |
Correct |
125 ms |
5856 KB |
Output is correct |
62 |
Correct |
175 ms |
15924 KB |
Output is correct |
63 |
Correct |
867 ms |
110520 KB |
Output is correct |
64 |
Correct |
1170 ms |
110656 KB |
Output is correct |
65 |
Correct |
1293 ms |
110772 KB |
Output is correct |
66 |
Correct |
1510 ms |
111548 KB |
Output is correct |
67 |
Correct |
1978 ms |
115424 KB |
Output is correct |
68 |
Correct |
1016 ms |
110460 KB |
Output is correct |
69 |
Correct |
1678 ms |
114840 KB |
Output is correct |
70 |
Correct |
963 ms |
110360 KB |
Output is correct |
71 |
Correct |
1152 ms |
110520 KB |
Output is correct |
72 |
Correct |
1430 ms |
110780 KB |
Output is correct |
73 |
Correct |
1694 ms |
111340 KB |
Output is correct |
74 |
Correct |
975 ms |
110652 KB |
Output is correct |
75 |
Correct |
1070 ms |
110592 KB |
Output is correct |