#include <bits/stdc++.h>
#include "towers.h"
using namespace std;
typedef pair <vector <int>, vector <int>> pvv;
const int INF = 2e9 + 5;
class Tournament {
int offset;
vector <int> tour;
public:
Tournament(vector <int> vals) {
for (offset = 1; offset < vals.size(); offset *= 2);
tour.resize(2 * offset);
for (int i = 0; i < offset; i++)
tour[i + offset] = i < vals.size() ? vals[i] : INF;
for (int i = offset - 1; i; i--)
tour[i] = min(tour[2 * i], tour[2 * i + 1]);
}
Tournament() {}
int query(int x, int lo, int hi, int from, int to) const {
if (lo >= to || hi <= from)
return INF;
if (lo >= from && hi <= to)
return tour[x];
int mid = (lo + hi) / 2;
return min(query(2 * x, lo, mid, from, to), query(2 * x + 1, mid, hi, from, to));
}
int query(int from, int to) const {
return query(1, 0, offset, from, to);
}
};
int N;
vector <int> H;
map <pair <int, int>, pvv> memo;
void init(int n, vector <int> h) {
N = n;
H = h;
}
int get_smaller(const vector <int> &v, int x) {
return lower_bound(v.begin(), v.end(), x) - v.begin();
}
const pvv& calc(int l, int r) {
pvv& ref = memo[{l, r}];
if (!ref.first.empty() && !ref.second.empty())
return ref;
int n = r - l + 1;
vector <int> h(n + 2);
vector <int> lft(n + 1), rig(n + 1);
h[0] = h[n + 1] = INF;
for (int i = 1; i <= n; i++)
h[i] = H[i + l - 1];
Tournament solver(h);
stack <int> s;
s.push(0);
for (int i = 1; i <= n; i++) {
while (h[s.top()] < h[i])
s.pop();
lft[i] = s.top();
s.push(i);
}
s = stack <int> ();
s.push(n + 1);
for (int i = n; i; i--) {
while (h[s.top()] < h[i])
s.pop();
rig[i] = s.top();
s.push(i);
}
for (int i = 1; i <= n; i++) {
int tmp = solver.query(lft[i] + 1, rig[i]);
ref.first.push_back(h[i] - tmp);
ref.second.push_back(min(h[lft[i]], h[rig[i]]) - tmp);
}
sort(ref.first.begin(), ref.first.end());
sort(ref.second.begin(), ref.second.end());
return ref;
}
int max_towers(int l, int r, int d) {
const pvv& ref = calc(l, r);
return get_smaller(ref.first, d) - get_smaller(ref.second, d);
}
Compilation message
towers.cpp: In constructor 'Tournament::Tournament(std::vector<int>)':
towers.cpp:14:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | for (offset = 1; offset < vals.size(); offset *= 2);
| ~~~~~~~^~~~~~~~~~~~~
towers.cpp:17:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | tour[i + offset] = i < vals.size() ? vals[i] : INF;
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4100 ms |
173036 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 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 |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
0 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
208 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
208 KB |
Output is correct |
16 |
Correct |
2 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
208 KB |
Output is correct |
19 |
Correct |
1 ms |
336 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 |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
208 KB |
Output is correct |
26 |
Correct |
1 ms |
388 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 |
2 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 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 |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
0 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
208 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
208 KB |
Output is correct |
16 |
Correct |
2 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
208 KB |
Output is correct |
19 |
Correct |
1 ms |
336 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 |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
208 KB |
Output is correct |
26 |
Correct |
1 ms |
388 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 |
2 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
392 KB |
Output is correct |
36 |
Correct |
12 ms |
1488 KB |
Output is correct |
37 |
Correct |
29 ms |
2584 KB |
Output is correct |
38 |
Correct |
14 ms |
1480 KB |
Output is correct |
39 |
Correct |
24 ms |
2052 KB |
Output is correct |
40 |
Correct |
13 ms |
1444 KB |
Output is correct |
41 |
Correct |
33 ms |
4252 KB |
Output is correct |
42 |
Correct |
11 ms |
1460 KB |
Output is correct |
43 |
Correct |
21 ms |
3108 KB |
Output is correct |
44 |
Correct |
12 ms |
1480 KB |
Output is correct |
45 |
Correct |
11 ms |
1452 KB |
Output is correct |
46 |
Correct |
17 ms |
1432 KB |
Output is correct |
47 |
Correct |
27 ms |
2632 KB |
Output is correct |
48 |
Correct |
16 ms |
1924 KB |
Output is correct |
49 |
Correct |
12 ms |
1352 KB |
Output is correct |
50 |
Correct |
19 ms |
1940 KB |
Output is correct |
51 |
Correct |
19 ms |
2888 KB |
Output is correct |
52 |
Correct |
47 ms |
4488 KB |
Output is correct |
53 |
Correct |
36 ms |
4508 KB |
Output is correct |
54 |
Correct |
45 ms |
4460 KB |
Output is correct |
55 |
Correct |
32 ms |
4416 KB |
Output is correct |
56 |
Correct |
40 ms |
4776 KB |
Output is correct |
57 |
Correct |
36 ms |
4444 KB |
Output is correct |
58 |
Correct |
46 ms |
4416 KB |
Output is correct |
59 |
Correct |
51 ms |
4500 KB |
Output is correct |
60 |
Correct |
46 ms |
4496 KB |
Output is correct |
61 |
Correct |
35 ms |
4416 KB |
Output is correct |
62 |
Correct |
38 ms |
4504 KB |
Output is correct |
63 |
Correct |
35 ms |
4500 KB |
Output is correct |
64 |
Correct |
31 ms |
4884 KB |
Output is correct |
65 |
Correct |
38 ms |
4376 KB |
Output is correct |
66 |
Correct |
39 ms |
4684 KB |
Output is correct |
67 |
Correct |
34 ms |
4476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4081 ms |
153232 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
307 ms |
1360 KB |
Output is correct |
2 |
Correct |
772 ms |
4440 KB |
Output is correct |
3 |
Correct |
785 ms |
4504 KB |
Output is correct |
4 |
Correct |
854 ms |
4528 KB |
Output is correct |
5 |
Correct |
985 ms |
4444 KB |
Output is correct |
6 |
Correct |
1010 ms |
4504 KB |
Output is correct |
7 |
Correct |
982 ms |
4500 KB |
Output is correct |
8 |
Correct |
916 ms |
5056 KB |
Output is correct |
9 |
Correct |
880 ms |
4416 KB |
Output is correct |
10 |
Correct |
870 ms |
4632 KB |
Output is correct |
11 |
Correct |
985 ms |
4956 KB |
Output is correct |
12 |
Correct |
46 ms |
4524 KB |
Output is correct |
13 |
Correct |
35 ms |
4524 KB |
Output is correct |
14 |
Correct |
42 ms |
4428 KB |
Output is correct |
15 |
Correct |
30 ms |
4484 KB |
Output is correct |
16 |
Correct |
35 ms |
4808 KB |
Output is correct |
17 |
Correct |
41 ms |
4428 KB |
Output is correct |
18 |
Correct |
40 ms |
4524 KB |
Output is correct |
19 |
Correct |
38 ms |
4516 KB |
Output is correct |
20 |
Correct |
36 ms |
4440 KB |
Output is correct |
21 |
Correct |
35 ms |
4524 KB |
Output is correct |
22 |
Correct |
38 ms |
4528 KB |
Output is correct |
23 |
Correct |
46 ms |
4508 KB |
Output is correct |
24 |
Correct |
35 ms |
4832 KB |
Output is correct |
25 |
Correct |
31 ms |
4368 KB |
Output is correct |
26 |
Correct |
39 ms |
4736 KB |
Output is correct |
27 |
Correct |
37 ms |
4444 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 |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
208 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
464 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
1 ms |
336 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 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 |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
0 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
208 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
208 KB |
Output is correct |
16 |
Correct |
2 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
208 KB |
Output is correct |
19 |
Correct |
1 ms |
336 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 |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
208 KB |
Output is correct |
26 |
Correct |
1 ms |
388 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 |
2 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
392 KB |
Output is correct |
36 |
Correct |
12 ms |
1488 KB |
Output is correct |
37 |
Correct |
29 ms |
2584 KB |
Output is correct |
38 |
Correct |
14 ms |
1480 KB |
Output is correct |
39 |
Correct |
24 ms |
2052 KB |
Output is correct |
40 |
Correct |
13 ms |
1444 KB |
Output is correct |
41 |
Correct |
33 ms |
4252 KB |
Output is correct |
42 |
Correct |
11 ms |
1460 KB |
Output is correct |
43 |
Correct |
21 ms |
3108 KB |
Output is correct |
44 |
Correct |
12 ms |
1480 KB |
Output is correct |
45 |
Correct |
11 ms |
1452 KB |
Output is correct |
46 |
Correct |
17 ms |
1432 KB |
Output is correct |
47 |
Correct |
27 ms |
2632 KB |
Output is correct |
48 |
Correct |
16 ms |
1924 KB |
Output is correct |
49 |
Correct |
12 ms |
1352 KB |
Output is correct |
50 |
Correct |
19 ms |
1940 KB |
Output is correct |
51 |
Correct |
19 ms |
2888 KB |
Output is correct |
52 |
Correct |
47 ms |
4488 KB |
Output is correct |
53 |
Correct |
36 ms |
4508 KB |
Output is correct |
54 |
Correct |
45 ms |
4460 KB |
Output is correct |
55 |
Correct |
32 ms |
4416 KB |
Output is correct |
56 |
Correct |
40 ms |
4776 KB |
Output is correct |
57 |
Correct |
36 ms |
4444 KB |
Output is correct |
58 |
Correct |
46 ms |
4416 KB |
Output is correct |
59 |
Correct |
51 ms |
4500 KB |
Output is correct |
60 |
Correct |
46 ms |
4496 KB |
Output is correct |
61 |
Correct |
35 ms |
4416 KB |
Output is correct |
62 |
Correct |
38 ms |
4504 KB |
Output is correct |
63 |
Correct |
35 ms |
4500 KB |
Output is correct |
64 |
Correct |
31 ms |
4884 KB |
Output is correct |
65 |
Correct |
38 ms |
4376 KB |
Output is correct |
66 |
Correct |
39 ms |
4684 KB |
Output is correct |
67 |
Correct |
34 ms |
4476 KB |
Output is correct |
68 |
Execution timed out |
4081 ms |
153232 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4100 ms |
173036 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |