#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;
vector<vector<int>> jmp;
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);
}
jmp = vector<vector<int>>(n, vector<int>(20, n));
for (int i = 0; i < n; i++) {
jmp[i][0] = segVal[pow2+segPeak[pow2+i]];
}
for (int b = 1; b < 20; b++) {
for (int i = 0; i < n; i++) {
int nxt = jmp[i][b-1];
if (nxt >= n) continue;
jmp[i][b] = jmp[nxt][b-1];
}
}
}
int max_towers(int L, int R, int D) {
if (!initialized) init2(D);
int res = 1;
int cur = L;
for (int b = 20-1; b >= 0; b--) {
int nxt = jmp[cur][b];
if (nxt > R) continue;
cur = nxt;
res += 1 << b;
}
return res;
}
#ifdef TEST
#include "stub.cpp"
#endif
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
348 ms |
9892 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 |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
656 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
600 KB |
Output is correct |
30 |
Correct |
1 ms |
600 KB |
Output is correct |
31 |
Correct |
1 ms |
600 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
656 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
600 KB |
Output is correct |
30 |
Correct |
1 ms |
600 KB |
Output is correct |
31 |
Correct |
1 ms |
600 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
36 |
Correct |
24 ms |
10584 KB |
Output is correct |
37 |
Correct |
50 ms |
16848 KB |
Output is correct |
38 |
Correct |
53 ms |
16840 KB |
Output is correct |
39 |
Correct |
56 ms |
16840 KB |
Output is correct |
40 |
Correct |
37 ms |
16848 KB |
Output is correct |
41 |
Correct |
43 ms |
16856 KB |
Output is correct |
42 |
Correct |
36 ms |
16840 KB |
Output is correct |
43 |
Correct |
57 ms |
16836 KB |
Output is correct |
44 |
Correct |
54 ms |
16840 KB |
Output is correct |
45 |
Correct |
55 ms |
16840 KB |
Output is correct |
46 |
Correct |
59 ms |
16840 KB |
Output is correct |
47 |
Correct |
34 ms |
16824 KB |
Output is correct |
48 |
Correct |
32 ms |
16848 KB |
Output is correct |
49 |
Correct |
33 ms |
16860 KB |
Output is correct |
50 |
Correct |
43 ms |
16836 KB |
Output is correct |
51 |
Correct |
43 ms |
16860 KB |
Output is correct |
52 |
Correct |
42 ms |
16848 KB |
Output is correct |
53 |
Correct |
32 ms |
16848 KB |
Output is correct |
54 |
Correct |
33 ms |
16848 KB |
Output is correct |
55 |
Correct |
42 ms |
16840 KB |
Output is correct |
56 |
Correct |
44 ms |
17060 KB |
Output is correct |
57 |
Correct |
45 ms |
16596 KB |
Output is correct |
58 |
Correct |
44 ms |
16840 KB |
Output is correct |
59 |
Correct |
40 ms |
16840 KB |
Output is correct |
60 |
Correct |
35 ms |
16840 KB |
Output is correct |
61 |
Correct |
45 ms |
16844 KB |
Output is correct |
62 |
Correct |
46 ms |
16840 KB |
Output is correct |
63 |
Correct |
41 ms |
16848 KB |
Output is correct |
64 |
Correct |
55 ms |
16856 KB |
Output is correct |
65 |
Correct |
54 ms |
16840 KB |
Output is correct |
66 |
Correct |
55 ms |
16840 KB |
Output is correct |
67 |
Correct |
52 ms |
16840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
538 ms |
16860 KB |
Output is correct |
2 |
Correct |
741 ms |
16836 KB |
Output is correct |
3 |
Correct |
749 ms |
16856 KB |
Output is correct |
4 |
Correct |
758 ms |
16848 KB |
Output is correct |
5 |
Correct |
724 ms |
16848 KB |
Output is correct |
6 |
Correct |
777 ms |
16848 KB |
Output is correct |
7 |
Correct |
838 ms |
16840 KB |
Output is correct |
8 |
Correct |
733 ms |
16864 KB |
Output is correct |
9 |
Correct |
749 ms |
16840 KB |
Output is correct |
10 |
Correct |
757 ms |
16840 KB |
Output is correct |
11 |
Correct |
693 ms |
16844 KB |
Output is correct |
12 |
Correct |
710 ms |
16840 KB |
Output is correct |
13 |
Correct |
706 ms |
16900 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
41 ms |
16920 KB |
Output is correct |
18 |
Correct |
36 ms |
16952 KB |
Output is correct |
19 |
Correct |
36 ms |
16840 KB |
Output is correct |
20 |
Correct |
47 ms |
16840 KB |
Output is correct |
21 |
Correct |
45 ms |
16840 KB |
Output is correct |
22 |
Correct |
36 ms |
16840 KB |
Output is correct |
23 |
Correct |
32 ms |
17092 KB |
Output is correct |
24 |
Correct |
32 ms |
16844 KB |
Output is correct |
25 |
Correct |
43 ms |
16840 KB |
Output is correct |
26 |
Correct |
51 ms |
16840 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
600 KB |
Output is correct |
30 |
Correct |
1 ms |
600 KB |
Output is correct |
31 |
Correct |
1 ms |
600 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
36 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
209 ms |
4456 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 |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
656 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
600 KB |
Output is correct |
30 |
Correct |
1 ms |
600 KB |
Output is correct |
31 |
Correct |
1 ms |
600 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
36 |
Correct |
24 ms |
10584 KB |
Output is correct |
37 |
Correct |
50 ms |
16848 KB |
Output is correct |
38 |
Correct |
53 ms |
16840 KB |
Output is correct |
39 |
Correct |
56 ms |
16840 KB |
Output is correct |
40 |
Correct |
37 ms |
16848 KB |
Output is correct |
41 |
Correct |
43 ms |
16856 KB |
Output is correct |
42 |
Correct |
36 ms |
16840 KB |
Output is correct |
43 |
Correct |
57 ms |
16836 KB |
Output is correct |
44 |
Correct |
54 ms |
16840 KB |
Output is correct |
45 |
Correct |
55 ms |
16840 KB |
Output is correct |
46 |
Correct |
59 ms |
16840 KB |
Output is correct |
47 |
Correct |
34 ms |
16824 KB |
Output is correct |
48 |
Correct |
32 ms |
16848 KB |
Output is correct |
49 |
Correct |
33 ms |
16860 KB |
Output is correct |
50 |
Correct |
43 ms |
16836 KB |
Output is correct |
51 |
Correct |
43 ms |
16860 KB |
Output is correct |
52 |
Correct |
42 ms |
16848 KB |
Output is correct |
53 |
Correct |
32 ms |
16848 KB |
Output is correct |
54 |
Correct |
33 ms |
16848 KB |
Output is correct |
55 |
Correct |
42 ms |
16840 KB |
Output is correct |
56 |
Correct |
44 ms |
17060 KB |
Output is correct |
57 |
Correct |
45 ms |
16596 KB |
Output is correct |
58 |
Correct |
44 ms |
16840 KB |
Output is correct |
59 |
Correct |
40 ms |
16840 KB |
Output is correct |
60 |
Correct |
35 ms |
16840 KB |
Output is correct |
61 |
Correct |
45 ms |
16844 KB |
Output is correct |
62 |
Correct |
46 ms |
16840 KB |
Output is correct |
63 |
Correct |
41 ms |
16848 KB |
Output is correct |
64 |
Correct |
55 ms |
16856 KB |
Output is correct |
65 |
Correct |
54 ms |
16840 KB |
Output is correct |
66 |
Correct |
55 ms |
16840 KB |
Output is correct |
67 |
Correct |
52 ms |
16840 KB |
Output is correct |
68 |
Correct |
538 ms |
16860 KB |
Output is correct |
69 |
Correct |
741 ms |
16836 KB |
Output is correct |
70 |
Correct |
749 ms |
16856 KB |
Output is correct |
71 |
Correct |
758 ms |
16848 KB |
Output is correct |
72 |
Correct |
724 ms |
16848 KB |
Output is correct |
73 |
Correct |
777 ms |
16848 KB |
Output is correct |
74 |
Correct |
838 ms |
16840 KB |
Output is correct |
75 |
Correct |
733 ms |
16864 KB |
Output is correct |
76 |
Correct |
749 ms |
16840 KB |
Output is correct |
77 |
Correct |
757 ms |
16840 KB |
Output is correct |
78 |
Correct |
693 ms |
16844 KB |
Output is correct |
79 |
Correct |
710 ms |
16840 KB |
Output is correct |
80 |
Correct |
706 ms |
16900 KB |
Output is correct |
81 |
Correct |
0 ms |
344 KB |
Output is correct |
82 |
Correct |
1 ms |
600 KB |
Output is correct |
83 |
Correct |
1 ms |
600 KB |
Output is correct |
84 |
Correct |
41 ms |
16920 KB |
Output is correct |
85 |
Correct |
36 ms |
16952 KB |
Output is correct |
86 |
Correct |
36 ms |
16840 KB |
Output is correct |
87 |
Correct |
47 ms |
16840 KB |
Output is correct |
88 |
Correct |
45 ms |
16840 KB |
Output is correct |
89 |
Correct |
36 ms |
16840 KB |
Output is correct |
90 |
Correct |
32 ms |
17092 KB |
Output is correct |
91 |
Correct |
32 ms |
16844 KB |
Output is correct |
92 |
Correct |
43 ms |
16840 KB |
Output is correct |
93 |
Correct |
51 ms |
16840 KB |
Output is correct |
94 |
Correct |
1 ms |
600 KB |
Output is correct |
95 |
Correct |
1 ms |
600 KB |
Output is correct |
96 |
Correct |
1 ms |
600 KB |
Output is correct |
97 |
Correct |
1 ms |
600 KB |
Output is correct |
98 |
Correct |
1 ms |
600 KB |
Output is correct |
99 |
Correct |
1 ms |
600 KB |
Output is correct |
100 |
Correct |
1 ms |
600 KB |
Output is correct |
101 |
Correct |
1 ms |
600 KB |
Output is correct |
102 |
Correct |
1 ms |
600 KB |
Output is correct |
103 |
Correct |
1 ms |
600 KB |
Output is correct |
104 |
Correct |
662 ms |
15612 KB |
Output is correct |
105 |
Correct |
804 ms |
16856 KB |
Output is correct |
106 |
Correct |
752 ms |
16840 KB |
Output is correct |
107 |
Correct |
798 ms |
16848 KB |
Output is correct |
108 |
Correct |
730 ms |
16856 KB |
Output is correct |
109 |
Correct |
790 ms |
16860 KB |
Output is correct |
110 |
Correct |
779 ms |
16840 KB |
Output is correct |
111 |
Correct |
777 ms |
16840 KB |
Output is correct |
112 |
Correct |
832 ms |
16816 KB |
Output is correct |
113 |
Correct |
723 ms |
16836 KB |
Output is correct |
114 |
Correct |
698 ms |
16856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
348 ms |
9892 KB |
31st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |