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<bits/stdc++.h>
using namespace std;
int K, i, j, N, R[200010], X[200010], lazy[800010];
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));
}
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++;
}
return;
}
int compare_plants(int x, int y)
{
x++;y++;
if(X[x]<X[y])return 1;
return -1;
}
Compilation message (stderr)
plants.cpp: In function 'void init(int, int, int)':
plants.cpp:31:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
31 | int m=s+e>>1;
| ~^~
plants.cpp: In function 'void update(int, int, int, int, int, int)':
plants.cpp:44:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
44 | busy(id);int m=s+e>>1;
| ~^~
plants.cpp: In function 'node get(int, int, int, int, int)':
plants.cpp:52:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
52 | busy(id);int m=s+e>>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... |