#include "bits/stdc++.h"
//#include "plants.h"
using namespace std;
#define int long long
pair<int,int> seg[800001];
int lazy[800001];
vector<int> arr;
void build(int p,int l,int r){
if(l==r){
seg[p] = {arr[l],l};
return ;
}
int md = (l+r)/2;
build(p*2,l,md);
build(p*2+1,md+1,r);
seg[p] = min(seg[p*2],seg[p*2+1]);
}
void prop(int p,int l,int r){
seg[p].first+=lazy[p];
if(l!=r){
lazy[p*2]+=lazy[p];
lazy[p*2+1]+=lazy[p];
}
lazy[p] = 0;
}
void update(int p,int l,int r,int lq,int rq,int val){
prop(p,l,r);
if(l>=lq&&r<=rq){
lazy[p]+=val;
prop(p,l,r);
return ;
}
if(r<lq||l>rq)return ;
int md = (l+r)/2;
update(p*2,l,md,lq,rq,val);
update(p*2+1,md+1,r,lq,rq,val);
seg[p] = min(seg[p*2],seg[p*2+1]);
}
pair<int,int> query(int p,int l,int r,int lq,int rq){
prop(p,l,r);
if(l>=lq&&r<=rq)return seg[p];
if(r<lq||l>rq)return {1e9,0};
int md = (l+r)/2;
return min(query(p*2,l,md,lq,rq),query(p*2+1,md+1,r,lq,rq));
}
int idx = 0,k,n;
vector<int> h;
int bad(int x){
int l = x-k+1 , r = x-1;
if(r==-1){
l+=n;r+=n;
pair<int,int> val = query(1,0,n-1,l,r);
if(val.first==0)return val.second;
return -1;
}else{
pair<int,int> val = query(1,0,n-1,max(0ll,l),r);
if(l<0){
l+=n;
val = min(val,query(1,0,n-1,l,n-1));
}
if(val.first==0)return val.second;
return -1;
}
}
void fix(){
pair<int,int> x = query(1,0,n-1,0,n-1);
if(x.first<0){
update(1,0,n-1,x.second,x.second,1e9);
}else return ;
fix();
}
void buld(int x){
while(bad(x)!=-1){
buld(bad(x));
}
int l = x-k+1 , r = x;
update(1,0,n-1,max(0ll,l),r,-1);
if(l<0){
l+=n;
update(1,0,n-1,l,n-1,-1);
}
h[x] = idx--;
fix();
}
int li[200001][20];
int ri[200001][20];
int PL[200001][20];
int PR[200001][20];
void init(int32_t K, vector<int32_t> r){
idx = r.size();
n = r.size();
k = K;
for(int i = 0;i<n;i++){
arr.push_back(r[i]);
h.push_back(-1);
}
build(1,0,n-1);
while(seg[1].first==0){
buld(seg[1].second);
}
for(int i = 0;i<n;i++){
if(h[i]==-1){
assert(0);
}
}
multiset<pair<int,int>> s;
for(int i = 0;i<k-1;i++){
s.insert({h[i],i});
}
for(int i = n-1;i>=0;i--){
auto it = s.lower_bound(make_pair(h[i],0));
if(it==s.begin()){
ri[i][0] = 0;
PR[i][0] = i;
}else{
it--;
PR[i][0] = (*it).second;
ri[i][0] = ((*it).second-i+n)%n;
}
int nah = (i+k-1)%n;
s.erase(s.lower_bound(make_pair(h[nah],nah)));
s.insert({h[i],i});
}
s.clear();
for(int i = n-k+1;i<n;i++){
s.insert({h[i],i});
}
for(int i = 0;i<n;i++){
auto it = s.lower_bound(make_pair(h[i],0));
if(it==s.begin()){
li[i][0] = 0;
PL[i][0] = i;
}else{
it--;
PL[i][0] = (*it).second;
li[i][0] = (i-(*it).second+n)%n;
}
int nah = (i-k+1+n)%n;
s.erase(s.lower_bound(make_pair(h[nah],nah)));
s.insert({h[i],i});
}
for(int j = 1;j<20;j++){
for(int i = 0;i<n;i++){
PL[i][j] = PL[PL[i][j-1]][j-1];
PR[i][j] = PR[PR[i][j-1]][j-1];
li[i][j] = li[i][j-1]+li[PL[i][j-1]][j-1];
ri[i][j] = ri[i][j-1]+ri[PR[i][j-1]][j-1];
}
}
}
bool lol(int x,int y){
if(x<y){
int st = x;
int cur = x;
for(int i = 19;i>=0;i--){
if(st+ri[cur][i]<y){
st+=ri[cur][i];
cur = PR[cur][i];
}
}
st+=ri[cur][0];
if(st>=y&&h[y]<=h[st%n])return 1;
st = x;cur = x;
y-=n;
for(int i = 19;i>=0;i--){
if(st-li[cur][i]>y){
st-=li[cur][i];
cur = PL[cur][i];
}
}
st-=li[cur][0];
if(st<=y&&h[((y%n)+n)%n]<=h[((st%n)+n)%n])return 1;
}else{
int st = x;
int cur = x;
y+=n;
for(int i = 19;i>=0;i--){
if(st+ri[cur][i]<y){
st+=ri[cur][i];
cur = PR[cur][i];
}
}
st+=ri[cur][0];
if(st>=y&&h[y%n]<=h[st%n])return 1;
st = x;cur = x;
y-=n;
for(int i = 19;i>=0;i--){
if(st-li[cur][i]>y){
st-=li[cur][i];
cur = PL[cur][i];
}
}
st-=li[cur][0];
if(st<=y&&h[((y%n)+n)%n]<=h[((st%n)+n)%n])return 1;
}
return 0;
}
int32_t compare_plants(int32_t x, int32_t y){
if(lol(x,y))return 1;
if(lol(y,x))return -1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10584 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10600 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
50 ms |
14296 KB |
Output is correct |
7 |
Correct |
85 ms |
30672 KB |
Output is correct |
8 |
Correct |
376 ms |
150200 KB |
Output is correct |
9 |
Correct |
395 ms |
150328 KB |
Output is correct |
10 |
Correct |
394 ms |
150200 KB |
Output is correct |
11 |
Correct |
393 ms |
150192 KB |
Output is correct |
12 |
Correct |
433 ms |
150184 KB |
Output is correct |
13 |
Correct |
352 ms |
150196 KB |
Output is correct |
14 |
Correct |
534 ms |
150452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
4 ms |
10844 KB |
Output is correct |
7 |
Correct |
59 ms |
20048 KB |
Output is correct |
8 |
Correct |
2 ms |
10584 KB |
Output is correct |
9 |
Correct |
4 ms |
10844 KB |
Output is correct |
10 |
Correct |
71 ms |
20172 KB |
Output is correct |
11 |
Correct |
62 ms |
20048 KB |
Output is correct |
12 |
Correct |
84 ms |
20232 KB |
Output is correct |
13 |
Correct |
57 ms |
20308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
4 ms |
10844 KB |
Output is correct |
7 |
Correct |
59 ms |
20048 KB |
Output is correct |
8 |
Correct |
2 ms |
10584 KB |
Output is correct |
9 |
Correct |
4 ms |
10844 KB |
Output is correct |
10 |
Correct |
71 ms |
20172 KB |
Output is correct |
11 |
Correct |
62 ms |
20048 KB |
Output is correct |
12 |
Correct |
84 ms |
20232 KB |
Output is correct |
13 |
Correct |
57 ms |
20308 KB |
Output is correct |
14 |
Correct |
104 ms |
29132 KB |
Output is correct |
15 |
Correct |
854 ms |
154708 KB |
Output is correct |
16 |
Correct |
105 ms |
29136 KB |
Output is correct |
17 |
Correct |
824 ms |
154808 KB |
Output is correct |
18 |
Correct |
601 ms |
153416 KB |
Output is correct |
19 |
Correct |
727 ms |
153448 KB |
Output is correct |
20 |
Correct |
847 ms |
159672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
63 ms |
15588 KB |
Output is correct |
4 |
Correct |
514 ms |
147636 KB |
Output is correct |
5 |
Correct |
487 ms |
147632 KB |
Output is correct |
6 |
Correct |
593 ms |
147384 KB |
Output is correct |
7 |
Correct |
670 ms |
147816 KB |
Output is correct |
8 |
Correct |
863 ms |
152892 KB |
Output is correct |
9 |
Correct |
414 ms |
147380 KB |
Output is correct |
10 |
Correct |
434 ms |
147384 KB |
Output is correct |
11 |
Correct |
389 ms |
147384 KB |
Output is correct |
12 |
Correct |
575 ms |
147380 KB |
Output is correct |
13 |
Correct |
540 ms |
150708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10668 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10704 KB |
Output is correct |
7 |
Correct |
14 ms |
11628 KB |
Output is correct |
8 |
Correct |
11 ms |
11608 KB |
Output is correct |
9 |
Correct |
14 ms |
11608 KB |
Output is correct |
10 |
Correct |
13 ms |
11636 KB |
Output is correct |
11 |
Correct |
14 ms |
11608 KB |
Output is correct |
12 |
Correct |
14 ms |
11612 KB |
Output is correct |
13 |
Correct |
16 ms |
11868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10840 KB |
Output is correct |
2 |
Correct |
1 ms |
10584 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10844 KB |
Output is correct |
6 |
Correct |
464 ms |
149488 KB |
Output is correct |
7 |
Correct |
606 ms |
149684 KB |
Output is correct |
8 |
Correct |
631 ms |
150196 KB |
Output is correct |
9 |
Correct |
737 ms |
155572 KB |
Output is correct |
10 |
Correct |
414 ms |
149576 KB |
Output is correct |
11 |
Correct |
536 ms |
154804 KB |
Output is correct |
12 |
Correct |
425 ms |
149692 KB |
Output is correct |
13 |
Correct |
478 ms |
149428 KB |
Output is correct |
14 |
Correct |
518 ms |
149680 KB |
Output is correct |
15 |
Correct |
633 ms |
150200 KB |
Output is correct |
16 |
Correct |
388 ms |
149348 KB |
Output is correct |
17 |
Correct |
431 ms |
149492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10584 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10600 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
50 ms |
14296 KB |
Output is correct |
7 |
Correct |
85 ms |
30672 KB |
Output is correct |
8 |
Correct |
376 ms |
150200 KB |
Output is correct |
9 |
Correct |
395 ms |
150328 KB |
Output is correct |
10 |
Correct |
394 ms |
150200 KB |
Output is correct |
11 |
Correct |
393 ms |
150192 KB |
Output is correct |
12 |
Correct |
433 ms |
150184 KB |
Output is correct |
13 |
Correct |
352 ms |
150196 KB |
Output is correct |
14 |
Correct |
534 ms |
150452 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
10588 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
1 ms |
10588 KB |
Output is correct |
20 |
Correct |
4 ms |
10844 KB |
Output is correct |
21 |
Correct |
59 ms |
20048 KB |
Output is correct |
22 |
Correct |
2 ms |
10584 KB |
Output is correct |
23 |
Correct |
4 ms |
10844 KB |
Output is correct |
24 |
Correct |
71 ms |
20172 KB |
Output is correct |
25 |
Correct |
62 ms |
20048 KB |
Output is correct |
26 |
Correct |
84 ms |
20232 KB |
Output is correct |
27 |
Correct |
57 ms |
20308 KB |
Output is correct |
28 |
Correct |
104 ms |
29132 KB |
Output is correct |
29 |
Correct |
854 ms |
154708 KB |
Output is correct |
30 |
Correct |
105 ms |
29136 KB |
Output is correct |
31 |
Correct |
824 ms |
154808 KB |
Output is correct |
32 |
Correct |
601 ms |
153416 KB |
Output is correct |
33 |
Correct |
727 ms |
153448 KB |
Output is correct |
34 |
Correct |
847 ms |
159672 KB |
Output is correct |
35 |
Correct |
1 ms |
10584 KB |
Output is correct |
36 |
Correct |
1 ms |
10588 KB |
Output is correct |
37 |
Correct |
63 ms |
15588 KB |
Output is correct |
38 |
Correct |
514 ms |
147636 KB |
Output is correct |
39 |
Correct |
487 ms |
147632 KB |
Output is correct |
40 |
Correct |
593 ms |
147384 KB |
Output is correct |
41 |
Correct |
670 ms |
147816 KB |
Output is correct |
42 |
Correct |
863 ms |
152892 KB |
Output is correct |
43 |
Correct |
414 ms |
147380 KB |
Output is correct |
44 |
Correct |
434 ms |
147384 KB |
Output is correct |
45 |
Correct |
389 ms |
147384 KB |
Output is correct |
46 |
Correct |
575 ms |
147380 KB |
Output is correct |
47 |
Correct |
540 ms |
150708 KB |
Output is correct |
48 |
Correct |
1 ms |
10588 KB |
Output is correct |
49 |
Correct |
1 ms |
10668 KB |
Output is correct |
50 |
Correct |
1 ms |
10588 KB |
Output is correct |
51 |
Correct |
1 ms |
10588 KB |
Output is correct |
52 |
Correct |
1 ms |
10588 KB |
Output is correct |
53 |
Correct |
3 ms |
10704 KB |
Output is correct |
54 |
Correct |
14 ms |
11628 KB |
Output is correct |
55 |
Correct |
11 ms |
11608 KB |
Output is correct |
56 |
Correct |
14 ms |
11608 KB |
Output is correct |
57 |
Correct |
13 ms |
11636 KB |
Output is correct |
58 |
Correct |
14 ms |
11608 KB |
Output is correct |
59 |
Correct |
14 ms |
11612 KB |
Output is correct |
60 |
Correct |
16 ms |
11868 KB |
Output is correct |
61 |
Correct |
77 ms |
17488 KB |
Output is correct |
62 |
Correct |
109 ms |
30756 KB |
Output is correct |
63 |
Correct |
446 ms |
150104 KB |
Output is correct |
64 |
Correct |
485 ms |
150384 KB |
Output is correct |
65 |
Correct |
587 ms |
150716 KB |
Output is correct |
66 |
Correct |
667 ms |
151224 KB |
Output is correct |
67 |
Correct |
779 ms |
156492 KB |
Output is correct |
68 |
Correct |
470 ms |
150324 KB |
Output is correct |
69 |
Correct |
685 ms |
156088 KB |
Output is correct |
70 |
Correct |
518 ms |
150492 KB |
Output is correct |
71 |
Correct |
524 ms |
150528 KB |
Output is correct |
72 |
Correct |
603 ms |
150452 KB |
Output is correct |
73 |
Correct |
677 ms |
151272 KB |
Output is correct |
74 |
Correct |
438 ms |
150196 KB |
Output is correct |
75 |
Correct |
489 ms |
150548 KB |
Output is correct |