#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<bool> vb;
typedef vector<vb> vvb;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,k,n) for(ll i=(ll)(k);i<(ll)(n);i++)
#define pb emplace_back
#define fi first
#define se second
template<class T> void out(T a){cout<<a<<endl;}
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<endl;}
const ll inf=1001001001001001001;
struct segtree{
vp seg;vi lazy;
ll n=1;
segtree(vp v){
while(n<v.size())n*=2;
seg=vp(n*2,P(2*inf,0));lazy=vi(n*2);
rep(i,v.size())seg[i+n]=v[i];
for(ll i=n-1;i>0;i--)seg[i]=min(seg[i*2],seg[i*2+1]);
}
void eval(ll k,ll l,ll r){
seg[k].fi+=lazy[k];
if(r-l>1)rep(j,2)lazy[k*2+j]+=lazy[k];
lazy[k]=0;
}
P get(ll a,ll b,ll k=1,ll l=0,ll r=-1){
if(r==-1)r=n;
eval(k,l,r);
if(r<=a||b<=l)return P(2*inf,0);
if(a<=l&&r<=b)return seg[k];
return min(get(a,b,k*2,l,(l+r)/2),get(a,b,k*2+1,(l+r)/2,r));
}
void add(ll x,ll a,ll b,ll k=1,ll l=0,ll r=-1){
if(r==-1)r=n;
eval(k,l,r);
if(r<=a||b<=l)return;
if(a<=l&&r<=b){
lazy[k]+=x;eval(k,l,r);return;
}
add(x,a,b,k*2,l,(l+r)/2);
add(x,a,b,k*2+1,(l+r)/2,r);
seg[k]=min(seg[k*2],seg[k*2+1]);
}
};
vi num,al,rv;
vvi ma,mi;
void init(int k,vector<int> v){
ll n=v.size();
vp tmp1(n),tmp2(n);
rep(i,n)tmp1[i]=P(v[i],i);
rep(i,n)tmp2[i]=P(inf,i);
segtree seg1(tmp1),seg2(tmp2);
auto dif=[&](ll x){
return (x%n+n)%n;
};
ll cnt=0;
num=vi(n);
while(true){
while(true){
auto tmp=seg1.get(0,n);
if(tmp.fi>0)break;
ll i=tmp.se;
seg1.add(inf,i,i+1);
seg2.add(-inf,i,i+1);
if(i+k<=n)seg2.add(1,i+1,i+k);
else{
seg2.add(1,i+1,n);
seg2.add(1,0,i+k-n);
}
}
auto tmp=seg2.get(0,n);
if(tmp.fi>0)break;
ll i=tmp.se;
seg2.add(inf,i,i+1);
num[i]=cnt++;
if(i-k+1>=0)seg1.add(-1,i-k+1,i);
else{
seg1.add(-1,0,i);
seg1.add(-1,i-k+1+n,n);
}
if(i+k<=n)seg2.add(-1,i+1,i+k);
else{
seg2.add(-1,i+1,n);
seg2.add(-1,0,i+k-n);
}
}
al=vi(n*3);
rep(i,n)al[num[i]*3]=i;
rep(i,n)al[num[i]*3+2]=i+n;
rep(i,n)al[num[i]*3+1]=i+n*2;
rv=vi(n*3);
rep(i,n*3)rv[al[i]]=i;
ma=vvi(20,vi(n*3,-1));
mi=vvi(20,vi(n*3,-1));
rep(i,n*3)ma[0][i]=mi[0][i]=i;
{
set<ll> S;
for(int i=n*3-1;i>=0;i--){
if(i+k<n*3)S.erase(rv[i+k]);
auto itr=S.lower_bound(rv[i]);
if(itr!=S.end())ma[0][rv[i]]=*itr;
S.insert(rv[i]);
}
}
{
set<ll> S;
rep(i,n*3){
if(i-k>=0)S.erase(rv[i-k]);
auto itr=S.lower_bound(rv[i]);
if(itr!=S.end())mi[0][rv[i]]=*itr;
S.insert(rv[i]);
}
}
// outv(ma[0]);outv(mi[0]);
rep(j,19)rep(i,n*3)ma[j+1][i]=ma[j][ma[j][i]];
rep(j,19)rep(i,n*3)mi[j+1][i]=mi[j][mi[j][i]];
// outv(num);
// outv(al);
}
int compare_plants(int x,int y){
int f=-1;
if(num[x]<num[y]){
swap(x,y);
f=1;
}
ll i=rv[y+num.size()],j=rv[x+num.size()];
// cout<<x<<' '<<y<<' '<<i<<' '<<j<<endl;
{
ll cur=i;
for(ll t=19;t>=0;t--)if(ma[t][cur]<=j)cur=ma[t][cur];
ll tmp=x+num.size();
if(x<y)tmp+=num.size();
if(al[cur]>=tmp)return f;
}
{
ll cur=i;
for(ll t=19;t>=0;t--)if(mi[t][cur]<=j)cur=mi[t][cur];
ll tmp=x+num.size();
if(x>y)tmp-=num.size();
if(al[cur]<=tmp)return f;
}
return 0;
}
Compilation message
plants.cpp: In constructor 'segtree::segtree(vp)':
plants.cpp:24:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | while(n<v.size())n*=2;
| ~^~~~~~~~~
plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:61:10: warning: variable 'dif' set but not used [-Wunused-but-set-variable]
61 | auto dif=[&](ll x){
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
58 ms |
3028 KB |
Output is correct |
7 |
Correct |
432 ms |
26956 KB |
Output is correct |
8 |
Correct |
1203 ms |
241580 KB |
Output is correct |
9 |
Correct |
1198 ms |
241572 KB |
Output is correct |
10 |
Correct |
1131 ms |
241524 KB |
Output is correct |
11 |
Correct |
1158 ms |
241580 KB |
Output is correct |
12 |
Correct |
1097 ms |
241540 KB |
Output is correct |
13 |
Correct |
959 ms |
241576 KB |
Output is correct |
14 |
Correct |
1330 ms |
241612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
7 ms |
1492 KB |
Output is correct |
7 |
Correct |
87 ms |
8680 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
6 ms |
1492 KB |
Output is correct |
10 |
Correct |
91 ms |
8696 KB |
Output is correct |
11 |
Correct |
102 ms |
8692 KB |
Output is correct |
12 |
Correct |
134 ms |
8692 KB |
Output is correct |
13 |
Correct |
88 ms |
8784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
7 ms |
1492 KB |
Output is correct |
7 |
Correct |
87 ms |
8680 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
6 ms |
1492 KB |
Output is correct |
10 |
Correct |
91 ms |
8696 KB |
Output is correct |
11 |
Correct |
102 ms |
8692 KB |
Output is correct |
12 |
Correct |
134 ms |
8692 KB |
Output is correct |
13 |
Correct |
88 ms |
8784 KB |
Output is correct |
14 |
Correct |
228 ms |
26968 KB |
Output is correct |
15 |
Correct |
2356 ms |
239756 KB |
Output is correct |
16 |
Correct |
230 ms |
26972 KB |
Output is correct |
17 |
Correct |
2297 ms |
239772 KB |
Output is correct |
18 |
Correct |
1551 ms |
238708 KB |
Output is correct |
19 |
Correct |
1732 ms |
238548 KB |
Output is correct |
20 |
Correct |
2124 ms |
243288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
109 ms |
5360 KB |
Output is correct |
4 |
Correct |
1210 ms |
238660 KB |
Output is correct |
5 |
Correct |
1277 ms |
238672 KB |
Output is correct |
6 |
Correct |
1460 ms |
238660 KB |
Output is correct |
7 |
Correct |
1782 ms |
238616 KB |
Output is correct |
8 |
Correct |
2175 ms |
238724 KB |
Output is correct |
9 |
Correct |
1127 ms |
238604 KB |
Output is correct |
10 |
Correct |
1190 ms |
238660 KB |
Output is correct |
11 |
Correct |
934 ms |
238748 KB |
Output is correct |
12 |
Correct |
1428 ms |
238656 KB |
Output is correct |
13 |
Correct |
1517 ms |
238748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
18 ms |
1332 KB |
Output is correct |
8 |
Correct |
15 ms |
1416 KB |
Output is correct |
9 |
Correct |
16 ms |
1364 KB |
Output is correct |
10 |
Correct |
20 ms |
1364 KB |
Output is correct |
11 |
Correct |
17 ms |
1340 KB |
Output is correct |
12 |
Correct |
16 ms |
1332 KB |
Output is correct |
13 |
Correct |
19 ms |
1336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
5 ms |
1368 KB |
Output is correct |
6 |
Correct |
1292 ms |
240832 KB |
Output is correct |
7 |
Correct |
1442 ms |
241080 KB |
Output is correct |
8 |
Correct |
1662 ms |
241232 KB |
Output is correct |
9 |
Correct |
1987 ms |
241420 KB |
Output is correct |
10 |
Correct |
1347 ms |
240888 KB |
Output is correct |
11 |
Correct |
1571 ms |
241428 KB |
Output is correct |
12 |
Correct |
1059 ms |
240712 KB |
Output is correct |
13 |
Correct |
1252 ms |
240880 KB |
Output is correct |
14 |
Correct |
1422 ms |
241056 KB |
Output is correct |
15 |
Correct |
1703 ms |
241268 KB |
Output is correct |
16 |
Correct |
1020 ms |
240816 KB |
Output is correct |
17 |
Correct |
1340 ms |
240920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
58 ms |
3028 KB |
Output is correct |
7 |
Correct |
432 ms |
26956 KB |
Output is correct |
8 |
Correct |
1203 ms |
241580 KB |
Output is correct |
9 |
Correct |
1198 ms |
241572 KB |
Output is correct |
10 |
Correct |
1131 ms |
241524 KB |
Output is correct |
11 |
Correct |
1158 ms |
241580 KB |
Output is correct |
12 |
Correct |
1097 ms |
241540 KB |
Output is correct |
13 |
Correct |
959 ms |
241576 KB |
Output is correct |
14 |
Correct |
1330 ms |
241612 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
7 ms |
1492 KB |
Output is correct |
21 |
Correct |
87 ms |
8680 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
6 ms |
1492 KB |
Output is correct |
24 |
Correct |
91 ms |
8696 KB |
Output is correct |
25 |
Correct |
102 ms |
8692 KB |
Output is correct |
26 |
Correct |
134 ms |
8692 KB |
Output is correct |
27 |
Correct |
88 ms |
8784 KB |
Output is correct |
28 |
Correct |
228 ms |
26968 KB |
Output is correct |
29 |
Correct |
2356 ms |
239756 KB |
Output is correct |
30 |
Correct |
230 ms |
26972 KB |
Output is correct |
31 |
Correct |
2297 ms |
239772 KB |
Output is correct |
32 |
Correct |
1551 ms |
238708 KB |
Output is correct |
33 |
Correct |
1732 ms |
238548 KB |
Output is correct |
34 |
Correct |
2124 ms |
243288 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
109 ms |
5360 KB |
Output is correct |
38 |
Correct |
1210 ms |
238660 KB |
Output is correct |
39 |
Correct |
1277 ms |
238672 KB |
Output is correct |
40 |
Correct |
1460 ms |
238660 KB |
Output is correct |
41 |
Correct |
1782 ms |
238616 KB |
Output is correct |
42 |
Correct |
2175 ms |
238724 KB |
Output is correct |
43 |
Correct |
1127 ms |
238604 KB |
Output is correct |
44 |
Correct |
1190 ms |
238660 KB |
Output is correct |
45 |
Correct |
934 ms |
238748 KB |
Output is correct |
46 |
Correct |
1428 ms |
238656 KB |
Output is correct |
47 |
Correct |
1517 ms |
238748 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
1 ms |
304 KB |
Output is correct |
53 |
Correct |
2 ms |
468 KB |
Output is correct |
54 |
Correct |
18 ms |
1332 KB |
Output is correct |
55 |
Correct |
15 ms |
1416 KB |
Output is correct |
56 |
Correct |
16 ms |
1364 KB |
Output is correct |
57 |
Correct |
20 ms |
1364 KB |
Output is correct |
58 |
Correct |
17 ms |
1340 KB |
Output is correct |
59 |
Correct |
16 ms |
1332 KB |
Output is correct |
60 |
Correct |
19 ms |
1336 KB |
Output is correct |
61 |
Correct |
116 ms |
6996 KB |
Output is correct |
62 |
Correct |
316 ms |
29016 KB |
Output is correct |
63 |
Correct |
1440 ms |
241656 KB |
Output is correct |
64 |
Correct |
1297 ms |
241740 KB |
Output is correct |
65 |
Correct |
1500 ms |
241900 KB |
Output is correct |
66 |
Correct |
1699 ms |
242140 KB |
Output is correct |
67 |
Correct |
2064 ms |
242332 KB |
Output is correct |
68 |
Correct |
1403 ms |
241640 KB |
Output is correct |
69 |
Correct |
1618 ms |
242376 KB |
Output is correct |
70 |
Correct |
1209 ms |
241532 KB |
Output is correct |
71 |
Correct |
1294 ms |
241748 KB |
Output is correct |
72 |
Correct |
1484 ms |
241996 KB |
Output is correct |
73 |
Correct |
1696 ms |
242096 KB |
Output is correct |
74 |
Correct |
1364 ms |
241736 KB |
Output is correct |
75 |
Correct |
1356 ms |
241888 KB |
Output is correct |