#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e5;
const int INF = 1e9+7;
int N, A[MAXN+10];
struct SEG
{
pii minv[MAXN*4+10], maxv[MAXN*4+10];
void init(int node, int tl, int tr)
{
if(tl==tr)
{
minv[node]={A[tl], tl};
maxv[node]={A[tl], tl};
return;
}
int mid=tl+tr>>1;
init(node*2, tl, mid);
init(node*2+1, mid+1, tr);
minv[node]=min(minv[node*2], minv[node*2+1]);
maxv[node]=max(maxv[node*2], maxv[node*2+1]);
}
pii query1(int node, int tl, int tr, int l, int r)
{
if(l<=tl && tr<=r) return minv[node];
if(r<tl || tr<l) return {INF, 0};
int mid=tl+tr>>1;
return min(query1(node*2, tl, mid, l, r), query1(node*2+1, mid+1, tr, l, r));
}
pii query2(int node, int tl, int tr, int l, int r)
{
if(l<=tl && tr<=r) return maxv[node];
if(r<tl || tr<l) return {-INF, 0};
int mid=tl+tr>>1;
return max(query2(node*2, tl, mid, l, r), query2(node*2+1, mid+1, tr, l, r));
}
}seg;
void init(int _N, vector<int> _H)
{
N=_N;
for(int i=1; i<=N; i++) A[i]=_H[i-1];
seg.init(1, 1, N);
}
int solve(int l, int r, int d)
{
//printf("%d %d %d\n", l, r, d);
if(l>r) return 0;
int ret=0;
pii p=seg.query2(1, 1, N, l, r);
int lv=solve(l, p.second-1, d), rv=solve(p.second+1, r, d);
if(seg.query1(1, 1, N, l, p.second-1).first>p.first-d) lv=0;
if(seg.query1(1, 1, N, p.second+1, r).first>p.first-d) rv=0;
ret=lv+rv;
return max(1, ret);
}
int max_towers(int L, int R, int D)
{
L++; R++;
return solve(L, R, D);
}
Compilation message
towers.cpp: In member function 'void SEG::init(int, int, int)':
towers.cpp:25:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
25 | int mid=tl+tr>>1;
| ~~^~~
towers.cpp: In member function 'pii SEG::query1(int, int, int, int, int)':
towers.cpp:35:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
35 | int mid=tl+tr>>1;
| ~~^~~
towers.cpp: In member function 'pii SEG::query2(int, int, int, int, int)':
towers.cpp:42:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
42 | int mid=tl+tr>>1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4008 ms |
9548 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
300 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 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 |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
300 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 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 |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 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 |
18 ms |
3120 KB |
Output is correct |
37 |
Correct |
25 ms |
5536 KB |
Output is correct |
38 |
Correct |
19 ms |
5544 KB |
Output is correct |
39 |
Correct |
23 ms |
5584 KB |
Output is correct |
40 |
Correct |
15 ms |
5584 KB |
Output is correct |
41 |
Correct |
51 ms |
5540 KB |
Output is correct |
42 |
Correct |
12 ms |
5576 KB |
Output is correct |
43 |
Correct |
35 ms |
12356 KB |
Output is correct |
44 |
Correct |
16 ms |
7112 KB |
Output is correct |
45 |
Correct |
18 ms |
5504 KB |
Output is correct |
46 |
Correct |
12 ms |
5548 KB |
Output is correct |
47 |
Correct |
24 ms |
5584 KB |
Output is correct |
48 |
Correct |
25 ms |
5536 KB |
Output is correct |
49 |
Correct |
14 ms |
5560 KB |
Output is correct |
50 |
Correct |
21 ms |
8220 KB |
Output is correct |
51 |
Correct |
28 ms |
10652 KB |
Output is correct |
52 |
Correct |
49 ms |
5540 KB |
Output is correct |
53 |
Correct |
49 ms |
5544 KB |
Output is correct |
54 |
Correct |
49 ms |
5584 KB |
Output is correct |
55 |
Correct |
47 ms |
19272 KB |
Output is correct |
56 |
Correct |
52 ms |
17988 KB |
Output is correct |
57 |
Correct |
50 ms |
5456 KB |
Output is correct |
58 |
Correct |
49 ms |
5552 KB |
Output is correct |
59 |
Correct |
55 ms |
5576 KB |
Output is correct |
60 |
Correct |
47 ms |
5548 KB |
Output is correct |
61 |
Correct |
47 ms |
5548 KB |
Output is correct |
62 |
Correct |
47 ms |
5548 KB |
Output is correct |
63 |
Correct |
47 ms |
5584 KB |
Output is correct |
64 |
Correct |
44 ms |
19264 KB |
Output is correct |
65 |
Correct |
58 ms |
19260 KB |
Output is correct |
66 |
Correct |
49 ms |
13720 KB |
Output is correct |
67 |
Correct |
48 ms |
18432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4037 ms |
5540 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4002 ms |
1632 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
300 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 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 |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 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 |
18 ms |
3120 KB |
Output is correct |
37 |
Correct |
25 ms |
5536 KB |
Output is correct |
38 |
Correct |
19 ms |
5544 KB |
Output is correct |
39 |
Correct |
23 ms |
5584 KB |
Output is correct |
40 |
Correct |
15 ms |
5584 KB |
Output is correct |
41 |
Correct |
51 ms |
5540 KB |
Output is correct |
42 |
Correct |
12 ms |
5576 KB |
Output is correct |
43 |
Correct |
35 ms |
12356 KB |
Output is correct |
44 |
Correct |
16 ms |
7112 KB |
Output is correct |
45 |
Correct |
18 ms |
5504 KB |
Output is correct |
46 |
Correct |
12 ms |
5548 KB |
Output is correct |
47 |
Correct |
24 ms |
5584 KB |
Output is correct |
48 |
Correct |
25 ms |
5536 KB |
Output is correct |
49 |
Correct |
14 ms |
5560 KB |
Output is correct |
50 |
Correct |
21 ms |
8220 KB |
Output is correct |
51 |
Correct |
28 ms |
10652 KB |
Output is correct |
52 |
Correct |
49 ms |
5540 KB |
Output is correct |
53 |
Correct |
49 ms |
5544 KB |
Output is correct |
54 |
Correct |
49 ms |
5584 KB |
Output is correct |
55 |
Correct |
47 ms |
19272 KB |
Output is correct |
56 |
Correct |
52 ms |
17988 KB |
Output is correct |
57 |
Correct |
50 ms |
5456 KB |
Output is correct |
58 |
Correct |
49 ms |
5552 KB |
Output is correct |
59 |
Correct |
55 ms |
5576 KB |
Output is correct |
60 |
Correct |
47 ms |
5548 KB |
Output is correct |
61 |
Correct |
47 ms |
5548 KB |
Output is correct |
62 |
Correct |
47 ms |
5548 KB |
Output is correct |
63 |
Correct |
47 ms |
5584 KB |
Output is correct |
64 |
Correct |
44 ms |
19264 KB |
Output is correct |
65 |
Correct |
58 ms |
19260 KB |
Output is correct |
66 |
Correct |
49 ms |
13720 KB |
Output is correct |
67 |
Correct |
48 ms |
18432 KB |
Output is correct |
68 |
Execution timed out |
4037 ms |
5540 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4008 ms |
9548 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |