#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
const int MAXK = 20;
using idata = vector<int>;
using igrid = vector<idata>;
idata heights;
int nbTowers;
void init(int N, idata H) {
nbTowers = N;
heights = H;
}
int D0 = -1;
idata jumps;
igrid jumps_2k;
igrid sum_2k;
struct MContainer {
set<pair<int, int>> m;
void append (int node, int height) {
m.insert({ height, node });
auto it = m.find({ height, node });
while (it != m.begin()) {
it --;
m.erase(*it);
it = m.find({ height, node });
}
}
int query (int height) { // find the first node greater
auto it = m.upper_bound({ height, - 1 });
if (it == m.end()) return -1;
return (*it).second;
}
};
int toMiddle (int i) {
if (i == -1) return -1;
return 2 * i + 1;
}
int toNormal (int i) {
if (i == -1) return -1;
return 2 * i;
}
void init (int D) {
// node 2i represents node i as a normal node
// node 2i + 1 represents node i as a middle node
jumps.clear();
jumps.resize(2 * nbTowers, - 1);
jumps_2k.clear();
jumps_2k.resize(2 * nbTowers, idata(MAXK, -1));
sum_2k.clear();
sum_2k.resize(2 * nbTowers, idata(MAXK, 0));
D0 = D;
MContainer container;
MContainer inverted;
for (int i = nbTowers - 1; i >= 0; i --) {
int lnt_mid = container.query( heights[i] + D );
int lnt_lnt = inverted .query( - heights[i] );
int mid_mid = container.query( heights[i] );
int mid_lnt = inverted .query( - heights[i] + D );
jumps[toNormal(i)] = toMiddle(lnt_mid);
if (lnt_mid == -1 || (lnt_lnt != -1 && lnt_lnt < lnt_mid))
jumps[toNormal(i)] = toNormal(lnt_lnt);
jumps[toMiddle(i)] = toNormal(mid_lnt);
if (mid_lnt == -1 || (mid_mid != -1 && mid_mid < mid_lnt))
jumps[toMiddle(i)] = toMiddle(mid_mid);
container.append(i, heights[i]);
inverted.append(i, -heights[i]);
}
for (int i = 0; i < 2 * nbTowers; i ++) {
jumps_2k[i][0] = jumps[i];
if (jumps[i] != -1 && (i & 1) == 1 && (jumps[i] & 1) == 0)
sum_2k[i][0] = 1;
}
for (int k = 0; k + 1 < MAXK; k ++) {
for (int i = 0; i < 2 * nbTowers; i ++) {
sum_2k[i][k + 1] = sum_2k[i][k];
if (jumps_2k[i][k] == -1) continue ;
sum_2k[i][k + 1] += sum_2k[jumps_2k[i][k]][k];
jumps_2k[i][k + 1] = jumps_2k[jumps_2k[i][k]][k];
}
}
}
int find (int node, int jump) {
for (int i = 0; i < MAXK; i ++) {
if ((1 << i) & jump) {
node = jumps_2k[node][i];
if (node == -1) return -1;
}
}
return node;
}
int jump (int node, int max) {
int a = 0;
int b = 2 * nbTowers;
int s = toNormal(node);
max = toMiddle(max);
while (b - a > 1) {
int c = (a + b) >> 1;
int j = find(s, c);
if (j == -1 || j > max) b = c;
else a = c;
}
if (find(s, a) & 1) a --;
int jump_count = a;
int res = 0;
for (int i = 0; i < MAXK; i ++) {
if ((1 << i) & jump_count) {
res += sum_2k[s][i];
s = jumps_2k[s][i];
}
}
return 1 + res;
}
int max_towers(int L, int R, int D) {
if (D != D0) {
init(D);
}
return jump(L, R);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4051 ms |
31532 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
2 ms |
1232 KB |
Output is correct |
3 |
Correct |
2 ms |
1232 KB |
Output is correct |
4 |
Correct |
2 ms |
1232 KB |
Output is correct |
5 |
Correct |
3 ms |
1192 KB |
Output is correct |
6 |
Correct |
2 ms |
1232 KB |
Output is correct |
7 |
Correct |
2 ms |
1232 KB |
Output is correct |
8 |
Correct |
2 ms |
1232 KB |
Output is correct |
9 |
Correct |
2 ms |
1232 KB |
Output is correct |
10 |
Correct |
3 ms |
1360 KB |
Output is correct |
11 |
Correct |
2 ms |
1232 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
2 ms |
1232 KB |
Output is correct |
14 |
Correct |
2 ms |
1232 KB |
Output is correct |
15 |
Correct |
2 ms |
1268 KB |
Output is correct |
16 |
Correct |
2 ms |
1232 KB |
Output is correct |
17 |
Correct |
2 ms |
1232 KB |
Output is correct |
18 |
Correct |
2 ms |
1360 KB |
Output is correct |
19 |
Correct |
2 ms |
1232 KB |
Output is correct |
20 |
Correct |
2 ms |
1232 KB |
Output is correct |
21 |
Correct |
2 ms |
1232 KB |
Output is correct |
22 |
Correct |
2 ms |
1232 KB |
Output is correct |
23 |
Correct |
2 ms |
1360 KB |
Output is correct |
24 |
Correct |
2 ms |
1232 KB |
Output is correct |
25 |
Correct |
1 ms |
720 KB |
Output is correct |
26 |
Correct |
2 ms |
1232 KB |
Output is correct |
27 |
Correct |
2 ms |
1232 KB |
Output is correct |
28 |
Correct |
2 ms |
1232 KB |
Output is correct |
29 |
Correct |
2 ms |
1232 KB |
Output is correct |
30 |
Correct |
2 ms |
1232 KB |
Output is correct |
31 |
Correct |
2 ms |
1232 KB |
Output is correct |
32 |
Correct |
2 ms |
1232 KB |
Output is correct |
33 |
Correct |
2 ms |
1232 KB |
Output is correct |
34 |
Correct |
2 ms |
1232 KB |
Output is correct |
35 |
Correct |
2 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
2 ms |
1232 KB |
Output is correct |
3 |
Correct |
2 ms |
1232 KB |
Output is correct |
4 |
Correct |
2 ms |
1232 KB |
Output is correct |
5 |
Correct |
3 ms |
1192 KB |
Output is correct |
6 |
Correct |
2 ms |
1232 KB |
Output is correct |
7 |
Correct |
2 ms |
1232 KB |
Output is correct |
8 |
Correct |
2 ms |
1232 KB |
Output is correct |
9 |
Correct |
2 ms |
1232 KB |
Output is correct |
10 |
Correct |
3 ms |
1360 KB |
Output is correct |
11 |
Correct |
2 ms |
1232 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
2 ms |
1232 KB |
Output is correct |
14 |
Correct |
2 ms |
1232 KB |
Output is correct |
15 |
Correct |
2 ms |
1268 KB |
Output is correct |
16 |
Correct |
2 ms |
1232 KB |
Output is correct |
17 |
Correct |
2 ms |
1232 KB |
Output is correct |
18 |
Correct |
2 ms |
1360 KB |
Output is correct |
19 |
Correct |
2 ms |
1232 KB |
Output is correct |
20 |
Correct |
2 ms |
1232 KB |
Output is correct |
21 |
Correct |
2 ms |
1232 KB |
Output is correct |
22 |
Correct |
2 ms |
1232 KB |
Output is correct |
23 |
Correct |
2 ms |
1360 KB |
Output is correct |
24 |
Correct |
2 ms |
1232 KB |
Output is correct |
25 |
Correct |
1 ms |
720 KB |
Output is correct |
26 |
Correct |
2 ms |
1232 KB |
Output is correct |
27 |
Correct |
2 ms |
1232 KB |
Output is correct |
28 |
Correct |
2 ms |
1232 KB |
Output is correct |
29 |
Correct |
2 ms |
1232 KB |
Output is correct |
30 |
Correct |
2 ms |
1232 KB |
Output is correct |
31 |
Correct |
2 ms |
1232 KB |
Output is correct |
32 |
Correct |
2 ms |
1232 KB |
Output is correct |
33 |
Correct |
2 ms |
1232 KB |
Output is correct |
34 |
Correct |
2 ms |
1232 KB |
Output is correct |
35 |
Correct |
2 ms |
1236 KB |
Output is correct |
36 |
Correct |
82 ms |
31880 KB |
Output is correct |
37 |
Correct |
138 ms |
48704 KB |
Output is correct |
38 |
Correct |
138 ms |
48820 KB |
Output is correct |
39 |
Correct |
123 ms |
48836 KB |
Output is correct |
40 |
Correct |
127 ms |
48832 KB |
Output is correct |
41 |
Correct |
127 ms |
48828 KB |
Output is correct |
42 |
Correct |
124 ms |
48824 KB |
Output is correct |
43 |
Correct |
149 ms |
53520 KB |
Output is correct |
44 |
Correct |
152 ms |
53516 KB |
Output is correct |
45 |
Correct |
154 ms |
52236 KB |
Output is correct |
46 |
Correct |
153 ms |
51756 KB |
Output is correct |
47 |
Correct |
145 ms |
48804 KB |
Output is correct |
48 |
Correct |
147 ms |
48828 KB |
Output is correct |
49 |
Correct |
126 ms |
48824 KB |
Output is correct |
50 |
Correct |
144 ms |
53620 KB |
Output is correct |
51 |
Correct |
140 ms |
53388 KB |
Output is correct |
52 |
Correct |
134 ms |
48828 KB |
Output is correct |
53 |
Correct |
132 ms |
48824 KB |
Output is correct |
54 |
Correct |
145 ms |
48884 KB |
Output is correct |
55 |
Correct |
150 ms |
53504 KB |
Output is correct |
56 |
Correct |
145 ms |
52984 KB |
Output is correct |
57 |
Correct |
129 ms |
47092 KB |
Output is correct |
58 |
Correct |
144 ms |
48940 KB |
Output is correct |
59 |
Correct |
131 ms |
48704 KB |
Output is correct |
60 |
Correct |
125 ms |
48820 KB |
Output is correct |
61 |
Correct |
128 ms |
48824 KB |
Output is correct |
62 |
Correct |
147 ms |
48824 KB |
Output is correct |
63 |
Correct |
136 ms |
48820 KB |
Output is correct |
64 |
Correct |
146 ms |
53568 KB |
Output is correct |
65 |
Correct |
148 ms |
53440 KB |
Output is correct |
66 |
Correct |
144 ms |
51732 KB |
Output is correct |
67 |
Correct |
152 ms |
53244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1134 ms |
48476 KB |
Output is correct |
2 |
Correct |
1636 ms |
48832 KB |
Output is correct |
3 |
Correct |
1602 ms |
48824 KB |
Output is correct |
4 |
Correct |
1528 ms |
48940 KB |
Output is correct |
5 |
Correct |
1651 ms |
48752 KB |
Output is correct |
6 |
Correct |
1520 ms |
48796 KB |
Output is correct |
7 |
Correct |
1632 ms |
48960 KB |
Output is correct |
8 |
Correct |
1626 ms |
53440 KB |
Output is correct |
9 |
Correct |
1454 ms |
53524 KB |
Output is correct |
10 |
Correct |
1559 ms |
52348 KB |
Output is correct |
11 |
Correct |
1598 ms |
52036 KB |
Output is correct |
12 |
Correct |
1529 ms |
53480 KB |
Output is correct |
13 |
Correct |
1640 ms |
53516 KB |
Output is correct |
14 |
Correct |
0 ms |
208 KB |
Output is correct |
15 |
Correct |
2 ms |
1232 KB |
Output is correct |
16 |
Correct |
2 ms |
1232 KB |
Output is correct |
17 |
Correct |
148 ms |
48824 KB |
Output is correct |
18 |
Correct |
123 ms |
48796 KB |
Output is correct |
19 |
Correct |
122 ms |
48784 KB |
Output is correct |
20 |
Correct |
136 ms |
53516 KB |
Output is correct |
21 |
Correct |
158 ms |
53300 KB |
Output is correct |
22 |
Correct |
124 ms |
48828 KB |
Output is correct |
23 |
Correct |
120 ms |
48824 KB |
Output is correct |
24 |
Correct |
127 ms |
48820 KB |
Output is correct |
25 |
Correct |
157 ms |
53524 KB |
Output is correct |
26 |
Correct |
136 ms |
53008 KB |
Output is correct |
27 |
Correct |
2 ms |
1232 KB |
Output is correct |
28 |
Correct |
2 ms |
1232 KB |
Output is correct |
29 |
Correct |
2 ms |
1232 KB |
Output is correct |
30 |
Correct |
2 ms |
1232 KB |
Output is correct |
31 |
Correct |
2 ms |
1232 KB |
Output is correct |
32 |
Correct |
2 ms |
1232 KB |
Output is correct |
33 |
Correct |
2 ms |
1232 KB |
Output is correct |
34 |
Correct |
2 ms |
1232 KB |
Output is correct |
35 |
Correct |
2 ms |
1232 KB |
Output is correct |
36 |
Correct |
2 ms |
1232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4027 ms |
11828 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
2 ms |
1232 KB |
Output is correct |
3 |
Correct |
2 ms |
1232 KB |
Output is correct |
4 |
Correct |
2 ms |
1232 KB |
Output is correct |
5 |
Correct |
3 ms |
1192 KB |
Output is correct |
6 |
Correct |
2 ms |
1232 KB |
Output is correct |
7 |
Correct |
2 ms |
1232 KB |
Output is correct |
8 |
Correct |
2 ms |
1232 KB |
Output is correct |
9 |
Correct |
2 ms |
1232 KB |
Output is correct |
10 |
Correct |
3 ms |
1360 KB |
Output is correct |
11 |
Correct |
2 ms |
1232 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
2 ms |
1232 KB |
Output is correct |
14 |
Correct |
2 ms |
1232 KB |
Output is correct |
15 |
Correct |
2 ms |
1268 KB |
Output is correct |
16 |
Correct |
2 ms |
1232 KB |
Output is correct |
17 |
Correct |
2 ms |
1232 KB |
Output is correct |
18 |
Correct |
2 ms |
1360 KB |
Output is correct |
19 |
Correct |
2 ms |
1232 KB |
Output is correct |
20 |
Correct |
2 ms |
1232 KB |
Output is correct |
21 |
Correct |
2 ms |
1232 KB |
Output is correct |
22 |
Correct |
2 ms |
1232 KB |
Output is correct |
23 |
Correct |
2 ms |
1360 KB |
Output is correct |
24 |
Correct |
2 ms |
1232 KB |
Output is correct |
25 |
Correct |
1 ms |
720 KB |
Output is correct |
26 |
Correct |
2 ms |
1232 KB |
Output is correct |
27 |
Correct |
2 ms |
1232 KB |
Output is correct |
28 |
Correct |
2 ms |
1232 KB |
Output is correct |
29 |
Correct |
2 ms |
1232 KB |
Output is correct |
30 |
Correct |
2 ms |
1232 KB |
Output is correct |
31 |
Correct |
2 ms |
1232 KB |
Output is correct |
32 |
Correct |
2 ms |
1232 KB |
Output is correct |
33 |
Correct |
2 ms |
1232 KB |
Output is correct |
34 |
Correct |
2 ms |
1232 KB |
Output is correct |
35 |
Correct |
2 ms |
1236 KB |
Output is correct |
36 |
Correct |
82 ms |
31880 KB |
Output is correct |
37 |
Correct |
138 ms |
48704 KB |
Output is correct |
38 |
Correct |
138 ms |
48820 KB |
Output is correct |
39 |
Correct |
123 ms |
48836 KB |
Output is correct |
40 |
Correct |
127 ms |
48832 KB |
Output is correct |
41 |
Correct |
127 ms |
48828 KB |
Output is correct |
42 |
Correct |
124 ms |
48824 KB |
Output is correct |
43 |
Correct |
149 ms |
53520 KB |
Output is correct |
44 |
Correct |
152 ms |
53516 KB |
Output is correct |
45 |
Correct |
154 ms |
52236 KB |
Output is correct |
46 |
Correct |
153 ms |
51756 KB |
Output is correct |
47 |
Correct |
145 ms |
48804 KB |
Output is correct |
48 |
Correct |
147 ms |
48828 KB |
Output is correct |
49 |
Correct |
126 ms |
48824 KB |
Output is correct |
50 |
Correct |
144 ms |
53620 KB |
Output is correct |
51 |
Correct |
140 ms |
53388 KB |
Output is correct |
52 |
Correct |
134 ms |
48828 KB |
Output is correct |
53 |
Correct |
132 ms |
48824 KB |
Output is correct |
54 |
Correct |
145 ms |
48884 KB |
Output is correct |
55 |
Correct |
150 ms |
53504 KB |
Output is correct |
56 |
Correct |
145 ms |
52984 KB |
Output is correct |
57 |
Correct |
129 ms |
47092 KB |
Output is correct |
58 |
Correct |
144 ms |
48940 KB |
Output is correct |
59 |
Correct |
131 ms |
48704 KB |
Output is correct |
60 |
Correct |
125 ms |
48820 KB |
Output is correct |
61 |
Correct |
128 ms |
48824 KB |
Output is correct |
62 |
Correct |
147 ms |
48824 KB |
Output is correct |
63 |
Correct |
136 ms |
48820 KB |
Output is correct |
64 |
Correct |
146 ms |
53568 KB |
Output is correct |
65 |
Correct |
148 ms |
53440 KB |
Output is correct |
66 |
Correct |
144 ms |
51732 KB |
Output is correct |
67 |
Correct |
152 ms |
53244 KB |
Output is correct |
68 |
Correct |
1134 ms |
48476 KB |
Output is correct |
69 |
Correct |
1636 ms |
48832 KB |
Output is correct |
70 |
Correct |
1602 ms |
48824 KB |
Output is correct |
71 |
Correct |
1528 ms |
48940 KB |
Output is correct |
72 |
Correct |
1651 ms |
48752 KB |
Output is correct |
73 |
Correct |
1520 ms |
48796 KB |
Output is correct |
74 |
Correct |
1632 ms |
48960 KB |
Output is correct |
75 |
Correct |
1626 ms |
53440 KB |
Output is correct |
76 |
Correct |
1454 ms |
53524 KB |
Output is correct |
77 |
Correct |
1559 ms |
52348 KB |
Output is correct |
78 |
Correct |
1598 ms |
52036 KB |
Output is correct |
79 |
Correct |
1529 ms |
53480 KB |
Output is correct |
80 |
Correct |
1640 ms |
53516 KB |
Output is correct |
81 |
Correct |
0 ms |
208 KB |
Output is correct |
82 |
Correct |
2 ms |
1232 KB |
Output is correct |
83 |
Correct |
2 ms |
1232 KB |
Output is correct |
84 |
Correct |
148 ms |
48824 KB |
Output is correct |
85 |
Correct |
123 ms |
48796 KB |
Output is correct |
86 |
Correct |
122 ms |
48784 KB |
Output is correct |
87 |
Correct |
136 ms |
53516 KB |
Output is correct |
88 |
Correct |
158 ms |
53300 KB |
Output is correct |
89 |
Correct |
124 ms |
48828 KB |
Output is correct |
90 |
Correct |
120 ms |
48824 KB |
Output is correct |
91 |
Correct |
127 ms |
48820 KB |
Output is correct |
92 |
Correct |
157 ms |
53524 KB |
Output is correct |
93 |
Correct |
136 ms |
53008 KB |
Output is correct |
94 |
Correct |
2 ms |
1232 KB |
Output is correct |
95 |
Correct |
2 ms |
1232 KB |
Output is correct |
96 |
Correct |
2 ms |
1232 KB |
Output is correct |
97 |
Correct |
2 ms |
1232 KB |
Output is correct |
98 |
Correct |
2 ms |
1232 KB |
Output is correct |
99 |
Correct |
2 ms |
1232 KB |
Output is correct |
100 |
Correct |
2 ms |
1232 KB |
Output is correct |
101 |
Correct |
2 ms |
1232 KB |
Output is correct |
102 |
Correct |
2 ms |
1232 KB |
Output is correct |
103 |
Correct |
2 ms |
1232 KB |
Output is correct |
104 |
Correct |
1293 ms |
43380 KB |
Output is correct |
105 |
Correct |
1431 ms |
48824 KB |
Output is correct |
106 |
Correct |
1348 ms |
48904 KB |
Output is correct |
107 |
Correct |
1577 ms |
48824 KB |
Output is correct |
108 |
Correct |
1363 ms |
48768 KB |
Output is correct |
109 |
Correct |
1511 ms |
48712 KB |
Output is correct |
110 |
Correct |
1593 ms |
48840 KB |
Output is correct |
111 |
Correct |
1372 ms |
53504 KB |
Output is correct |
112 |
Correct |
1537 ms |
53472 KB |
Output is correct |
113 |
Correct |
995 ms |
52548 KB |
Output is correct |
114 |
Correct |
1615 ms |
53028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4051 ms |
31532 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |