#include "plants.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=2e5+7, INF=1e9+7, LG=20;
set<int>A, B;
pair<int,int>tr[4*LIM];
int lazy[4*LIM], T[LIM], gdzie[LIM], nxt[LIM][LG], prv[LIM][LG], n, k, N=1;
int tr2[4*LIM];
ll sumnxt[LIM][LG], sumprv[LIM][LG];
void spl(int v) {
tr[2*v].st+=lazy[v];
tr[2*v+1].st+=lazy[v];
lazy[2*v]+=lazy[v];
lazy[2*v+1]+=lazy[v];
lazy[v]=0;
}
void upd(int v, int l, int r, int a, int b, int x) {
if(r<a || b<l) return;
if(a<=l && r<=b) {
tr[v].st+=x;
lazy[v]+=x;
return;
}
if(lazy[v]) spl(v);
int mid=(l+r)/2;
upd(2*v, l, mid, a, b, x);
upd(2*v+1, mid+1, r, a, b, x);
tr[v]=min(tr[2*v], tr[2*v+1]);
}
pair<int,int>cnt(int v, int l, int r, int a, int b) {
if(r<a || b<l) return {INF, INF};
if(a<=l && r<=b) return tr[v];
if(lazy[v]) spl(v);
int mid=(l+r)/2;
return min(cnt(2*v, l, mid, a, b), cnt(2*v+1, mid+1, r, a, b));
}
int dyst(int a, int b) {
if(a<=b) return b-a;
return n-a+b;
}
pair<int,int>licz(int x) {
auto it=A.lower_bound(x);
int a=-1, b=-1;
if(it==A.end()) {
auto it2=A.begin();
b=*it2;
} else b=*it;
if(it==A.begin()) {
auto it2=A.end(); --it2;
a=*it2;
} else {
--it;
a=*it;
}
return {a, b};
}
void dodaj(int x) {
if(A.size()==0) {
A.insert(x);
B.insert(x);
return;
}
pair<int,int>p=licz(x);
int a=p.st, b=p.nd;
if(dyst(x, b)<k && B.find(b)!=B.end()) B.erase(b);
if(dyst(a, x)>=k) B.insert(x);
A.insert(x);
}
void usun(int x) {
A.erase(x);
if(B.find(x)!=B.end()) B.erase(x);
if(A.size()==1) B.insert(*A.begin());
if(A.size()<2) return;
pair<int,int>p=licz(x);
int a=p.st, b=p.nd;
if(dyst(a, b)>=k) B.insert(b);
}
void upd2(int v, int x) {
v+=N;
tr2[v]=x;
v/=2;
while(v) {
tr2[v]=min(tr2[2*v], tr2[2*v+1]);
v/=2;
}
}
int cnt2(int l, int r) {
l+=N; r+=N;
int ans=min(tr2[l], tr2[r]);
while(l/2!=r/2) {
if(l%2==0) ans=min(ans, tr2[l+1]);
if(r%2==1) ans=min(ans, tr2[r-1]);
l/=2; r/=2;
}
return ans;
}
void init(int _k, vector<int>r) {
n=r.size(); k=_k;
while(N<n) N*=2;
rep(i, n) tr[N+i]={r[i], i};
for(int i=n; i<N; ++i) tr[N+i]={INF, INF};
for(int i=N-1; i; --i) tr[i]=min(tr[2*i], tr[2*i+1]);
rep(i, n) {
while(tr[1].st==0) {
dodaj(tr[1].nd);
upd(1, 0, N-1, tr[1].nd, tr[1].nd, INF);
}
int x=*B.begin();
usun(x);
upd(1, 0, N-1, x-k+1, x, -1);
if(x-k+1<0) upd(1, 0, N-1, n+x-k+1, n-1, -1);
T[x]=n-i-1;
gdzie[n-i-1]=x;
}
rep(i, 2*N) tr2[i]=INF;
for(int i=n-1; i>=0; --i) {
int p=gdzie[i];
int a=cnt2(p, min(p+k-1, n-1));
if(p+k-1>n-1) a=min(a, cnt2(0, p+k-1-n));
int b=cnt2(max(0, p-k+1), p);
if(p-k+1<0) b=min(b, cnt2(n+p-k+1, n-1));
if(a==INF) a=i;
if(b==INF) b=i;
nxt[i][0]=a;
prv[i][0]=b;
sumnxt[i][0]=dyst(p, gdzie[a]);
sumprv[i][0]=dyst(gdzie[b], p);
upd2(p, i);
}
for(int j=1; j<LG; ++j) rep(i, n) {
nxt[i][j]=nxt[nxt[i][j-1]][j-1];
prv[i][j]=prv[prv[i][j-1]][j-1];
sumnxt[i][j]=sumnxt[i][j-1]+sumnxt[nxt[i][j-1]][j-1];
sumprv[i][j]=sumprv[i][j-1]+sumprv[prv[i][j-1]][j-1];
}
}
bool mniejszy(int x, int y) {
if(x>y) return false;
if(true) {
int p=x;
ll a=dyst(gdzie[x], gdzie[y]), b=0;
for(int i=LG-1; i>=0; --i) if(nxt[p][i]<=y) {
b+=sumnxt[p][i];
p=nxt[p][i];
}
if(b>=a) return true;
}
if(true) {
int p=x;
ll a=dyst(gdzie[y], gdzie[x]), b=0;
for(int i=LG-1; i>=0; --i) if(prv[p][i]<=y) {
b+=sumprv[p][i];
p=prv[p][i];
}
if(b>=a) return true;
}
return false;
}
int compare_plants(int x, int y) {
if(mniejszy(T[x], T[y])) return -1;
if(mniejszy(T[y], T[x])) return 1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10704 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 |
41 ms |
13400 KB |
Output is correct |
7 |
Correct |
94 ms |
24644 KB |
Output is correct |
8 |
Correct |
282 ms |
115376 KB |
Output is correct |
9 |
Correct |
278 ms |
114772 KB |
Output is correct |
10 |
Correct |
280 ms |
114792 KB |
Output is correct |
11 |
Correct |
308 ms |
115436 KB |
Output is correct |
12 |
Correct |
332 ms |
115172 KB |
Output is correct |
13 |
Correct |
395 ms |
119636 KB |
Output is correct |
14 |
Correct |
274 ms |
110164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 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 |
3 ms |
10844 KB |
Output is correct |
7 |
Correct |
61 ms |
17724 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10952 KB |
Output is correct |
10 |
Correct |
59 ms |
17724 KB |
Output is correct |
11 |
Correct |
75 ms |
17748 KB |
Output is correct |
12 |
Correct |
64 ms |
17928 KB |
Output is correct |
13 |
Correct |
60 ms |
17744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 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 |
3 ms |
10844 KB |
Output is correct |
7 |
Correct |
61 ms |
17724 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10952 KB |
Output is correct |
10 |
Correct |
59 ms |
17724 KB |
Output is correct |
11 |
Correct |
75 ms |
17748 KB |
Output is correct |
12 |
Correct |
64 ms |
17928 KB |
Output is correct |
13 |
Correct |
60 ms |
17744 KB |
Output is correct |
14 |
Correct |
92 ms |
24400 KB |
Output is correct |
15 |
Correct |
457 ms |
113704 KB |
Output is correct |
16 |
Correct |
96 ms |
26452 KB |
Output is correct |
17 |
Correct |
441 ms |
113748 KB |
Output is correct |
18 |
Correct |
464 ms |
118096 KB |
Output is correct |
19 |
Correct |
350 ms |
113656 KB |
Output is correct |
20 |
Correct |
462 ms |
113748 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 |
55 ms |
17692 KB |
Output is correct |
4 |
Correct |
349 ms |
116304 KB |
Output is correct |
5 |
Correct |
347 ms |
114016 KB |
Output is correct |
6 |
Correct |
406 ms |
113752 KB |
Output is correct |
7 |
Correct |
396 ms |
113744 KB |
Output is correct |
8 |
Correct |
411 ms |
113672 KB |
Output is correct |
9 |
Correct |
356 ms |
115536 KB |
Output is correct |
10 |
Correct |
334 ms |
113748 KB |
Output is correct |
11 |
Correct |
381 ms |
122192 KB |
Output is correct |
12 |
Correct |
267 ms |
113492 KB |
Output is correct |
13 |
Correct |
455 ms |
120052 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 |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
16 ms |
11404 KB |
Output is correct |
8 |
Correct |
10 ms |
11356 KB |
Output is correct |
9 |
Correct |
11 ms |
11356 KB |
Output is correct |
10 |
Correct |
10 ms |
11356 KB |
Output is correct |
11 |
Correct |
12 ms |
11356 KB |
Output is correct |
12 |
Correct |
12 ms |
11308 KB |
Output is correct |
13 |
Correct |
11 ms |
11352 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 |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10840 KB |
Output is correct |
6 |
Correct |
293 ms |
112880 KB |
Output is correct |
7 |
Correct |
323 ms |
112976 KB |
Output is correct |
8 |
Correct |
367 ms |
113272 KB |
Output is correct |
9 |
Correct |
400 ms |
113532 KB |
Output is correct |
10 |
Correct |
283 ms |
114996 KB |
Output is correct |
11 |
Correct |
361 ms |
113744 KB |
Output is correct |
12 |
Correct |
274 ms |
115796 KB |
Output is correct |
13 |
Correct |
288 ms |
113520 KB |
Output is correct |
14 |
Correct |
333 ms |
113224 KB |
Output is correct |
15 |
Correct |
360 ms |
113356 KB |
Output is correct |
16 |
Correct |
290 ms |
114272 KB |
Output is correct |
17 |
Correct |
277 ms |
112952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10704 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 |
41 ms |
13400 KB |
Output is correct |
7 |
Correct |
94 ms |
24644 KB |
Output is correct |
8 |
Correct |
282 ms |
115376 KB |
Output is correct |
9 |
Correct |
278 ms |
114772 KB |
Output is correct |
10 |
Correct |
280 ms |
114792 KB |
Output is correct |
11 |
Correct |
308 ms |
115436 KB |
Output is correct |
12 |
Correct |
332 ms |
115172 KB |
Output is correct |
13 |
Correct |
395 ms |
119636 KB |
Output is correct |
14 |
Correct |
274 ms |
110164 KB |
Output is correct |
15 |
Correct |
1 ms |
10584 KB |
Output is correct |
16 |
Correct |
2 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 |
3 ms |
10844 KB |
Output is correct |
21 |
Correct |
61 ms |
17724 KB |
Output is correct |
22 |
Correct |
2 ms |
10588 KB |
Output is correct |
23 |
Correct |
3 ms |
10952 KB |
Output is correct |
24 |
Correct |
59 ms |
17724 KB |
Output is correct |
25 |
Correct |
75 ms |
17748 KB |
Output is correct |
26 |
Correct |
64 ms |
17928 KB |
Output is correct |
27 |
Correct |
60 ms |
17744 KB |
Output is correct |
28 |
Correct |
92 ms |
24400 KB |
Output is correct |
29 |
Correct |
457 ms |
113704 KB |
Output is correct |
30 |
Correct |
96 ms |
26452 KB |
Output is correct |
31 |
Correct |
441 ms |
113748 KB |
Output is correct |
32 |
Correct |
464 ms |
118096 KB |
Output is correct |
33 |
Correct |
350 ms |
113656 KB |
Output is correct |
34 |
Correct |
462 ms |
113748 KB |
Output is correct |
35 |
Correct |
1 ms |
10588 KB |
Output is correct |
36 |
Correct |
1 ms |
10588 KB |
Output is correct |
37 |
Correct |
55 ms |
17692 KB |
Output is correct |
38 |
Correct |
349 ms |
116304 KB |
Output is correct |
39 |
Correct |
347 ms |
114016 KB |
Output is correct |
40 |
Correct |
406 ms |
113752 KB |
Output is correct |
41 |
Correct |
396 ms |
113744 KB |
Output is correct |
42 |
Correct |
411 ms |
113672 KB |
Output is correct |
43 |
Correct |
356 ms |
115536 KB |
Output is correct |
44 |
Correct |
334 ms |
113748 KB |
Output is correct |
45 |
Correct |
381 ms |
122192 KB |
Output is correct |
46 |
Correct |
267 ms |
113492 KB |
Output is correct |
47 |
Correct |
455 ms |
120052 KB |
Output is correct |
48 |
Correct |
1 ms |
10588 KB |
Output is correct |
49 |
Correct |
1 ms |
10588 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 |
2 ms |
10588 KB |
Output is correct |
54 |
Correct |
16 ms |
11404 KB |
Output is correct |
55 |
Correct |
10 ms |
11356 KB |
Output is correct |
56 |
Correct |
11 ms |
11356 KB |
Output is correct |
57 |
Correct |
10 ms |
11356 KB |
Output is correct |
58 |
Correct |
12 ms |
11356 KB |
Output is correct |
59 |
Correct |
12 ms |
11308 KB |
Output is correct |
60 |
Correct |
11 ms |
11352 KB |
Output is correct |
61 |
Correct |
50 ms |
19536 KB |
Output is correct |
62 |
Correct |
74 ms |
26640 KB |
Output is correct |
63 |
Correct |
320 ms |
114768 KB |
Output is correct |
64 |
Correct |
349 ms |
113744 KB |
Output is correct |
65 |
Correct |
356 ms |
113908 KB |
Output is correct |
66 |
Correct |
423 ms |
114108 KB |
Output is correct |
67 |
Correct |
426 ms |
114412 KB |
Output is correct |
68 |
Correct |
340 ms |
115992 KB |
Output is correct |
69 |
Correct |
358 ms |
114736 KB |
Output is correct |
70 |
Correct |
323 ms |
116444 KB |
Output is correct |
71 |
Correct |
341 ms |
114492 KB |
Output is correct |
72 |
Correct |
347 ms |
114000 KB |
Output is correct |
73 |
Correct |
378 ms |
114040 KB |
Output is correct |
74 |
Correct |
314 ms |
114512 KB |
Output is correct |
75 |
Correct |
325 ms |
113840 KB |
Output is correct |