#include <bits/stdc++.h>
#include "towers.h"
using namespace std;
typedef long long ll;
int n, pow2;
vector<int> h;
vector<int> segMn, segMx, segPeak, segVal;
bool initialized;
int nxtPeak(int i, int val) {
i += pow2;
while (i) {
if (segMx[i] >= val) break;
if (i & 1) {
if ((i & (i+1)) == 0) return n;
i++;
}
i /= 2;
}
while (i < pow2) {
if (segMx[2*i] >= val) i = 2*i;
else i = 2*i+1;
}
return i-pow2;
}
int nxtVal(int i, int val) {
i += pow2;
while (i) {
if (segMn[i] <= val) break;
if (i & 1) {
if ((i & (i+1)) == 0) return n;
i++;
}
i /= 2;
}
while (i < pow2) {
if (segMn[2*i] <= val) i = 2*i;
else i = 2*i+1;
}
return i-pow2;
}
void setSegPeak(int i, int val) {
i += pow2;
segPeak[i] = val;
i /= 2;
while (i) {
segPeak[i] = min(segPeak[2*i], segPeak[2*i+1]);
i /= 2;
}
}
void setSegVal(int i, int val) {
i += pow2;
segVal[i] = val;
i /= 2;
while (i) {
segVal[i] = min(segVal[2*i], segVal[2*i+1]);
i /= 2;
}
}
int getSegPeak(int low, int high) {
low += pow2; high += pow2;
int res = n;
while (low <= high) {
if (low & 1) res = min(res, segPeak[low++]);
if (!(high & 1)) res = min(res, segPeak[high--]);
low /= 2; high /= 2;
}
return res;
}
int getSegVal(int low, int high) {
low += pow2; high += pow2;
int res = n;
while (low <= high) {
if (low & 1) res = min(res, segVal[low++]);
if (!(high & 1)) res = min(res, segVal[high--]);
low /= 2; high /= 2;
}
return res;
}
void init(int N, vector<int> H) {
n = N; h = H;
initialized = false;
pow2 = 1 << (int)ceil(log2(n));
segMn = vector<int>(2*pow2);
segMx = vector<int>(2*pow2);
for (int i = 0; i < n; i++) {
segMn[pow2+i] = h[i];
segMx[pow2+i] = h[i];
}
for (int i = pow2-1; i > 0; i--) {
segMn[i] = min(segMn[2*i], segMn[2*i+1]);
segMx[i] = max(segMx[2*i], segMx[2*i+1]);
}
}
void init2(int D) {
initialized = true;
segPeak = vector<int>(2*pow2, n);
segVal = vector<int>(2*pow2, n);
for (int i = n-2; i >= 0; i--) {
int nxtP = nxtPeak(i+1, h[i]+D);
int nxtV = nxtVal(i+1, h[i]-D);
nxtP = min(nxtP, getSegPeak(i+1, nxtP-1));
nxtV = min(nxtV, getSegVal(i+1, nxtV-1));
setSegPeak(i, nxtP);
setSegVal(i, nxtV);
}
}
int max_towers(int L, int R, int D) {
if (!initialized) init2(D);
int res = 0;
bool foundK = false;
int cur = L;
while (cur <= R) {
res++;
cur = segPeak[pow2+cur];
cur = segVal[pow2+cur];
}
return res;
}
#ifdef TEST
#include "stub.cpp"
#endif
Compilation message
towers.cpp: In function 'int max_towers(int, int, int)':
towers.cpp:122:10: warning: unused variable 'foundK' [-Wunused-variable]
122 | bool foundK = false;
| ^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
353 ms |
2924 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 |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
520 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
520 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
516 KB |
Output is correct |
36 |
Correct |
15 ms |
2904 KB |
Output is correct |
37 |
Correct |
41 ms |
5328 KB |
Output is correct |
38 |
Correct |
36 ms |
5300 KB |
Output is correct |
39 |
Correct |
41 ms |
5328 KB |
Output is correct |
40 |
Correct |
28 ms |
5328 KB |
Output is correct |
41 |
Correct |
35 ms |
5328 KB |
Output is correct |
42 |
Correct |
30 ms |
5328 KB |
Output is correct |
43 |
Correct |
48 ms |
5328 KB |
Output is correct |
44 |
Correct |
46 ms |
5328 KB |
Output is correct |
45 |
Correct |
52 ms |
5580 KB |
Output is correct |
46 |
Correct |
42 ms |
5328 KB |
Output is correct |
47 |
Correct |
22 ms |
5328 KB |
Output is correct |
48 |
Correct |
22 ms |
5328 KB |
Output is correct |
49 |
Correct |
24 ms |
5324 KB |
Output is correct |
50 |
Correct |
39 ms |
5328 KB |
Output is correct |
51 |
Correct |
35 ms |
5328 KB |
Output is correct |
52 |
Correct |
28 ms |
5308 KB |
Output is correct |
53 |
Correct |
24 ms |
5328 KB |
Output is correct |
54 |
Correct |
21 ms |
5328 KB |
Output is correct |
55 |
Correct |
30 ms |
5200 KB |
Output is correct |
56 |
Correct |
31 ms |
5328 KB |
Output is correct |
57 |
Correct |
34 ms |
5084 KB |
Output is correct |
58 |
Correct |
32 ms |
5328 KB |
Output is correct |
59 |
Correct |
29 ms |
5328 KB |
Output is correct |
60 |
Correct |
29 ms |
5328 KB |
Output is correct |
61 |
Correct |
26 ms |
5328 KB |
Output is correct |
62 |
Correct |
33 ms |
5216 KB |
Output is correct |
63 |
Correct |
26 ms |
5328 KB |
Output is correct |
64 |
Correct |
46 ms |
5328 KB |
Output is correct |
65 |
Correct |
47 ms |
5328 KB |
Output is correct |
66 |
Correct |
44 ms |
5328 KB |
Output is correct |
67 |
Correct |
50 ms |
5328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4078 ms |
5332 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1251 ms |
1744 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 |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
520 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
516 KB |
Output is correct |
36 |
Correct |
15 ms |
2904 KB |
Output is correct |
37 |
Correct |
41 ms |
5328 KB |
Output is correct |
38 |
Correct |
36 ms |
5300 KB |
Output is correct |
39 |
Correct |
41 ms |
5328 KB |
Output is correct |
40 |
Correct |
28 ms |
5328 KB |
Output is correct |
41 |
Correct |
35 ms |
5328 KB |
Output is correct |
42 |
Correct |
30 ms |
5328 KB |
Output is correct |
43 |
Correct |
48 ms |
5328 KB |
Output is correct |
44 |
Correct |
46 ms |
5328 KB |
Output is correct |
45 |
Correct |
52 ms |
5580 KB |
Output is correct |
46 |
Correct |
42 ms |
5328 KB |
Output is correct |
47 |
Correct |
22 ms |
5328 KB |
Output is correct |
48 |
Correct |
22 ms |
5328 KB |
Output is correct |
49 |
Correct |
24 ms |
5324 KB |
Output is correct |
50 |
Correct |
39 ms |
5328 KB |
Output is correct |
51 |
Correct |
35 ms |
5328 KB |
Output is correct |
52 |
Correct |
28 ms |
5308 KB |
Output is correct |
53 |
Correct |
24 ms |
5328 KB |
Output is correct |
54 |
Correct |
21 ms |
5328 KB |
Output is correct |
55 |
Correct |
30 ms |
5200 KB |
Output is correct |
56 |
Correct |
31 ms |
5328 KB |
Output is correct |
57 |
Correct |
34 ms |
5084 KB |
Output is correct |
58 |
Correct |
32 ms |
5328 KB |
Output is correct |
59 |
Correct |
29 ms |
5328 KB |
Output is correct |
60 |
Correct |
29 ms |
5328 KB |
Output is correct |
61 |
Correct |
26 ms |
5328 KB |
Output is correct |
62 |
Correct |
33 ms |
5216 KB |
Output is correct |
63 |
Correct |
26 ms |
5328 KB |
Output is correct |
64 |
Correct |
46 ms |
5328 KB |
Output is correct |
65 |
Correct |
47 ms |
5328 KB |
Output is correct |
66 |
Correct |
44 ms |
5328 KB |
Output is correct |
67 |
Correct |
50 ms |
5328 KB |
Output is correct |
68 |
Execution timed out |
4078 ms |
5332 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
353 ms |
2924 KB |
31st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |