#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
struct ST
{
int l, r;
ST *left, *right;
int mn;
ST() {}
ST(int l, int r, const vector<int>& h): l(l), r(r)
{
if (l < r)
{
left = new ST(l, (l + r) / 2, h);
right = new ST((l + r) / 2 + 1, r, h);
mn = min(left->mn, right->mn);
}
else
{
left = right = NULL;
mn = h[l];
}
}
int get(int x, int y)
{
if (x <= l && r <= y)
return mn;
if (r < x || y < l)
return 2e9;
return min(left->get(x, y), right->get(x, y));
}
} medis;
vector<vector<pair<int, int>>>Fl, Fr;
int N;
vector<int>H;
void init(int N_, vector<int> H_) {
N = N_;
H = H_;
vector<int> p(N);
iota(p.begin(), p.end(), 0);
sort(p.begin(), p.end(), [&](int a, int b) {return H[a] > H[b];});
set<int>X;
medis = ST(0, N - 1, H);
auto get = [&](int l, int r)
{
if (l + 1 >= r)
return -1;
int h = medis.get(l + 1, r - 1);
return max(0, min(H[l] - h, H[r] - h));
};
map<int, vector<pair<int, int>>>L;
map<int, vector<pair<int, int>>>R;
auto add = [&](int l, int r, int d, int w)
{
R[d].push_back({r, w});
L[d].push_back({l, w});
};
for (int i : p)
{
int r = -1, l = -1;
auto it = X.lower_bound(i);
if (it != X.end())
{
r = *it;
}
if (it != X.begin())
{
it--;
l = *it;
}
X.insert(i);
if (l == -1 || r == -1)
{
if (r != -1 && i + 1 < r)
add(i, r, get(i, r), 1);
if (l != -1 && l + 1 < i)
add(l, i, get(l, i), 1);
}
else
{
int d = get(l, r);
int d1 = get(i, r);
int d2 = get(l, i);
if (d1 >= 0 || d2 >= 0)
{
add(i, r, d1, 1);
add(l, i, d2, 1);
assert(max(d1, d2) <= d);
add(l, r, max(d1, d2), -1);
}
}
}
Fl = vector<vector<pair<int, int>>>(N);
Fr = vector<vector<pair<int, int>>>(N);
for (const auto &dd : L)
{
int d = dd.first;
for (pair<int, int>lw : dd.second)
for (int i = lw.first; i < N; i |= i + 1)
{
if (!Fl[i].empty() && Fl[i].back().first == d)
Fl[i].back().second += lw.second;
else
Fl[i].push_back({d, lw.second});
}
}
for (const auto &dd : R)
{
int d = dd.first;
for (pair<int, int>rw : dd.second)
for (int i = rw.first; i < N; i |= i + 1)
{
if (!Fr[i].empty() && Fr[i].back().first == d)
Fr[i].back().second += rw.second;
else
Fr[i].push_back({d, rw.second});
}
}
for (int i = 0; i < N; i++)
{
for (int j = 1; j < (int)Fl[i].size(); j++)
Fl[i][j].second += Fl[i][j - 1].second;
for (int j = 1; j < (int)Fr[i].size(); j++)
Fr[i][j].second += Fr[i][j - 1].second;
}
}
int get(int l, int r, int d)
{
int ret = 0;
int i = r;
while (i >= 0)
{
if (!Fr[i].empty())
{
auto it = lower_bound(Fr[i].begin(), Fr[i].end(), make_pair(d, (int) - 1e9));
ret += Fr[i].back().second;
if (it != Fr[i].begin())
{
it--;
ret -= it->second;
}
}
i = (i & (i + 1)) - 1;
}
i = l - 1;
while (i >= 0) {
if (!Fl[i].empty())
{
auto it = lower_bound(Fl[i].begin(), Fl[i].end(), make_pair(d, (int) - 1e9));
ret -= Fl[i].back().second;
if (it != Fl[i].begin())
{
it--;
ret += it->second;
}
}
i = (i & (i + 1)) - 1;
}
return ret;
}
int max_towers(int L, int R, int D) {
int xx = -1;
int yy = -1;
for (int x = L + 1; x <= R; x++)
{
if (medis.get(L, x - 1) + D <= H[x])
{
xx = x;
break;
}
}
for (int y = R - 1; y >= L; y--)
{
if (medis.get(y + 1, R) + D <= H[y])
{
yy = y;
break;
}
}
if (xx != -1 && yy != -1 && xx <= yy)
return 2 + get(xx, yy, D);
return 1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4018 ms |
12312 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 |
5 ms |
1488 KB |
Output is correct |
3 |
Correct |
5 ms |
1488 KB |
Output is correct |
4 |
Correct |
4 ms |
1360 KB |
Output is correct |
5 |
Correct |
4 ms |
1360 KB |
Output is correct |
6 |
Correct |
4 ms |
1360 KB |
Output is correct |
7 |
Correct |
4 ms |
1360 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
3 ms |
1212 KB |
Output is correct |
11 |
Correct |
3 ms |
1360 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
4 ms |
1384 KB |
Output is correct |
16 |
Correct |
4 ms |
1364 KB |
Output is correct |
17 |
Correct |
4 ms |
1368 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
2 ms |
976 KB |
Output is correct |
20 |
Correct |
5 ms |
1488 KB |
Output is correct |
21 |
Correct |
4 ms |
1360 KB |
Output is correct |
22 |
Correct |
4 ms |
1360 KB |
Output is correct |
23 |
Correct |
1 ms |
592 KB |
Output is correct |
24 |
Correct |
2 ms |
848 KB |
Output is correct |
25 |
Correct |
2 ms |
720 KB |
Output is correct |
26 |
Correct |
5 ms |
1444 KB |
Output is correct |
27 |
Correct |
4 ms |
1488 KB |
Output is correct |
28 |
Correct |
4 ms |
1360 KB |
Output is correct |
29 |
Correct |
4 ms |
1360 KB |
Output is correct |
30 |
Correct |
4 ms |
1360 KB |
Output is correct |
31 |
Correct |
4 ms |
1360 KB |
Output is correct |
32 |
Correct |
1 ms |
592 KB |
Output is correct |
33 |
Correct |
1 ms |
592 KB |
Output is correct |
34 |
Correct |
2 ms |
848 KB |
Output is correct |
35 |
Correct |
3 ms |
1104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
5 ms |
1488 KB |
Output is correct |
3 |
Correct |
5 ms |
1488 KB |
Output is correct |
4 |
Correct |
4 ms |
1360 KB |
Output is correct |
5 |
Correct |
4 ms |
1360 KB |
Output is correct |
6 |
Correct |
4 ms |
1360 KB |
Output is correct |
7 |
Correct |
4 ms |
1360 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
3 ms |
1212 KB |
Output is correct |
11 |
Correct |
3 ms |
1360 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
4 ms |
1384 KB |
Output is correct |
16 |
Correct |
4 ms |
1364 KB |
Output is correct |
17 |
Correct |
4 ms |
1368 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
2 ms |
976 KB |
Output is correct |
20 |
Correct |
5 ms |
1488 KB |
Output is correct |
21 |
Correct |
4 ms |
1360 KB |
Output is correct |
22 |
Correct |
4 ms |
1360 KB |
Output is correct |
23 |
Correct |
1 ms |
592 KB |
Output is correct |
24 |
Correct |
2 ms |
848 KB |
Output is correct |
25 |
Correct |
2 ms |
720 KB |
Output is correct |
26 |
Correct |
5 ms |
1444 KB |
Output is correct |
27 |
Correct |
4 ms |
1488 KB |
Output is correct |
28 |
Correct |
4 ms |
1360 KB |
Output is correct |
29 |
Correct |
4 ms |
1360 KB |
Output is correct |
30 |
Correct |
4 ms |
1360 KB |
Output is correct |
31 |
Correct |
4 ms |
1360 KB |
Output is correct |
32 |
Correct |
1 ms |
592 KB |
Output is correct |
33 |
Correct |
1 ms |
592 KB |
Output is correct |
34 |
Correct |
2 ms |
848 KB |
Output is correct |
35 |
Correct |
3 ms |
1104 KB |
Output is correct |
36 |
Correct |
303 ms |
43380 KB |
Output is correct |
37 |
Correct |
586 ms |
67820 KB |
Output is correct |
38 |
Correct |
527 ms |
67804 KB |
Output is correct |
39 |
Correct |
467 ms |
63244 KB |
Output is correct |
40 |
Correct |
487 ms |
63184 KB |
Output is correct |
41 |
Correct |
472 ms |
63516 KB |
Output is correct |
42 |
Correct |
474 ms |
62976 KB |
Output is correct |
43 |
Correct |
69 ms |
20548 KB |
Output is correct |
44 |
Correct |
62 ms |
20768 KB |
Output is correct |
45 |
Correct |
165 ms |
46072 KB |
Output is correct |
46 |
Correct |
138 ms |
42248 KB |
Output is correct |
47 |
Correct |
517 ms |
67896 KB |
Output is correct |
48 |
Correct |
468 ms |
63032 KB |
Output is correct |
49 |
Correct |
470 ms |
62524 KB |
Output is correct |
50 |
Correct |
59 ms |
20516 KB |
Output is correct |
51 |
Correct |
57 ms |
21736 KB |
Output is correct |
52 |
Correct |
519 ms |
68228 KB |
Output is correct |
53 |
Correct |
466 ms |
62956 KB |
Output is correct |
54 |
Correct |
464 ms |
63156 KB |
Output is correct |
55 |
Correct |
61 ms |
20528 KB |
Output is correct |
56 |
Correct |
187 ms |
49860 KB |
Output is correct |
57 |
Correct |
501 ms |
65260 KB |
Output is correct |
58 |
Correct |
530 ms |
68288 KB |
Output is correct |
59 |
Correct |
527 ms |
67496 KB |
Output is correct |
60 |
Correct |
463 ms |
62964 KB |
Output is correct |
61 |
Correct |
469 ms |
63088 KB |
Output is correct |
62 |
Correct |
464 ms |
62896 KB |
Output is correct |
63 |
Correct |
497 ms |
62872 KB |
Output is correct |
64 |
Correct |
66 ms |
20636 KB |
Output is correct |
65 |
Correct |
64 ms |
20644 KB |
Output is correct |
66 |
Correct |
202 ms |
54128 KB |
Output is correct |
67 |
Correct |
71 ms |
23504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1417 ms |
67784 KB |
Output is correct |
2 |
Correct |
1780 ms |
68280 KB |
Output is correct |
3 |
Correct |
1606 ms |
68004 KB |
Output is correct |
4 |
Correct |
1633 ms |
63420 KB |
Output is correct |
5 |
Correct |
1476 ms |
62840 KB |
Output is correct |
6 |
Correct |
1714 ms |
63288 KB |
Output is correct |
7 |
Correct |
1632 ms |
63220 KB |
Output is correct |
8 |
Execution timed out |
4032 ms |
20636 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
455 ms |
15932 KB |
Output is correct |
2 |
Correct |
2176 ms |
68060 KB |
Output is correct |
3 |
Correct |
1957 ms |
67732 KB |
Output is correct |
4 |
Correct |
2029 ms |
63164 KB |
Output is correct |
5 |
Correct |
1873 ms |
63004 KB |
Output is correct |
6 |
Correct |
1918 ms |
63112 KB |
Output is correct |
7 |
Correct |
1855 ms |
63376 KB |
Output is correct |
8 |
Execution timed out |
4043 ms |
20552 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
5 ms |
1488 KB |
Output is correct |
3 |
Correct |
5 ms |
1488 KB |
Output is correct |
4 |
Correct |
4 ms |
1360 KB |
Output is correct |
5 |
Correct |
4 ms |
1360 KB |
Output is correct |
6 |
Correct |
4 ms |
1360 KB |
Output is correct |
7 |
Correct |
4 ms |
1360 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
3 ms |
1212 KB |
Output is correct |
11 |
Correct |
3 ms |
1360 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
4 ms |
1384 KB |
Output is correct |
16 |
Correct |
4 ms |
1364 KB |
Output is correct |
17 |
Correct |
4 ms |
1368 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
2 ms |
976 KB |
Output is correct |
20 |
Correct |
5 ms |
1488 KB |
Output is correct |
21 |
Correct |
4 ms |
1360 KB |
Output is correct |
22 |
Correct |
4 ms |
1360 KB |
Output is correct |
23 |
Correct |
1 ms |
592 KB |
Output is correct |
24 |
Correct |
2 ms |
848 KB |
Output is correct |
25 |
Correct |
2 ms |
720 KB |
Output is correct |
26 |
Correct |
5 ms |
1444 KB |
Output is correct |
27 |
Correct |
4 ms |
1488 KB |
Output is correct |
28 |
Correct |
4 ms |
1360 KB |
Output is correct |
29 |
Correct |
4 ms |
1360 KB |
Output is correct |
30 |
Correct |
4 ms |
1360 KB |
Output is correct |
31 |
Correct |
4 ms |
1360 KB |
Output is correct |
32 |
Correct |
1 ms |
592 KB |
Output is correct |
33 |
Correct |
1 ms |
592 KB |
Output is correct |
34 |
Correct |
2 ms |
848 KB |
Output is correct |
35 |
Correct |
3 ms |
1104 KB |
Output is correct |
36 |
Correct |
303 ms |
43380 KB |
Output is correct |
37 |
Correct |
586 ms |
67820 KB |
Output is correct |
38 |
Correct |
527 ms |
67804 KB |
Output is correct |
39 |
Correct |
467 ms |
63244 KB |
Output is correct |
40 |
Correct |
487 ms |
63184 KB |
Output is correct |
41 |
Correct |
472 ms |
63516 KB |
Output is correct |
42 |
Correct |
474 ms |
62976 KB |
Output is correct |
43 |
Correct |
69 ms |
20548 KB |
Output is correct |
44 |
Correct |
62 ms |
20768 KB |
Output is correct |
45 |
Correct |
165 ms |
46072 KB |
Output is correct |
46 |
Correct |
138 ms |
42248 KB |
Output is correct |
47 |
Correct |
517 ms |
67896 KB |
Output is correct |
48 |
Correct |
468 ms |
63032 KB |
Output is correct |
49 |
Correct |
470 ms |
62524 KB |
Output is correct |
50 |
Correct |
59 ms |
20516 KB |
Output is correct |
51 |
Correct |
57 ms |
21736 KB |
Output is correct |
52 |
Correct |
519 ms |
68228 KB |
Output is correct |
53 |
Correct |
466 ms |
62956 KB |
Output is correct |
54 |
Correct |
464 ms |
63156 KB |
Output is correct |
55 |
Correct |
61 ms |
20528 KB |
Output is correct |
56 |
Correct |
187 ms |
49860 KB |
Output is correct |
57 |
Correct |
501 ms |
65260 KB |
Output is correct |
58 |
Correct |
530 ms |
68288 KB |
Output is correct |
59 |
Correct |
527 ms |
67496 KB |
Output is correct |
60 |
Correct |
463 ms |
62964 KB |
Output is correct |
61 |
Correct |
469 ms |
63088 KB |
Output is correct |
62 |
Correct |
464 ms |
62896 KB |
Output is correct |
63 |
Correct |
497 ms |
62872 KB |
Output is correct |
64 |
Correct |
66 ms |
20636 KB |
Output is correct |
65 |
Correct |
64 ms |
20644 KB |
Output is correct |
66 |
Correct |
202 ms |
54128 KB |
Output is correct |
67 |
Correct |
71 ms |
23504 KB |
Output is correct |
68 |
Correct |
1417 ms |
67784 KB |
Output is correct |
69 |
Correct |
1780 ms |
68280 KB |
Output is correct |
70 |
Correct |
1606 ms |
68004 KB |
Output is correct |
71 |
Correct |
1633 ms |
63420 KB |
Output is correct |
72 |
Correct |
1476 ms |
62840 KB |
Output is correct |
73 |
Correct |
1714 ms |
63288 KB |
Output is correct |
74 |
Correct |
1632 ms |
63220 KB |
Output is correct |
75 |
Execution timed out |
4032 ms |
20636 KB |
Time limit exceeded |
76 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4018 ms |
12312 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |