#include <cstdio>
#include <cassert>
#include <vector>
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define all(x) x.begin(), x.end()
#define rll(x) x.rbegin(), x.rend()
#define comp(x) x.erase(unique(all(x)), x.end())
ll n;
struct lazyseg{ //min query
vector<ll> tree, lazy;
lazyseg(): tree(808080), lazy(808080){}
void prop(ll node, ll s, ll e){
if(!lazy[node])return;
tree[node] += lazy[node];
if(s^e)lazy[node<<1] += lazy[node], lazy[node<<1|1] += lazy[node]; lazy[node]=0;
}
void init(ll node, ll s, ll e, vector<int> &v){
if(s==e){
tree[node] = v[s];
}
else{
ll mid = s+e>>1;
init(node<<1,s,mid,v); init(node<<1|1,mid+1,e,v);
tree[node] = min(tree[node<<1], tree[node<<1|1]);
}
}
void upd(ll node, ll s, ll e, ll l, ll r, ll x){
prop(node,s,e);
if(e<l or r<s)return;
if(l<=s and e<=r){
lazy[node] = x;
prop(node,s,e); return;
}
ll mid = s+e>>1;
upd(node<<1,s,mid,l,r,x); upd(node<<1|1,mid+1,e,l,r,x);
tree[node] = min(tree[node<<1], tree[node<<1|1]);
}
ll query(ll node, ll s, ll e, ll l, ll r){
prop(node,s,e);
if(e<l or r<s)return 1e18;
if(l<=s and e<=r)return tree[node];
ll mid = s+e>>1; return min(query(node<<1,s,mid,l,r), query(node<<1|1,mid+1,e,l,r));
}
ll find_pos(ll idx){ //idx 이상
if(query(1,0,n-1,idx,n-1) > 0)return -1;
ll l = idx-1, r = n;
while(l+1 < r){
ll mid = l+r>>1;
if(query(1,0,n-1,idx,mid) == 0)r = mid; else l = mid;
}
return r;
}
} seg;
struct segtree{
vector<pair<ll,ll>> tree;
segtree(): tree(1616161){}
void upd(ll node, ll s, ll e, ll i, ll d){
if(e<i or i<s)return;
if(s==e)tree[node] = {d+1,s};
else{
ll mid = s+e>>1;
upd(node<<1,s,mid,i,d); upd(node<<1|1,mid+1,e,i,d);
tree[node] = max(tree[node<<1],tree[node<<1|1]);
}
}
pair<ll,ll> query(ll node, ll s, ll e, ll l, ll r){
if(e<l or r<s)return {-1,0};
if(l<=s and e<=r)return tree[node];
ll mid = s+e>>1; return max(query(node<<1,s,mid,l,r), query(node<<1|1,mid+1,e,l,r));
}
} maxseg;
ll L[404040][22], R[404040][22];
vector<ll> v;
bool chk(ll l, ll r){
l = (l+n)%n;
r = (r+n)%n;
if(l<=r)return seg.query(1,0,n-1,l,r) > 0;
return min(seg.query(1,0,n-1,l,n-1), seg.query(1,0,n-1,0,r)) > 0;
}
void subs(ll l, ll r){
l = (l+n)%n;
r = (r+n)%n;
if(l<=r)seg.upd(1,0,n-1,l,r,-1);
else{
seg.upd(1,0,n-1,l,n-1,-1);
seg.upd(1,0,n-1,0,r,-1);
}
}
ll K;
void init(int k, std::vector<int> r) {
n = r.size(); k--;
K=k;
seg.init(1,0,n-1,r);
vector<ll> cand;
for(int i = 0 ; i < n ; i++)if(r[i] == 0){
if(chk(i-k, i-1))cand.push_back(i);
}
v.resize(n*2);
for(int t = 0 ; cand.size() ; t++){
vector<ll> nxt;
for(auto i : cand){
v[i] = v[n+i] = n-t, subs(i-k, i-1);
seg.upd(1,0,n-1,i,i,1e9);
}
for(auto i : cand){
ll l = (i-k+n)%n, r = (i-1+n)%n;
ll p = -1;
if(l<=r){
p = seg.find_pos(l);
if((l <= p and p <= r) and chk(p-k,p-1))nxt.push_back(p);
}
else{
p = seg.find_pos(l);
if(p==-1)p = seg.find_pos(0);
if(((l <= p and p <= n) or (0 <= p and p <= r)) and chk(p-k,p-1))nxt.push_back(p);
}
l = (i+1)%n, r = (i+k)%n;
p = -1;
if(l<=r){
p = seg.find_pos(l);
if((l <= p and p <= r) and chk(p-k,p-1))nxt.push_back(p);
}
else{
p = seg.find_pos(l);
if(p==-1)p = seg.find_pos(0);
if(((l <= p and p <= n) or (0 <= p and p <= r)) and chk(p-k,p-1))nxt.push_back(p);
}
}
cand.clear(); sort(all(nxt)); comp(nxt); for(auto i : nxt)cand.push_back(i);
}
vector<pair<ll,ll>> e;
for(int i = 0 ; i < n ; i++)e.push_back({v[i],i});
sort(all(e));
memset(L,-1,sizeof(L));
memset(R,-1,sizeof(R));
for(auto [d,x] : e){
if(x-k >= 0){
auto [_,t] = maxseg.query(1,0,n*2-1, x-k, x-1);
if(_ > 0)L[x][0] = t;
}
if(x+k < n*2){
auto [_,t] = maxseg.query(1,0,n*2-1, x+1, x+k);
if(_ > 0)R[x][0] = t;
}
if(x+n-k >= 0){
auto [_,t] = maxseg.query(1,0,n*2-1, x+n-k, x+n-1);
if(_ > 0)L[n+x][0] = t;
}
if(x+n+k < n*2){
auto [_,t] = maxseg.query(1,0,n*2-1, x+n+1, x+n+k);
if(_ > 0)R[n+x][0] = t;
}
maxseg.upd(1,0,n*2-1,x,d);
maxseg.upd(1,0,n*2-1,x+n,d);
}
for(int j = 1 ; j <= 20 ; j++){
for(int i = 0 ; i < n*2 ; i++){
if(L[i][j-1] < 0)L[i][j] = -1;
else if(L[L[i][j-1]][j-1] < 0)L[i][j] = -1;
else L[i][j] = L[L[i][j-1]][j-1];
if(R[i][j-1] < 0)R[i][j] = -1;
else if(R[R[i][j-1]][j-1] < 0)R[i][j] = -1;
else R[i][j] = R[R[i][j-1]][j-1];
}
}
}
int compare_plants(int x, int y) {
if(x < y){
ll nx = x; //x < y
for(int i = 20 ; i >= 0 ; i--){
if(R[nx][i] < 0 or R[nx][i] + K >= y)continue;
nx = R[nx][i];
}
if(nx + K < y)nx = R[nx][0];
if(nx>=0 and nx + K >= y and v[nx] > v[y])return 1;
nx = n+x;
for(int i = 20 ; i >= 0 ; i--){
if(L[nx][i] < 0 or L[nx][i] - K <= y)continue;
nx = L[nx][i];
}
if(nx - K > y)nx = L[nx][0];
if(nx>=0 and nx - K <= y and v[nx] > v[y])return 1;
swap(x,y); //x > y
nx = x;
for(int i = 20 ; i >= 0 ; i--){
if(L[nx][i] < 0 or L[nx][i] - K <= y)continue;
nx = L[nx][i];
}
if(nx - K > y)nx = L[nx][0];
if(nx>=0 and nx - K <= y and v[nx] > v[y])return -1;
nx = x;
for(int i = 20 ; i >= 0 ; i--){
if(R[nx][i] < 0 or R[nx][i] + K >= n+y)continue;
nx = R[nx][i];
}
if(nx + K < n+y)nx = R[nx][0];
if(nx>=0 and nx + K >= n+y and v[nx] > v[n+y])return -1;
}
else{
ll nx = x; //x > y
for(int i = 20 ; i >= 0 ; i--){
if(L[nx][i] < 0 or L[nx][i] - K <= y)continue;
nx = L[nx][i];
}
if(nx - K > y)nx = L[nx][0];
if(nx>=0 and nx - K <= y and v[nx] > v[y])return 1;
nx = x;
for(int i = 20 ; i >= 0 ; i--){
if(R[nx][i] < 0 or R[nx][i] + K >= n+y)continue;
nx = R[nx][i];
}
if(nx + K < n+y)nx = R[nx][0];
if(nx>=0 and nx + K >= n+y and v[nx] > v[n+y])return 1;
swap(x,y); //x < y
nx = x;
for(int i = 20 ; i >= 0 ; i--){
if(R[nx][i] < 0 or R[nx][i] + K >= y)continue;
nx = R[nx][i];
}
if(nx + K < y)nx = R[nx][0];
if(nx>=0 and nx + K >= y and v[nx] > v[y])return -1;
nx = n+x;
for(int i = 20 ; i >= 0 ; i--){
if(L[nx][i] < 0 or L[nx][i] - K <= y)continue;
nx = L[nx][i];
}
if(nx - K > y)nx = L[nx][0];
if(nx>=0 and nx - K <= y and v[nx] > v[y])return -1;
}
return 0;
}
Compilation message
plants.cpp: In member function 'void lazyseg::prop(ll, ll, ll)':
plants.cpp:17:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
17 | if(s^e)lazy[node<<1] += lazy[node], lazy[node<<1|1] += lazy[node]; lazy[node]=0;
| ^~
plants.cpp:17:76: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
17 | if(s^e)lazy[node<<1] += lazy[node], lazy[node<<1|1] += lazy[node]; lazy[node]=0;
| ^~~~
plants.cpp: In member function 'void lazyseg::init(ll, ll, ll, std::vector<int>&)':
plants.cpp:24:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
24 | ll mid = s+e>>1;
| ~^~
plants.cpp: In member function 'void lazyseg::upd(ll, ll, ll, ll, ll, ll)':
plants.cpp:36:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
36 | ll mid = s+e>>1;
| ~^~
plants.cpp: In member function 'll lazyseg::query(ll, ll, ll, ll, ll)':
plants.cpp:44:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
44 | ll mid = s+e>>1; return min(query(node<<1,s,mid,l,r), query(node<<1|1,mid+1,e,l,r));
| ~^~
plants.cpp: In member function 'll lazyseg::find_pos(ll)':
plants.cpp:50:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
50 | ll mid = l+r>>1;
| ~^~
plants.cpp: In member function 'void segtree::upd(ll, ll, ll, ll, ll)':
plants.cpp:63:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
63 | ll mid = s+e>>1;
| ~^~
plants.cpp: In member function 'std::pair<long long int, long long int> segtree::query(ll, ll, ll, ll, ll)':
plants.cpp:71:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
71 | ll mid = s+e>>1; return max(query(node<<1,s,mid,l,r), query(node<<1|1,mid+1,e,l,r));
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
177488 KB |
Output is correct |
2 |
Correct |
18 ms |
177500 KB |
Output is correct |
3 |
Correct |
18 ms |
177464 KB |
Output is correct |
4 |
Correct |
18 ms |
177448 KB |
Output is correct |
5 |
Correct |
20 ms |
177500 KB |
Output is correct |
6 |
Correct |
54 ms |
180340 KB |
Output is correct |
7 |
Correct |
158 ms |
180804 KB |
Output is correct |
8 |
Correct |
1266 ms |
188708 KB |
Output is correct |
9 |
Correct |
1361 ms |
187696 KB |
Output is correct |
10 |
Correct |
1404 ms |
188404 KB |
Output is correct |
11 |
Correct |
1232 ms |
187836 KB |
Output is correct |
12 |
Correct |
1110 ms |
188348 KB |
Output is correct |
13 |
Correct |
1093 ms |
187828 KB |
Output is correct |
14 |
Correct |
845 ms |
188348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
177488 KB |
Output is correct |
2 |
Correct |
19 ms |
177440 KB |
Output is correct |
3 |
Correct |
19 ms |
177500 KB |
Output is correct |
4 |
Correct |
19 ms |
177500 KB |
Output is correct |
5 |
Correct |
20 ms |
177496 KB |
Output is correct |
6 |
Correct |
23 ms |
177500 KB |
Output is correct |
7 |
Correct |
78 ms |
180480 KB |
Output is correct |
8 |
Correct |
20 ms |
177500 KB |
Output is correct |
9 |
Correct |
22 ms |
177492 KB |
Output is correct |
10 |
Correct |
74 ms |
180588 KB |
Output is correct |
11 |
Correct |
68 ms |
180480 KB |
Output is correct |
12 |
Correct |
82 ms |
180736 KB |
Output is correct |
13 |
Correct |
78 ms |
180552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
177488 KB |
Output is correct |
2 |
Correct |
19 ms |
177440 KB |
Output is correct |
3 |
Correct |
19 ms |
177500 KB |
Output is correct |
4 |
Correct |
19 ms |
177500 KB |
Output is correct |
5 |
Correct |
20 ms |
177496 KB |
Output is correct |
6 |
Correct |
23 ms |
177500 KB |
Output is correct |
7 |
Correct |
78 ms |
180480 KB |
Output is correct |
8 |
Correct |
20 ms |
177500 KB |
Output is correct |
9 |
Correct |
22 ms |
177492 KB |
Output is correct |
10 |
Correct |
74 ms |
180588 KB |
Output is correct |
11 |
Correct |
68 ms |
180480 KB |
Output is correct |
12 |
Correct |
82 ms |
180736 KB |
Output is correct |
13 |
Correct |
78 ms |
180552 KB |
Output is correct |
14 |
Correct |
162 ms |
181212 KB |
Output is correct |
15 |
Correct |
1523 ms |
188148 KB |
Output is correct |
16 |
Correct |
162 ms |
180752 KB |
Output is correct |
17 |
Correct |
1519 ms |
188660 KB |
Output is correct |
18 |
Correct |
1237 ms |
188092 KB |
Output is correct |
19 |
Correct |
954 ms |
188664 KB |
Output is correct |
20 |
Correct |
1365 ms |
187828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
177496 KB |
Output is correct |
2 |
Correct |
19 ms |
177500 KB |
Output is correct |
3 |
Correct |
65 ms |
180284 KB |
Output is correct |
4 |
Correct |
1221 ms |
187632 KB |
Output is correct |
5 |
Correct |
1495 ms |
188344 KB |
Output is correct |
6 |
Correct |
1710 ms |
191432 KB |
Output is correct |
7 |
Correct |
1768 ms |
191124 KB |
Output is correct |
8 |
Correct |
1663 ms |
191316 KB |
Output is correct |
9 |
Correct |
1362 ms |
191268 KB |
Output is correct |
10 |
Correct |
1315 ms |
190908 KB |
Output is correct |
11 |
Correct |
1145 ms |
191168 KB |
Output is correct |
12 |
Correct |
953 ms |
190904 KB |
Output is correct |
13 |
Correct |
1271 ms |
191420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
177496 KB |
Output is correct |
2 |
Correct |
19 ms |
177500 KB |
Output is correct |
3 |
Correct |
18 ms |
177460 KB |
Output is correct |
4 |
Correct |
19 ms |
177456 KB |
Output is correct |
5 |
Correct |
19 ms |
177440 KB |
Output is correct |
6 |
Correct |
20 ms |
177512 KB |
Output is correct |
7 |
Correct |
33 ms |
178000 KB |
Output is correct |
8 |
Correct |
28 ms |
178012 KB |
Output is correct |
9 |
Correct |
29 ms |
178260 KB |
Output is correct |
10 |
Correct |
27 ms |
178332 KB |
Output is correct |
11 |
Correct |
29 ms |
178324 KB |
Output is correct |
12 |
Correct |
29 ms |
178512 KB |
Output is correct |
13 |
Correct |
27 ms |
178328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
177496 KB |
Output is correct |
2 |
Correct |
19 ms |
177528 KB |
Output is correct |
3 |
Correct |
19 ms |
177500 KB |
Output is correct |
4 |
Correct |
19 ms |
177544 KB |
Output is correct |
5 |
Correct |
24 ms |
177500 KB |
Output is correct |
6 |
Correct |
1576 ms |
187672 KB |
Output is correct |
7 |
Correct |
1714 ms |
187632 KB |
Output is correct |
8 |
Correct |
1743 ms |
187636 KB |
Output is correct |
9 |
Correct |
1664 ms |
188404 KB |
Output is correct |
10 |
Correct |
1424 ms |
188096 KB |
Output is correct |
11 |
Correct |
1534 ms |
187836 KB |
Output is correct |
12 |
Correct |
1151 ms |
188344 KB |
Output is correct |
13 |
Correct |
1490 ms |
187892 KB |
Output is correct |
14 |
Correct |
1611 ms |
188664 KB |
Output is correct |
15 |
Correct |
1714 ms |
190252 KB |
Output is correct |
16 |
Correct |
1250 ms |
190040 KB |
Output is correct |
17 |
Correct |
1478 ms |
190136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
177488 KB |
Output is correct |
2 |
Correct |
18 ms |
177500 KB |
Output is correct |
3 |
Correct |
18 ms |
177464 KB |
Output is correct |
4 |
Correct |
18 ms |
177448 KB |
Output is correct |
5 |
Correct |
20 ms |
177500 KB |
Output is correct |
6 |
Correct |
54 ms |
180340 KB |
Output is correct |
7 |
Correct |
158 ms |
180804 KB |
Output is correct |
8 |
Correct |
1266 ms |
188708 KB |
Output is correct |
9 |
Correct |
1361 ms |
187696 KB |
Output is correct |
10 |
Correct |
1404 ms |
188404 KB |
Output is correct |
11 |
Correct |
1232 ms |
187836 KB |
Output is correct |
12 |
Correct |
1110 ms |
188348 KB |
Output is correct |
13 |
Correct |
1093 ms |
187828 KB |
Output is correct |
14 |
Correct |
845 ms |
188348 KB |
Output is correct |
15 |
Correct |
19 ms |
177488 KB |
Output is correct |
16 |
Correct |
19 ms |
177440 KB |
Output is correct |
17 |
Correct |
19 ms |
177500 KB |
Output is correct |
18 |
Correct |
19 ms |
177500 KB |
Output is correct |
19 |
Correct |
20 ms |
177496 KB |
Output is correct |
20 |
Correct |
23 ms |
177500 KB |
Output is correct |
21 |
Correct |
78 ms |
180480 KB |
Output is correct |
22 |
Correct |
20 ms |
177500 KB |
Output is correct |
23 |
Correct |
22 ms |
177492 KB |
Output is correct |
24 |
Correct |
74 ms |
180588 KB |
Output is correct |
25 |
Correct |
68 ms |
180480 KB |
Output is correct |
26 |
Correct |
82 ms |
180736 KB |
Output is correct |
27 |
Correct |
78 ms |
180552 KB |
Output is correct |
28 |
Correct |
162 ms |
181212 KB |
Output is correct |
29 |
Correct |
1523 ms |
188148 KB |
Output is correct |
30 |
Correct |
162 ms |
180752 KB |
Output is correct |
31 |
Correct |
1519 ms |
188660 KB |
Output is correct |
32 |
Correct |
1237 ms |
188092 KB |
Output is correct |
33 |
Correct |
954 ms |
188664 KB |
Output is correct |
34 |
Correct |
1365 ms |
187828 KB |
Output is correct |
35 |
Correct |
18 ms |
177496 KB |
Output is correct |
36 |
Correct |
19 ms |
177500 KB |
Output is correct |
37 |
Correct |
65 ms |
180284 KB |
Output is correct |
38 |
Correct |
1221 ms |
187632 KB |
Output is correct |
39 |
Correct |
1495 ms |
188344 KB |
Output is correct |
40 |
Correct |
1710 ms |
191432 KB |
Output is correct |
41 |
Correct |
1768 ms |
191124 KB |
Output is correct |
42 |
Correct |
1663 ms |
191316 KB |
Output is correct |
43 |
Correct |
1362 ms |
191268 KB |
Output is correct |
44 |
Correct |
1315 ms |
190908 KB |
Output is correct |
45 |
Correct |
1145 ms |
191168 KB |
Output is correct |
46 |
Correct |
953 ms |
190904 KB |
Output is correct |
47 |
Correct |
1271 ms |
191420 KB |
Output is correct |
48 |
Correct |
20 ms |
177496 KB |
Output is correct |
49 |
Correct |
19 ms |
177500 KB |
Output is correct |
50 |
Correct |
18 ms |
177460 KB |
Output is correct |
51 |
Correct |
19 ms |
177456 KB |
Output is correct |
52 |
Correct |
19 ms |
177440 KB |
Output is correct |
53 |
Correct |
20 ms |
177512 KB |
Output is correct |
54 |
Correct |
33 ms |
178000 KB |
Output is correct |
55 |
Correct |
28 ms |
178012 KB |
Output is correct |
56 |
Correct |
29 ms |
178260 KB |
Output is correct |
57 |
Correct |
27 ms |
178332 KB |
Output is correct |
58 |
Correct |
29 ms |
178324 KB |
Output is correct |
59 |
Correct |
29 ms |
178512 KB |
Output is correct |
60 |
Correct |
27 ms |
178328 KB |
Output is correct |
61 |
Correct |
75 ms |
182072 KB |
Output is correct |
62 |
Correct |
167 ms |
182904 KB |
Output is correct |
63 |
Correct |
1362 ms |
191368 KB |
Output is correct |
64 |
Correct |
1601 ms |
190772 KB |
Output is correct |
65 |
Correct |
1796 ms |
191160 KB |
Output is correct |
66 |
Correct |
1812 ms |
191376 KB |
Output is correct |
67 |
Correct |
1639 ms |
191420 KB |
Output is correct |
68 |
Correct |
1410 ms |
190756 KB |
Output is correct |
69 |
Correct |
1502 ms |
191420 KB |
Output is correct |
70 |
Correct |
1324 ms |
190648 KB |
Output is correct |
71 |
Correct |
1511 ms |
190720 KB |
Output is correct |
72 |
Correct |
1691 ms |
190920 KB |
Output is correct |
73 |
Correct |
1733 ms |
191120 KB |
Output is correct |
74 |
Correct |
1440 ms |
190792 KB |
Output is correct |
75 |
Correct |
1424 ms |
190908 KB |
Output is correct |