#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define RE(a,b) REP(a,0,b)
#define FOR(a,b) for(auto& a:b)
#define pb push_back
#define fi first
#define se second
#define all(a) a.begin(), a.end()
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
const int INF=1e9;
const int MX=4e5+100;
int n, k;
vi r;
int h[MX];
int htoi[MX];
int nxtUp[MX][20];
int nxtDo[MX][20];
int END;
struct Seg {
ii SEG[MX*4]; int LAZY[MX*4];
void build(const vi& f, int p=0, int l=0, int r=n-1) {
LAZY[p] = 0;
if(l == r) {
SEG[p] = {f[l], l};
return;
}
int m=(l+r)/2;
build(f,p*2+1,l,m);
build(f,p*2+2,m+1,r);
SEG[p] = max(SEG[p*2+1], SEG[p*2+2]);
}
void add(int i, int j, int v, int lazy=0, int p=0, int l=0, int r=n-1) {
LAZY[p] += lazy;
SEG[p].fi += lazy;
if(j < l || i > r) return;
if(i <= l && j >= r) {
SEG[p].fi += v;
LAZY[p] += v;
return;
}
int m=(l+r)/2;
add(i,j,v,LAZY[p],p*2+1,l,m);
add(i,j,v,LAZY[p],p*2+2,m+1,r);
LAZY[p] = 0;
SEG[p] = max(SEG[p*2+1], SEG[p*2+2]);
}
void addRound(int i, int j, int v) {
if(i >= n) i-=n, j-=n;
if(j < n) {
add(i,j,v);
} else {
add(i,n-1,v);
add(0,j-n,v);
}
}
} seg1, seg2;
struct SegMin {
int SEG[MX*2];
void build() {
RE(i,n*2) SEG[i+n*2]=END;
REV(i,1,n*2) SEG[i]=min(SEG[i*2],SEG[i*2+1]);
}
void set(int x, int v) {
for(SEG[x+=n*2]=v; x>1; x/=2) SEG[x/2]=min(SEG[x],SEG[x^1]);
}
int get(int l, int r) {
int ans=INF;
for(l+=n*2,r+=n*2; l<r; l/=2, r/=2) {
if(l&1) ans = min(ans,SEG[l++]);
if(r&1) ans = min(ans,SEG[--r]);
}
return ans;
}
} segH;
void init(int _k, vi _r) {
k = _k; r = _r;
n = r.size();
END = n*2;
seg1.build(r);
seg2.build(vi(n,0));
RE(i,n) {
while(seg1.SEG[0].fi == k-1) {
int x = seg1.SEG[0].se;
seg1.add(x,x,-INF);
seg2.add(x,x,1);
seg2.addRound(x+1,x+k-1,-1);
}
int x = seg2.SEG[0].se;
seg2.add(x,x,-INF);
seg2.addRound(x+1,x+k-1,1);
seg1.addRound(x+n-k+1,x+n-1,1);
h[x] = h[x+n] = i;
htoi[i] = x;
}
htoi[END] = END;
// calculate next up
{
nxtUp[END][0] = END;
segH.build();
REV(i,0,n) {
int x = htoi[i];
segH.set(x,i);
segH.set(x+n,i);
int nxt = htoi[segH.get(x+1,x+k)];
if(nxt < x) nxt += n;
nxtUp[x ][0] = nxt;
nxtUp[x+n][0] = min(END,nxt+n);
}
}
// calculate next do
{
nxtDo[END][0] = END;
segH.build();
REP(i,0,n) {
int x = htoi[i];
segH.set(x,-i);
segH.set(x+n,-i);
int res = -segH.get(x+1,x+k);
int nxt = (res >= 0 ? htoi[res] : END);
if(nxt < x) nxt += n;
nxtDo[x ][0] = nxt;
nxtDo[x+n][0] = min(END,nxt+n);
}
}
REP(j,1,20) {
RE(i,n*2+1) {
nxtDo[i][j] = nxtDo[nxtDo[i][j-1]][j-1];
nxtUp[i][j] = nxtUp[nxtUp[i][j-1]][j-1];
}
}
return;
}
int getNextUp(int i, int j) {
RE(x,20) if(j&(1<<x)) i = nxtUp[i][x];
return i;
}
int getNextDo(int i, int j) {
RE(x,20) if(j&(1<<x)) i = nxtDo[i][x];
return i;
}
int comp(int x, int y) {
if(h[x] < h[y]) {
int lb=0, ub=n;
while(lb != ub) {
int mid=(lb+ub)/2;
if(getNextUp(x,mid)+k-1 >= y) ub=mid;
else lb=mid+1;
}
int i = getNextUp(x,lb);
if(i != END && i+k-1 >= y && h[i] <= h[y]) return -1;
return 0;
} else {
int lb=0, ub=n;
while(lb != ub) {
int mid=(lb+ub)/2;
if(getNextDo(x,mid)+k-1 >= y) ub=mid;
else lb=mid+1;
}
int i = getNextDo(x,lb);
if(i != END && i+k-1 >= y && h[i] >= h[y]) return 1;
return 0;
}
}
int compare_plants(int x, int y) {
int res = comp(x,y);
if(res != 0) return res;
return -comp(y,x+n);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
25472 KB |
Output is correct |
2 |
Correct |
20 ms |
25472 KB |
Output is correct |
3 |
Correct |
15 ms |
25472 KB |
Output is correct |
4 |
Correct |
15 ms |
25472 KB |
Output is correct |
5 |
Correct |
15 ms |
25472 KB |
Output is correct |
6 |
Correct |
142 ms |
28280 KB |
Output is correct |
7 |
Correct |
547 ms |
35964 KB |
Output is correct |
8 |
Correct |
1115 ms |
102376 KB |
Output is correct |
9 |
Correct |
1458 ms |
102372 KB |
Output is correct |
10 |
Correct |
1970 ms |
102376 KB |
Output is correct |
11 |
Correct |
2613 ms |
102500 KB |
Output is correct |
12 |
Correct |
2629 ms |
102376 KB |
Output is correct |
13 |
Correct |
2480 ms |
102384 KB |
Output is correct |
14 |
Correct |
2323 ms |
102500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
25472 KB |
Output is correct |
2 |
Correct |
15 ms |
25472 KB |
Output is correct |
3 |
Correct |
15 ms |
25472 KB |
Output is correct |
4 |
Correct |
15 ms |
25472 KB |
Output is correct |
5 |
Correct |
16 ms |
25472 KB |
Output is correct |
6 |
Correct |
22 ms |
25856 KB |
Output is correct |
7 |
Correct |
202 ms |
30200 KB |
Output is correct |
8 |
Correct |
18 ms |
25472 KB |
Output is correct |
9 |
Correct |
23 ms |
25908 KB |
Output is correct |
10 |
Correct |
213 ms |
30328 KB |
Output is correct |
11 |
Correct |
220 ms |
30072 KB |
Output is correct |
12 |
Correct |
221 ms |
30336 KB |
Output is correct |
13 |
Correct |
204 ms |
30200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
25472 KB |
Output is correct |
2 |
Correct |
15 ms |
25472 KB |
Output is correct |
3 |
Correct |
15 ms |
25472 KB |
Output is correct |
4 |
Correct |
15 ms |
25472 KB |
Output is correct |
5 |
Correct |
16 ms |
25472 KB |
Output is correct |
6 |
Correct |
22 ms |
25856 KB |
Output is correct |
7 |
Correct |
202 ms |
30200 KB |
Output is correct |
8 |
Correct |
18 ms |
25472 KB |
Output is correct |
9 |
Correct |
23 ms |
25908 KB |
Output is correct |
10 |
Correct |
213 ms |
30328 KB |
Output is correct |
11 |
Correct |
220 ms |
30072 KB |
Output is correct |
12 |
Correct |
221 ms |
30336 KB |
Output is correct |
13 |
Correct |
204 ms |
30200 KB |
Output is correct |
14 |
Correct |
343 ms |
35960 KB |
Output is correct |
15 |
Correct |
1786 ms |
102376 KB |
Output is correct |
16 |
Correct |
396 ms |
35956 KB |
Output is correct |
17 |
Correct |
1801 ms |
102372 KB |
Output is correct |
18 |
Correct |
1952 ms |
102372 KB |
Output is correct |
19 |
Correct |
1792 ms |
102376 KB |
Output is correct |
20 |
Correct |
1595 ms |
102376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
25472 KB |
Output is correct |
2 |
Correct |
15 ms |
25472 KB |
Output is correct |
3 |
Correct |
254 ms |
29048 KB |
Output is correct |
4 |
Correct |
2760 ms |
102504 KB |
Output is correct |
5 |
Correct |
2238 ms |
102404 KB |
Output is correct |
6 |
Correct |
2353 ms |
102376 KB |
Output is correct |
7 |
Correct |
2060 ms |
102376 KB |
Output is correct |
8 |
Correct |
1865 ms |
102376 KB |
Output is correct |
9 |
Correct |
2258 ms |
102504 KB |
Output is correct |
10 |
Correct |
1893 ms |
102372 KB |
Output is correct |
11 |
Correct |
2495 ms |
102428 KB |
Output is correct |
12 |
Correct |
2418 ms |
102508 KB |
Output is correct |
13 |
Correct |
2167 ms |
102372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
25472 KB |
Output is correct |
2 |
Correct |
15 ms |
25472 KB |
Output is correct |
3 |
Correct |
15 ms |
25472 KB |
Output is correct |
4 |
Correct |
15 ms |
25472 KB |
Output is correct |
5 |
Correct |
15 ms |
25472 KB |
Output is correct |
6 |
Correct |
19 ms |
25472 KB |
Output is correct |
7 |
Correct |
60 ms |
26232 KB |
Output is correct |
8 |
Correct |
47 ms |
26232 KB |
Output is correct |
9 |
Correct |
56 ms |
26112 KB |
Output is correct |
10 |
Correct |
46 ms |
26232 KB |
Output is correct |
11 |
Correct |
58 ms |
26112 KB |
Output is correct |
12 |
Correct |
53 ms |
26104 KB |
Output is correct |
13 |
Correct |
47 ms |
26360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
25472 KB |
Output is correct |
2 |
Correct |
17 ms |
25472 KB |
Output is correct |
3 |
Correct |
15 ms |
25472 KB |
Output is correct |
4 |
Correct |
15 ms |
25472 KB |
Output is correct |
5 |
Correct |
18 ms |
25856 KB |
Output is correct |
6 |
Correct |
1502 ms |
102524 KB |
Output is correct |
7 |
Correct |
1593 ms |
102500 KB |
Output is correct |
8 |
Correct |
1526 ms |
102504 KB |
Output is correct |
9 |
Correct |
1554 ms |
102504 KB |
Output is correct |
10 |
Correct |
1262 ms |
102504 KB |
Output is correct |
11 |
Correct |
1443 ms |
102632 KB |
Output is correct |
12 |
Correct |
2074 ms |
102500 KB |
Output is correct |
13 |
Correct |
1813 ms |
102472 KB |
Output is correct |
14 |
Correct |
1656 ms |
102504 KB |
Output is correct |
15 |
Correct |
1573 ms |
102464 KB |
Output is correct |
16 |
Correct |
1407 ms |
102504 KB |
Output is correct |
17 |
Correct |
1457 ms |
102504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
25472 KB |
Output is correct |
2 |
Correct |
20 ms |
25472 KB |
Output is correct |
3 |
Correct |
15 ms |
25472 KB |
Output is correct |
4 |
Correct |
15 ms |
25472 KB |
Output is correct |
5 |
Correct |
15 ms |
25472 KB |
Output is correct |
6 |
Correct |
142 ms |
28280 KB |
Output is correct |
7 |
Correct |
547 ms |
35964 KB |
Output is correct |
8 |
Correct |
1115 ms |
102376 KB |
Output is correct |
9 |
Correct |
1458 ms |
102372 KB |
Output is correct |
10 |
Correct |
1970 ms |
102376 KB |
Output is correct |
11 |
Correct |
2613 ms |
102500 KB |
Output is correct |
12 |
Correct |
2629 ms |
102376 KB |
Output is correct |
13 |
Correct |
2480 ms |
102384 KB |
Output is correct |
14 |
Correct |
2323 ms |
102500 KB |
Output is correct |
15 |
Correct |
15 ms |
25472 KB |
Output is correct |
16 |
Correct |
15 ms |
25472 KB |
Output is correct |
17 |
Correct |
15 ms |
25472 KB |
Output is correct |
18 |
Correct |
15 ms |
25472 KB |
Output is correct |
19 |
Correct |
16 ms |
25472 KB |
Output is correct |
20 |
Correct |
22 ms |
25856 KB |
Output is correct |
21 |
Correct |
202 ms |
30200 KB |
Output is correct |
22 |
Correct |
18 ms |
25472 KB |
Output is correct |
23 |
Correct |
23 ms |
25908 KB |
Output is correct |
24 |
Correct |
213 ms |
30328 KB |
Output is correct |
25 |
Correct |
220 ms |
30072 KB |
Output is correct |
26 |
Correct |
221 ms |
30336 KB |
Output is correct |
27 |
Correct |
204 ms |
30200 KB |
Output is correct |
28 |
Correct |
343 ms |
35960 KB |
Output is correct |
29 |
Correct |
1786 ms |
102376 KB |
Output is correct |
30 |
Correct |
396 ms |
35956 KB |
Output is correct |
31 |
Correct |
1801 ms |
102372 KB |
Output is correct |
32 |
Correct |
1952 ms |
102372 KB |
Output is correct |
33 |
Correct |
1792 ms |
102376 KB |
Output is correct |
34 |
Correct |
1595 ms |
102376 KB |
Output is correct |
35 |
Correct |
15 ms |
25472 KB |
Output is correct |
36 |
Correct |
15 ms |
25472 KB |
Output is correct |
37 |
Correct |
254 ms |
29048 KB |
Output is correct |
38 |
Correct |
2760 ms |
102504 KB |
Output is correct |
39 |
Correct |
2238 ms |
102404 KB |
Output is correct |
40 |
Correct |
2353 ms |
102376 KB |
Output is correct |
41 |
Correct |
2060 ms |
102376 KB |
Output is correct |
42 |
Correct |
1865 ms |
102376 KB |
Output is correct |
43 |
Correct |
2258 ms |
102504 KB |
Output is correct |
44 |
Correct |
1893 ms |
102372 KB |
Output is correct |
45 |
Correct |
2495 ms |
102428 KB |
Output is correct |
46 |
Correct |
2418 ms |
102508 KB |
Output is correct |
47 |
Correct |
2167 ms |
102372 KB |
Output is correct |
48 |
Correct |
15 ms |
25472 KB |
Output is correct |
49 |
Correct |
15 ms |
25472 KB |
Output is correct |
50 |
Correct |
15 ms |
25472 KB |
Output is correct |
51 |
Correct |
15 ms |
25472 KB |
Output is correct |
52 |
Correct |
15 ms |
25472 KB |
Output is correct |
53 |
Correct |
19 ms |
25472 KB |
Output is correct |
54 |
Correct |
60 ms |
26232 KB |
Output is correct |
55 |
Correct |
47 ms |
26232 KB |
Output is correct |
56 |
Correct |
56 ms |
26112 KB |
Output is correct |
57 |
Correct |
46 ms |
26232 KB |
Output is correct |
58 |
Correct |
58 ms |
26112 KB |
Output is correct |
59 |
Correct |
53 ms |
26104 KB |
Output is correct |
60 |
Correct |
47 ms |
26360 KB |
Output is correct |
61 |
Correct |
272 ms |
28920 KB |
Output is correct |
62 |
Correct |
409 ms |
35864 KB |
Output is correct |
63 |
Correct |
1275 ms |
102372 KB |
Output is correct |
64 |
Correct |
1916 ms |
102500 KB |
Output is correct |
65 |
Correct |
2392 ms |
102376 KB |
Output is correct |
66 |
Correct |
2007 ms |
102436 KB |
Output is correct |
67 |
Correct |
1784 ms |
102596 KB |
Output is correct |
68 |
Correct |
2083 ms |
102500 KB |
Output is correct |
69 |
Correct |
1863 ms |
102424 KB |
Output is correct |
70 |
Correct |
2556 ms |
102504 KB |
Output is correct |
71 |
Correct |
2244 ms |
102504 KB |
Output is correct |
72 |
Correct |
2446 ms |
102504 KB |
Output is correct |
73 |
Correct |
2106 ms |
102472 KB |
Output is correct |
74 |
Correct |
1367 ms |
102488 KB |
Output is correct |
75 |
Correct |
1550 ms |
102504 KB |
Output is correct |