#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
const int maxl = 20;
const int maxn = 100010;
const int inf = 1000000010;
class SparseTable
{
public:
void build(int n, vector<int> v, bool mn = false)
{
isMin = mn;
for(int i = 1 ; i <= n ; i++)
tab[0][i] = (mn ? -v[i] : v[i]);
for(int k = 1 ; k < maxl ; k++)
for(int i = 1 ; i + (1 << k) - 1 <= n ; i++)
tab[k][i] = max( tab[k - 1][i] , tab[k - 1][i + (1 << (k - 1))] );
}
int query(int l, int r)
{
if( l > r )
return (isMin ? inf : -inf);
int t = 31 - __builtin_clz(r - l + 1);
int ans = max( tab[t][l] , tab[t][r - (1 << t) + 1] );
return (isMin ? -ans : ans);
}
protected:
bool isMin;
int tab[maxl][maxn];
};
int n;
int v[maxn];
bool hasBuilt = false;
SparseTable stV, stR, stL;
vector<int> valleys, optL, optR;
bool isValid(int i, int j, int D)
{
int peak = stV.query( i + 1 , j - 1 );
if( v[i] <= peak - D && v[j] <= peak - D )
return true;
return false;
}
int findOptL(int i, int D)
{
int l = 1, r = i - 1;
if( v[i] > stV.query( l , i - 1 ) - D )
return 0;
while( l < r )
{
int m = ( l + r )/2;
if( l == r - 1 ) m = r;
if( v[i] <= stV.query( m , i - 1 ) - D )
l = m;
else
r = m - 1;
}
return l;
}
int findOptR(int i, int D)
{
int l = i + 1, r = n;
if( v[i] > stV.query( i + 1 , r ) - D )
return n + 1;
while( l < r )
{
int m = ( l + r )/2;
if( v[i] <= stV.query( i + 1 , m ) - D )
r = m;
else
l = m + 1;
}
return r;
}
void buildHistogram(int D)
{
optL.resize( n + 1 );
optR.resize( n + 1 );
for(int i = 1 ; i <= n ; i++)
{
optL[i] = findOptL(i, D);
optR[i] = findOptR(i, D);
}
}
void build(int D)
{
buildHistogram(D);
stR.build( n , optR , true );
stL.build( n , optL );
for(int i = 1 ; i <= n ; i++)
{
while( !valleys.empty() )
{
int j = valleys.back();
if( isValid( j , i , D ) )
{
valleys.push_back( i );
break;
}
if( v[j] < v[i] )
break;
valleys.pop_back();
}
if( valleys.empty() )
valleys.push_back( i );
}
hasBuilt = true;
}
void init(int N, vector<int> H) {
n = N;
H.insert( H.begin() , 0 );
for(int i = 1 ; i <= n ; i++)
v[i] = H[i];
stV.build( n , H );
}
bool newPairL(int L, int opt)
{
int l = L, r = optL[opt] - 1;
if( stR.query( l , r ) < opt )
return true;
return false;
}
bool newPairR(int R, int opt)
{
int l = optR[opt] + 1, r = R;
if( opt < stL.query( l , r ) )
return true;
return false;
}
int max_towers(int L, int R, int D)
{
if( !hasBuilt )
build( D );
L++; R++;
if( R - L + 1 <= 2 )
return 1;
int pL = lower_bound( valleys.begin() , valleys.end() , L ) - valleys.begin();
int pR = upper_bound( valleys.begin() , valleys.end() , R ) - valleys.begin();
if( pL == pR )
{
if( newPairL( L , R ) || newPairR( R , L ) )
return 2;
return 1;
}
int ans = pR - pL;
if( newPairL( L , valleys[pL] ) )
ans++;
if( newPairR( R , valleys[pR - 1] ) )
ans++;
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
429 ms |
11980 KB |
31st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
692 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
2 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
720 KB |
Output is correct |
16 |
Correct |
1 ms |
720 KB |
Output is correct |
17 |
Correct |
1 ms |
592 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
592 KB |
Output is correct |
21 |
Correct |
1 ms |
720 KB |
Output is correct |
22 |
Correct |
1 ms |
720 KB |
Output is correct |
23 |
Correct |
1 ms |
592 KB |
Output is correct |
24 |
Correct |
1 ms |
592 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
592 KB |
Output is correct |
27 |
Correct |
1 ms |
592 KB |
Output is correct |
28 |
Correct |
1 ms |
592 KB |
Output is correct |
29 |
Correct |
1 ms |
592 KB |
Output is correct |
30 |
Correct |
1 ms |
592 KB |
Output is correct |
31 |
Correct |
2 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
592 KB |
Output is correct |
33 |
Correct |
1 ms |
592 KB |
Output is correct |
34 |
Correct |
1 ms |
592 KB |
Output is correct |
35 |
Correct |
1 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
692 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
2 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
720 KB |
Output is correct |
16 |
Correct |
1 ms |
720 KB |
Output is correct |
17 |
Correct |
1 ms |
592 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
592 KB |
Output is correct |
21 |
Correct |
1 ms |
720 KB |
Output is correct |
22 |
Correct |
1 ms |
720 KB |
Output is correct |
23 |
Correct |
1 ms |
592 KB |
Output is correct |
24 |
Correct |
1 ms |
592 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
592 KB |
Output is correct |
27 |
Correct |
1 ms |
592 KB |
Output is correct |
28 |
Correct |
1 ms |
592 KB |
Output is correct |
29 |
Correct |
1 ms |
592 KB |
Output is correct |
30 |
Correct |
1 ms |
592 KB |
Output is correct |
31 |
Correct |
2 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
592 KB |
Output is correct |
33 |
Correct |
1 ms |
592 KB |
Output is correct |
34 |
Correct |
1 ms |
592 KB |
Output is correct |
35 |
Correct |
1 ms |
592 KB |
Output is correct |
36 |
Correct |
26 ms |
13196 KB |
Output is correct |
37 |
Correct |
38 ms |
20756 KB |
Output is correct |
38 |
Correct |
36 ms |
20680 KB |
Output is correct |
39 |
Correct |
27 ms |
20696 KB |
Output is correct |
40 |
Correct |
38 ms |
20712 KB |
Output is correct |
41 |
Correct |
34 ms |
20760 KB |
Output is correct |
42 |
Correct |
42 ms |
20832 KB |
Output is correct |
43 |
Correct |
34 ms |
20724 KB |
Output is correct |
44 |
Correct |
32 ms |
20832 KB |
Output is correct |
45 |
Correct |
34 ms |
20752 KB |
Output is correct |
46 |
Correct |
32 ms |
20704 KB |
Output is correct |
47 |
Correct |
44 ms |
20748 KB |
Output is correct |
48 |
Correct |
41 ms |
20764 KB |
Output is correct |
49 |
Correct |
46 ms |
20792 KB |
Output is correct |
50 |
Correct |
33 ms |
20688 KB |
Output is correct |
51 |
Correct |
33 ms |
20780 KB |
Output is correct |
52 |
Correct |
44 ms |
20708 KB |
Output is correct |
53 |
Correct |
39 ms |
20832 KB |
Output is correct |
54 |
Correct |
39 ms |
20752 KB |
Output is correct |
55 |
Correct |
31 ms |
20748 KB |
Output is correct |
56 |
Correct |
35 ms |
20764 KB |
Output is correct |
57 |
Correct |
35 ms |
20044 KB |
Output is correct |
58 |
Correct |
39 ms |
20756 KB |
Output is correct |
59 |
Correct |
39 ms |
20692 KB |
Output is correct |
60 |
Correct |
45 ms |
20908 KB |
Output is correct |
61 |
Correct |
43 ms |
20720 KB |
Output is correct |
62 |
Correct |
40 ms |
20708 KB |
Output is correct |
63 |
Correct |
44 ms |
20752 KB |
Output is correct |
64 |
Correct |
37 ms |
20752 KB |
Output is correct |
65 |
Correct |
30 ms |
20740 KB |
Output is correct |
66 |
Correct |
33 ms |
20760 KB |
Output is correct |
67 |
Correct |
29 ms |
20752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
655 ms |
20672 KB |
Output is correct |
2 |
Correct |
851 ms |
20772 KB |
Output is correct |
3 |
Correct |
829 ms |
20668 KB |
Output is correct |
4 |
Correct |
784 ms |
20752 KB |
Output is correct |
5 |
Correct |
753 ms |
20792 KB |
Output is correct |
6 |
Correct |
819 ms |
20784 KB |
Output is correct |
7 |
Correct |
952 ms |
20916 KB |
Output is correct |
8 |
Correct |
1026 ms |
20700 KB |
Output is correct |
9 |
Correct |
522 ms |
20664 KB |
Output is correct |
10 |
Correct |
923 ms |
20664 KB |
Output is correct |
11 |
Correct |
833 ms |
20764 KB |
Output is correct |
12 |
Correct |
770 ms |
20776 KB |
Output is correct |
13 |
Correct |
747 ms |
20740 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
592 KB |
Output is correct |
17 |
Correct |
44 ms |
20712 KB |
Output is correct |
18 |
Correct |
43 ms |
20752 KB |
Output is correct |
19 |
Correct |
40 ms |
20712 KB |
Output is correct |
20 |
Correct |
31 ms |
20760 KB |
Output is correct |
21 |
Correct |
31 ms |
20664 KB |
Output is correct |
22 |
Correct |
42 ms |
20752 KB |
Output is correct |
23 |
Correct |
54 ms |
20908 KB |
Output is correct |
24 |
Correct |
43 ms |
20788 KB |
Output is correct |
25 |
Correct |
38 ms |
20680 KB |
Output is correct |
26 |
Correct |
36 ms |
20664 KB |
Output is correct |
27 |
Correct |
1 ms |
592 KB |
Output is correct |
28 |
Correct |
1 ms |
592 KB |
Output is correct |
29 |
Correct |
1 ms |
592 KB |
Output is correct |
30 |
Correct |
1 ms |
592 KB |
Output is correct |
31 |
Correct |
1 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
592 KB |
Output is correct |
33 |
Correct |
1 ms |
720 KB |
Output is correct |
34 |
Correct |
1 ms |
592 KB |
Output is correct |
35 |
Correct |
1 ms |
592 KB |
Output is correct |
36 |
Correct |
1 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
298 ms |
4716 KB |
2nd lines differ - on the 1st token, expected: '7063', found: '7197' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
692 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
2 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
720 KB |
Output is correct |
16 |
Correct |
1 ms |
720 KB |
Output is correct |
17 |
Correct |
1 ms |
592 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
592 KB |
Output is correct |
21 |
Correct |
1 ms |
720 KB |
Output is correct |
22 |
Correct |
1 ms |
720 KB |
Output is correct |
23 |
Correct |
1 ms |
592 KB |
Output is correct |
24 |
Correct |
1 ms |
592 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
592 KB |
Output is correct |
27 |
Correct |
1 ms |
592 KB |
Output is correct |
28 |
Correct |
1 ms |
592 KB |
Output is correct |
29 |
Correct |
1 ms |
592 KB |
Output is correct |
30 |
Correct |
1 ms |
592 KB |
Output is correct |
31 |
Correct |
2 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
592 KB |
Output is correct |
33 |
Correct |
1 ms |
592 KB |
Output is correct |
34 |
Correct |
1 ms |
592 KB |
Output is correct |
35 |
Correct |
1 ms |
592 KB |
Output is correct |
36 |
Correct |
26 ms |
13196 KB |
Output is correct |
37 |
Correct |
38 ms |
20756 KB |
Output is correct |
38 |
Correct |
36 ms |
20680 KB |
Output is correct |
39 |
Correct |
27 ms |
20696 KB |
Output is correct |
40 |
Correct |
38 ms |
20712 KB |
Output is correct |
41 |
Correct |
34 ms |
20760 KB |
Output is correct |
42 |
Correct |
42 ms |
20832 KB |
Output is correct |
43 |
Correct |
34 ms |
20724 KB |
Output is correct |
44 |
Correct |
32 ms |
20832 KB |
Output is correct |
45 |
Correct |
34 ms |
20752 KB |
Output is correct |
46 |
Correct |
32 ms |
20704 KB |
Output is correct |
47 |
Correct |
44 ms |
20748 KB |
Output is correct |
48 |
Correct |
41 ms |
20764 KB |
Output is correct |
49 |
Correct |
46 ms |
20792 KB |
Output is correct |
50 |
Correct |
33 ms |
20688 KB |
Output is correct |
51 |
Correct |
33 ms |
20780 KB |
Output is correct |
52 |
Correct |
44 ms |
20708 KB |
Output is correct |
53 |
Correct |
39 ms |
20832 KB |
Output is correct |
54 |
Correct |
39 ms |
20752 KB |
Output is correct |
55 |
Correct |
31 ms |
20748 KB |
Output is correct |
56 |
Correct |
35 ms |
20764 KB |
Output is correct |
57 |
Correct |
35 ms |
20044 KB |
Output is correct |
58 |
Correct |
39 ms |
20756 KB |
Output is correct |
59 |
Correct |
39 ms |
20692 KB |
Output is correct |
60 |
Correct |
45 ms |
20908 KB |
Output is correct |
61 |
Correct |
43 ms |
20720 KB |
Output is correct |
62 |
Correct |
40 ms |
20708 KB |
Output is correct |
63 |
Correct |
44 ms |
20752 KB |
Output is correct |
64 |
Correct |
37 ms |
20752 KB |
Output is correct |
65 |
Correct |
30 ms |
20740 KB |
Output is correct |
66 |
Correct |
33 ms |
20760 KB |
Output is correct |
67 |
Correct |
29 ms |
20752 KB |
Output is correct |
68 |
Correct |
655 ms |
20672 KB |
Output is correct |
69 |
Correct |
851 ms |
20772 KB |
Output is correct |
70 |
Correct |
829 ms |
20668 KB |
Output is correct |
71 |
Correct |
784 ms |
20752 KB |
Output is correct |
72 |
Correct |
753 ms |
20792 KB |
Output is correct |
73 |
Correct |
819 ms |
20784 KB |
Output is correct |
74 |
Correct |
952 ms |
20916 KB |
Output is correct |
75 |
Correct |
1026 ms |
20700 KB |
Output is correct |
76 |
Correct |
522 ms |
20664 KB |
Output is correct |
77 |
Correct |
923 ms |
20664 KB |
Output is correct |
78 |
Correct |
833 ms |
20764 KB |
Output is correct |
79 |
Correct |
770 ms |
20776 KB |
Output is correct |
80 |
Correct |
747 ms |
20740 KB |
Output is correct |
81 |
Correct |
1 ms |
336 KB |
Output is correct |
82 |
Correct |
1 ms |
592 KB |
Output is correct |
83 |
Correct |
1 ms |
592 KB |
Output is correct |
84 |
Correct |
44 ms |
20712 KB |
Output is correct |
85 |
Correct |
43 ms |
20752 KB |
Output is correct |
86 |
Correct |
40 ms |
20712 KB |
Output is correct |
87 |
Correct |
31 ms |
20760 KB |
Output is correct |
88 |
Correct |
31 ms |
20664 KB |
Output is correct |
89 |
Correct |
42 ms |
20752 KB |
Output is correct |
90 |
Correct |
54 ms |
20908 KB |
Output is correct |
91 |
Correct |
43 ms |
20788 KB |
Output is correct |
92 |
Correct |
38 ms |
20680 KB |
Output is correct |
93 |
Correct |
36 ms |
20664 KB |
Output is correct |
94 |
Correct |
1 ms |
592 KB |
Output is correct |
95 |
Correct |
1 ms |
592 KB |
Output is correct |
96 |
Correct |
1 ms |
592 KB |
Output is correct |
97 |
Correct |
1 ms |
592 KB |
Output is correct |
98 |
Correct |
1 ms |
592 KB |
Output is correct |
99 |
Correct |
1 ms |
592 KB |
Output is correct |
100 |
Correct |
1 ms |
720 KB |
Output is correct |
101 |
Correct |
1 ms |
592 KB |
Output is correct |
102 |
Correct |
1 ms |
592 KB |
Output is correct |
103 |
Correct |
1 ms |
592 KB |
Output is correct |
104 |
Correct |
794 ms |
18380 KB |
Output is correct |
105 |
Correct |
914 ms |
20720 KB |
Output is correct |
106 |
Correct |
864 ms |
20752 KB |
Output is correct |
107 |
Correct |
692 ms |
20664 KB |
Output is correct |
108 |
Incorrect |
906 ms |
20776 KB |
19976th lines differ - on the 1st token, expected: '2', found: '1' |
109 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
429 ms |
11980 KB |
31st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |