#include "plants.h"
#include <bits/stdc++.h>
#define sz(x) (int)(x.size())
#define debug printf
#define lp(i,a,b) for(int i = a ; i < b; i++)
#define pb push_back
#define ff first
#define ss second
#define mk make_pair
#define pii pair<int,int>
#define ll long long
#define all(x) x.begin(),x.end()
const int MAX = 2e5+10 ;
const int LOG = 20 ;
const int inf = 1e8+10 ;
using namespace std ;
struct Seg
{
ll lz[MAX*4] , mn[MAX*4];
int idx[MAX*4] ;
void clean()
{
memset(lz, 0, sizeof lz) ;
memset(mn, 0, sizeof mn) ;
}
int m(int l, int r) { return (l+r)>>1 ; }
void refresh(int pos, int l, int r)
{
if(!lz[pos]) return ;
mn[pos] += lz[pos] ;
if(l == r) return (void)(lz[pos] = 0 ) ;
lz[pos<<1] += lz[pos] ;
lz[pos<<1|1] += lz[pos] ;
lz[pos] = 0 ;
}
void upd(int pos, int l, int r, int beg, int en , int val )
{
refresh(pos,l,r) ;
if(l > en || r < beg ) return ;
if(l >= beg && r <= en)
{
lz[pos] += (ll)val ;
refresh(pos,l,r) ;
if(l == r) idx[pos] = l ;
return ;
}
upd(pos<<1 , l, m(l,r) , beg, en, val ) ;
upd(pos<<1|1 , m(l,r)+1,r, beg, en, val ) ;
if(mn[pos<<1] <= mn[pos<<1|1])
{
idx[pos] = idx[pos<<1] ;
mn[pos] = mn[pos<<1] ;
}
else
{
idx[pos] = idx[pos<<1|1] ;
mn[pos] = mn[pos<<1|1] ;
}
}
pii qry(int pos, int l, int r, int beg, int en )
{
refresh(pos,l,r) ;
if( l > en || r < beg ) return mk(inf, -1) ;
if(l >= beg && r <= en) return mk( mn[pos] , idx[pos] ) ;
pii al = qry(pos<<1 , l, m(l,r) , beg, en ) ;
pii ar = qry(pos<<1|1 , m(l,r)+1,r, beg, en ) ;
if( al.ff <= ar.ff ) return al ;
return ar ;
}
} seg ;
int n , last , k ;
int dp[2][LOG][MAX] , dist[2][LOG][MAX] ;
vector<int> h , lef , rig ;
vector<pii> intervalLeft[MAX] , intervalRight[MAX] ;
void extract(int x)
{
while(true)
{
pii p = mk(inf, -1) ;
for(auto e : intervalLeft[x] )
p = min(p, seg.qry(1,0,n-1, e.ff, e.ss) ) ;
if(p.ff == 0)
{
extract(p.ss) ;
continue ;
}
h[x] = last-- ;
seg.upd(1,0,n-1, x , x , inf ) ;
for(auto e : intervalLeft[x] ) seg.upd(1,0,n-1, e.ff, e.ss, -1) ;
return ;
}
}
bool isOn(int m, int i) { return ((1<<i)&m) != 0 ; }
int getDist( bool clockwise , int a, int b )
{
if(a == b) return 0 ;
if(!clockwise)
{
if(b < a) return a - b ;
return a + n - b ;
}
if( b > a ) return b-a ;
return n-a+b ;
}
void init(int K, vector<int> r)
{
k = K ;
n = sz(r) ;
h.resize(n,0) ;
last = n ;
//Find intervals
intervalLeft[0].push_back( mk(n-k+1,n-1) ) ;
lp(i,1,n)
{
intervalLeft[i].push_back( mk(max(0,i-k+1) , i-1) ) ;
if( i >= k-1 ) continue ;
int falta = k-1 - i ;
intervalLeft[i].pb( mk( n-falta , n-1 ) ) ;
}
intervalRight[n-1].pb( mk( 0 , k-2 ) ) ;
lp(i,0,n-1)
{
intervalRight[i].pb( mk( i+1, min(n-1, i+k-1) ) ) ;
if( i+k-1 <= n-1 ) continue ;
int lef = k-1 - (n-i-1) ;
intervalRight[i].pb( mk(0,lef-1)) ;
}
for(int i = 0 ; i < n ; i++ )
seg.upd(1,0,n-1, i , i , r[i] );
while(true)
{
pii p = seg.qry(1,0,n-1,0,n-1) ;
if(p.ff != 0) break ;
extract(p.ss) ;
}
if(2*k > n ) return ;
seg.clean() ;
vector<int> idx(n) ;
iota(all(idx) , 0 ) ;
sort(all(idx), [&](int i, int j){ return h[i] < h[j]; } ) ;
memset(dp, -1, sizeof dp ) ;
for(auto e : idx )
{
pii p = mk(0 , -1 ) ;
for(auto e : intervalLeft[e] )
p = min(p , seg.qry(1,0,n-1, e.ff, e.ss) ) ;
dp[0][0][e] = p.ss ;
p = mk(0, -1 ) ;
for(auto e : intervalRight[e] )
p = min(p, seg.qry(1,0,n-1, e.ff, e.ss) ) ;
dp[1][0][e] = p.ss ;
seg.upd(1,0,n-1,e,e, -h[e] ) ;
}
lp(i,0,2)
lp(j,0,n)
if(dp[i][0][j] != -1)
dist[i][0][j] = getDist(i, j , dp[i][0][j] ) ;
lp(i,0,2)
lp(j,1,LOG)
lp(g,0,n)
{
if(dp[i][j-1][g] == -1) continue ;
dp[i][j][g] = dp[i][j-1][ dp[i][j-1][g] ] ;
dist[i][j][g] = dist[i][j-1][g] + dist[i][j-1][ dp[i][j-1][g] ] ;
}
/*for(auto e : h ) cout << e << " " ;
cout << endl ;
cout << dp[0][0][59] << endl ; */
}
int compare_plants(int x, int y)
{
if( 2*k > n || getDist(false, x, y) < k || getDist(true, x, y) < k)
return h[x] > h[y] ? 1 : -1 ;
for(int cur : {x,y} )
for(int type = 0 ; type < 2 ; type++ )
{
int filho = cur , _filho ;
int other = (cur == x ) ? y : x ;
int d = getDist(type, cur, other) ;
if(h[other] > h[cur]) continue ;
for(int i = LOG-1 ; i >= 0 ; i-- )
{
_filho = dp[type][i][filho] ;
if(_filho == -1 ) continue ;
if(dist[type][i][filho] > d) continue ;
//cout << filho << " " << type << " " << _filho << " " << h[_filho] << " " << i << " " << dist[type][i][filho] << " "<< d << endl ;
//int distToCur = getDist(type, _filho, cur ) ;
int distToOther = getDist(type, _filho, other ) ;
if(distToOther < k )
{
if(h[other] <= h[_filho] )
return (cur == x) ? 1 : -1 ;
}
else if(h[_filho] > h[other] ) d -= dist[type][i][filho] , filho = _filho ;
}
if(h[other] <= h[filho] && getDist(type, filho, other) < k )
return (cur == x) ? 1 : -1 ;
}
return 0 ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9728 KB |
Output is correct |
2 |
Correct |
6 ms |
9728 KB |
Output is correct |
3 |
Correct |
30 ms |
53624 KB |
Output is correct |
4 |
Correct |
29 ms |
53632 KB |
Output is correct |
5 |
Correct |
30 ms |
53624 KB |
Output is correct |
6 |
Correct |
105 ms |
56484 KB |
Output is correct |
7 |
Correct |
198 ms |
59800 KB |
Output is correct |
8 |
Correct |
683 ms |
84600 KB |
Output is correct |
9 |
Correct |
718 ms |
88312 KB |
Output is correct |
10 |
Correct |
747 ms |
87928 KB |
Output is correct |
11 |
Correct |
790 ms |
85624 KB |
Output is correct |
12 |
Correct |
800 ms |
86652 KB |
Output is correct |
13 |
Correct |
894 ms |
87540 KB |
Output is correct |
14 |
Correct |
806 ms |
87544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9728 KB |
Output is correct |
2 |
Correct |
6 ms |
9728 KB |
Output is correct |
3 |
Correct |
6 ms |
9728 KB |
Output is correct |
4 |
Correct |
6 ms |
9728 KB |
Output is correct |
5 |
Correct |
8 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9856 KB |
Output is correct |
7 |
Correct |
86 ms |
13304 KB |
Output is correct |
8 |
Correct |
8 ms |
9856 KB |
Output is correct |
9 |
Correct |
10 ms |
9856 KB |
Output is correct |
10 |
Correct |
86 ms |
13304 KB |
Output is correct |
11 |
Correct |
83 ms |
13176 KB |
Output is correct |
12 |
Correct |
87 ms |
13432 KB |
Output is correct |
13 |
Correct |
85 ms |
13304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9728 KB |
Output is correct |
2 |
Correct |
6 ms |
9728 KB |
Output is correct |
3 |
Correct |
6 ms |
9728 KB |
Output is correct |
4 |
Correct |
6 ms |
9728 KB |
Output is correct |
5 |
Correct |
8 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9856 KB |
Output is correct |
7 |
Correct |
86 ms |
13304 KB |
Output is correct |
8 |
Correct |
8 ms |
9856 KB |
Output is correct |
9 |
Correct |
10 ms |
9856 KB |
Output is correct |
10 |
Correct |
86 ms |
13304 KB |
Output is correct |
11 |
Correct |
83 ms |
13176 KB |
Output is correct |
12 |
Correct |
87 ms |
13432 KB |
Output is correct |
13 |
Correct |
85 ms |
13304 KB |
Output is correct |
14 |
Correct |
133 ms |
15352 KB |
Output is correct |
15 |
Correct |
789 ms |
37368 KB |
Output is correct |
16 |
Correct |
128 ms |
15352 KB |
Output is correct |
17 |
Correct |
800 ms |
37348 KB |
Output is correct |
18 |
Correct |
452 ms |
37240 KB |
Output is correct |
19 |
Correct |
463 ms |
37244 KB |
Output is correct |
20 |
Correct |
725 ms |
37364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
53632 KB |
Output is correct |
2 |
Correct |
29 ms |
53624 KB |
Output is correct |
3 |
Correct |
139 ms |
56824 KB |
Output is correct |
4 |
Correct |
989 ms |
102576 KB |
Output is correct |
5 |
Correct |
1086 ms |
96632 KB |
Output is correct |
6 |
Correct |
1383 ms |
98936 KB |
Output is correct |
7 |
Correct |
1444 ms |
100472 KB |
Output is correct |
8 |
Correct |
1399 ms |
101880 KB |
Output is correct |
9 |
Correct |
1000 ms |
89832 KB |
Output is correct |
10 |
Correct |
983 ms |
96376 KB |
Output is correct |
11 |
Correct |
890 ms |
87544 KB |
Output is correct |
12 |
Correct |
763 ms |
87672 KB |
Output is correct |
13 |
Correct |
731 ms |
88184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9728 KB |
Output is correct |
2 |
Correct |
6 ms |
9728 KB |
Output is correct |
3 |
Correct |
29 ms |
53632 KB |
Output is correct |
4 |
Correct |
29 ms |
53624 KB |
Output is correct |
5 |
Correct |
30 ms |
53628 KB |
Output is correct |
6 |
Correct |
32 ms |
53632 KB |
Output is correct |
7 |
Correct |
50 ms |
54264 KB |
Output is correct |
8 |
Correct |
46 ms |
54264 KB |
Output is correct |
9 |
Correct |
52 ms |
54264 KB |
Output is correct |
10 |
Correct |
46 ms |
54392 KB |
Output is correct |
11 |
Correct |
51 ms |
54392 KB |
Output is correct |
12 |
Correct |
52 ms |
54264 KB |
Output is correct |
13 |
Correct |
22 ms |
10368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9728 KB |
Output is correct |
2 |
Correct |
6 ms |
9728 KB |
Output is correct |
3 |
Correct |
30 ms |
53624 KB |
Output is correct |
4 |
Correct |
30 ms |
53632 KB |
Output is correct |
5 |
Correct |
33 ms |
53760 KB |
Output is correct |
6 |
Correct |
745 ms |
86136 KB |
Output is correct |
7 |
Correct |
1194 ms |
91284 KB |
Output is correct |
8 |
Correct |
1295 ms |
97144 KB |
Output is correct |
9 |
Correct |
1411 ms |
101936 KB |
Output is correct |
10 |
Correct |
672 ms |
89464 KB |
Output is correct |
11 |
Correct |
979 ms |
94968 KB |
Output is correct |
12 |
Correct |
762 ms |
103672 KB |
Output is correct |
13 |
Correct |
852 ms |
97780 KB |
Output is correct |
14 |
Correct |
1198 ms |
98808 KB |
Output is correct |
15 |
Correct |
1389 ms |
100344 KB |
Output is correct |
16 |
Correct |
702 ms |
97144 KB |
Output is correct |
17 |
Correct |
754 ms |
92316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9728 KB |
Output is correct |
2 |
Correct |
6 ms |
9728 KB |
Output is correct |
3 |
Correct |
30 ms |
53624 KB |
Output is correct |
4 |
Correct |
29 ms |
53632 KB |
Output is correct |
5 |
Correct |
30 ms |
53624 KB |
Output is correct |
6 |
Correct |
105 ms |
56484 KB |
Output is correct |
7 |
Correct |
198 ms |
59800 KB |
Output is correct |
8 |
Correct |
683 ms |
84600 KB |
Output is correct |
9 |
Correct |
718 ms |
88312 KB |
Output is correct |
10 |
Correct |
747 ms |
87928 KB |
Output is correct |
11 |
Correct |
790 ms |
85624 KB |
Output is correct |
12 |
Correct |
800 ms |
86652 KB |
Output is correct |
13 |
Correct |
894 ms |
87540 KB |
Output is correct |
14 |
Correct |
806 ms |
87544 KB |
Output is correct |
15 |
Correct |
6 ms |
9728 KB |
Output is correct |
16 |
Correct |
6 ms |
9728 KB |
Output is correct |
17 |
Correct |
6 ms |
9728 KB |
Output is correct |
18 |
Correct |
6 ms |
9728 KB |
Output is correct |
19 |
Correct |
8 ms |
9728 KB |
Output is correct |
20 |
Correct |
10 ms |
9856 KB |
Output is correct |
21 |
Correct |
86 ms |
13304 KB |
Output is correct |
22 |
Correct |
8 ms |
9856 KB |
Output is correct |
23 |
Correct |
10 ms |
9856 KB |
Output is correct |
24 |
Correct |
86 ms |
13304 KB |
Output is correct |
25 |
Correct |
83 ms |
13176 KB |
Output is correct |
26 |
Correct |
87 ms |
13432 KB |
Output is correct |
27 |
Correct |
85 ms |
13304 KB |
Output is correct |
28 |
Correct |
133 ms |
15352 KB |
Output is correct |
29 |
Correct |
789 ms |
37368 KB |
Output is correct |
30 |
Correct |
128 ms |
15352 KB |
Output is correct |
31 |
Correct |
800 ms |
37348 KB |
Output is correct |
32 |
Correct |
452 ms |
37240 KB |
Output is correct |
33 |
Correct |
463 ms |
37244 KB |
Output is correct |
34 |
Correct |
725 ms |
37364 KB |
Output is correct |
35 |
Correct |
30 ms |
53632 KB |
Output is correct |
36 |
Correct |
29 ms |
53624 KB |
Output is correct |
37 |
Correct |
139 ms |
56824 KB |
Output is correct |
38 |
Correct |
989 ms |
102576 KB |
Output is correct |
39 |
Correct |
1086 ms |
96632 KB |
Output is correct |
40 |
Correct |
1383 ms |
98936 KB |
Output is correct |
41 |
Correct |
1444 ms |
100472 KB |
Output is correct |
42 |
Correct |
1399 ms |
101880 KB |
Output is correct |
43 |
Correct |
1000 ms |
89832 KB |
Output is correct |
44 |
Correct |
983 ms |
96376 KB |
Output is correct |
45 |
Correct |
890 ms |
87544 KB |
Output is correct |
46 |
Correct |
763 ms |
87672 KB |
Output is correct |
47 |
Correct |
731 ms |
88184 KB |
Output is correct |
48 |
Correct |
6 ms |
9728 KB |
Output is correct |
49 |
Correct |
6 ms |
9728 KB |
Output is correct |
50 |
Correct |
29 ms |
53632 KB |
Output is correct |
51 |
Correct |
29 ms |
53624 KB |
Output is correct |
52 |
Correct |
30 ms |
53628 KB |
Output is correct |
53 |
Correct |
32 ms |
53632 KB |
Output is correct |
54 |
Correct |
50 ms |
54264 KB |
Output is correct |
55 |
Correct |
46 ms |
54264 KB |
Output is correct |
56 |
Correct |
52 ms |
54264 KB |
Output is correct |
57 |
Correct |
46 ms |
54392 KB |
Output is correct |
58 |
Correct |
51 ms |
54392 KB |
Output is correct |
59 |
Correct |
52 ms |
54264 KB |
Output is correct |
60 |
Correct |
22 ms |
10368 KB |
Output is correct |
61 |
Correct |
129 ms |
56696 KB |
Output is correct |
62 |
Correct |
201 ms |
59128 KB |
Output is correct |
63 |
Correct |
804 ms |
82932 KB |
Output is correct |
64 |
Correct |
1006 ms |
86392 KB |
Output is correct |
65 |
Correct |
1367 ms |
91384 KB |
Output is correct |
66 |
Correct |
1419 ms |
97144 KB |
Output is correct |
67 |
Incorrect |
1383 ms |
101968 KB |
Output isn't correct |
68 |
Halted |
0 ms |
0 KB |
- |