#include "towers.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
const int INF = 2'000'000'000;
vector<int> H;
int N;
class segTree{
public:
int lef_ind;
int rig_ind;
int min_val;
int max_val;
segTree* lef_child;
segTree* rig_child;
segTree(int l, int r)
{
lef_ind = l;
rig_ind = r;
min_val = INF;
max_val = -1;
if (l!=r)
{
int mid = (l+r)/2;
lef_child = new segTree(l, mid);
rig_child = new segTree(mid+1, r);
}
else
{
lef_child = nullptr;
rig_child = nullptr;
}
};
void update(int ind, int val)
{
if (lef_child == nullptr)
{
min_val = min(min_val, val);
max_val = max(max_val, val);
}
else
{
int mid = (lef_ind+rig_ind)/2;
if (ind<=mid)
lef_child->update(ind, val);
else
rig_child->update(ind, val);
min_val = min(lef_child->min_val, rig_child->min_val);
max_val = max(lef_child->max_val, rig_child->max_val);
}
}
int mini(int l, int r)
{
int lef_overlap = max(l, lef_ind);
int rig_overlap = min(r, rig_ind);
if ((lef_ind==lef_overlap) and (rig_ind==rig_overlap))
return min_val;
if (lef_overlap > rig_overlap)
return INF;
return min(lef_child->mini(l, r), rig_child->mini(l, r));
}
int maxi(int l, int r)
{
int lef_overlap = max(l, lef_ind);
int rig_overlap = min(r, rig_ind);
if ((lef_ind==lef_overlap) and (rig_ind==rig_overlap))
return max_val;
if (lef_overlap > rig_overlap)
return 0;
return max(lef_child->maxi(l, r), rig_child->maxi(l, r));
}
};
void init(int n, std::vector<int> h) {
H = h;
N = n;
}
int max_towers(int L, int R, int D) {
segTree lo = segTree(1, 100000);
segTree hi = segTree(0, 100000);
lo.update(1, H[L]);
hi.update(0, H[L]);
int ans = 1;
for (int i=L+1; i<=R; i++)
{
//cout << i << '\n';
//go high
int bin_lo = 1;
int bin_hi = i-L;//perhaps edit to 100'000?
int target = H[i]-D;
//cout << lo.mini(bin_lo, bin_hi) << " is the value\n";
if (lo.mini(bin_lo, bin_hi) > target)
{
bin_lo = 0;
bin_hi = 0;
}
while (bin_lo != bin_hi)
{
int bin_mid = (bin_lo + bin_hi + 1)/2;
if (lo.mini(bin_mid, bin_hi) <= target)
bin_lo = bin_mid;
else
bin_hi = bin_mid-1;
}
int ans_one = bin_lo;
//cout << "reached halfway\n";
//go low
bin_lo = 0;
bin_hi = i-L;//perhaps edit to 100'000?
target = H[i]+D;
if (hi.maxi(bin_lo, bin_hi) < target)
{
bin_lo = 0;
bin_hi = 0;
}
while (bin_lo != bin_hi)
{
int bin_mid = (bin_lo + bin_hi + 1)/2;
if (hi.maxi(bin_mid, bin_hi) >= target)
bin_lo = bin_mid;
else
bin_hi = bin_mid-1;
}
int ans_two = bin_lo+1;
lo.update(ans_two, H[i]);
hi.update(ans_one, H[i]);
ans = max(ans, ans_two);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3160 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
19032 KB |
Output is correct |
2 |
Correct |
16 ms |
19148 KB |
Output is correct |
3 |
Correct |
14 ms |
19032 KB |
Output is correct |
4 |
Correct |
15 ms |
19032 KB |
Output is correct |
5 |
Correct |
15 ms |
19196 KB |
Output is correct |
6 |
Correct |
15 ms |
19132 KB |
Output is correct |
7 |
Correct |
14 ms |
19032 KB |
Output is correct |
8 |
Correct |
13 ms |
19032 KB |
Output is correct |
9 |
Correct |
13 ms |
19032 KB |
Output is correct |
10 |
Correct |
13 ms |
19032 KB |
Output is correct |
11 |
Correct |
14 ms |
19100 KB |
Output is correct |
12 |
Correct |
13 ms |
19032 KB |
Output is correct |
13 |
Correct |
16 ms |
19032 KB |
Output is correct |
14 |
Correct |
14 ms |
19032 KB |
Output is correct |
15 |
Correct |
13 ms |
19032 KB |
Output is correct |
16 |
Correct |
16 ms |
19228 KB |
Output is correct |
17 |
Correct |
14 ms |
19032 KB |
Output is correct |
18 |
Correct |
14 ms |
18984 KB |
Output is correct |
19 |
Correct |
14 ms |
19032 KB |
Output is correct |
20 |
Correct |
18 ms |
19032 KB |
Output is correct |
21 |
Correct |
18 ms |
19032 KB |
Output is correct |
22 |
Correct |
18 ms |
19020 KB |
Output is correct |
23 |
Correct |
15 ms |
19032 KB |
Output is correct |
24 |
Correct |
16 ms |
19052 KB |
Output is correct |
25 |
Correct |
16 ms |
19032 KB |
Output is correct |
26 |
Correct |
15 ms |
19032 KB |
Output is correct |
27 |
Correct |
16 ms |
19032 KB |
Output is correct |
28 |
Correct |
16 ms |
19032 KB |
Output is correct |
29 |
Correct |
16 ms |
19020 KB |
Output is correct |
30 |
Correct |
17 ms |
19032 KB |
Output is correct |
31 |
Correct |
15 ms |
19224 KB |
Output is correct |
32 |
Correct |
15 ms |
19192 KB |
Output is correct |
33 |
Correct |
16 ms |
19032 KB |
Output is correct |
34 |
Correct |
14 ms |
19160 KB |
Output is correct |
35 |
Correct |
16 ms |
19032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
19032 KB |
Output is correct |
2 |
Correct |
16 ms |
19148 KB |
Output is correct |
3 |
Correct |
14 ms |
19032 KB |
Output is correct |
4 |
Correct |
15 ms |
19032 KB |
Output is correct |
5 |
Correct |
15 ms |
19196 KB |
Output is correct |
6 |
Correct |
15 ms |
19132 KB |
Output is correct |
7 |
Correct |
14 ms |
19032 KB |
Output is correct |
8 |
Correct |
13 ms |
19032 KB |
Output is correct |
9 |
Correct |
13 ms |
19032 KB |
Output is correct |
10 |
Correct |
13 ms |
19032 KB |
Output is correct |
11 |
Correct |
14 ms |
19100 KB |
Output is correct |
12 |
Correct |
13 ms |
19032 KB |
Output is correct |
13 |
Correct |
16 ms |
19032 KB |
Output is correct |
14 |
Correct |
14 ms |
19032 KB |
Output is correct |
15 |
Correct |
13 ms |
19032 KB |
Output is correct |
16 |
Correct |
16 ms |
19228 KB |
Output is correct |
17 |
Correct |
14 ms |
19032 KB |
Output is correct |
18 |
Correct |
14 ms |
18984 KB |
Output is correct |
19 |
Correct |
14 ms |
19032 KB |
Output is correct |
20 |
Correct |
18 ms |
19032 KB |
Output is correct |
21 |
Correct |
18 ms |
19032 KB |
Output is correct |
22 |
Correct |
18 ms |
19020 KB |
Output is correct |
23 |
Correct |
15 ms |
19032 KB |
Output is correct |
24 |
Correct |
16 ms |
19052 KB |
Output is correct |
25 |
Correct |
16 ms |
19032 KB |
Output is correct |
26 |
Correct |
15 ms |
19032 KB |
Output is correct |
27 |
Correct |
16 ms |
19032 KB |
Output is correct |
28 |
Correct |
16 ms |
19032 KB |
Output is correct |
29 |
Correct |
16 ms |
19020 KB |
Output is correct |
30 |
Correct |
17 ms |
19032 KB |
Output is correct |
31 |
Correct |
15 ms |
19224 KB |
Output is correct |
32 |
Correct |
15 ms |
19192 KB |
Output is correct |
33 |
Correct |
16 ms |
19032 KB |
Output is correct |
34 |
Correct |
14 ms |
19160 KB |
Output is correct |
35 |
Correct |
16 ms |
19032 KB |
Output is correct |
36 |
Correct |
76 ms |
19716 KB |
Output is correct |
37 |
Correct |
58 ms |
19920 KB |
Output is correct |
38 |
Correct |
48 ms |
19920 KB |
Output is correct |
39 |
Correct |
37 ms |
19912 KB |
Output is correct |
40 |
Correct |
51 ms |
19920 KB |
Output is correct |
41 |
Correct |
204 ms |
19984 KB |
Output is correct |
42 |
Correct |
25 ms |
19912 KB |
Output is correct |
43 |
Correct |
86 ms |
19920 KB |
Output is correct |
44 |
Correct |
29 ms |
19852 KB |
Output is correct |
45 |
Correct |
24 ms |
19920 KB |
Output is correct |
46 |
Correct |
37 ms |
19896 KB |
Output is correct |
47 |
Correct |
137 ms |
20168 KB |
Output is correct |
48 |
Correct |
102 ms |
19920 KB |
Output is correct |
49 |
Correct |
47 ms |
19912 KB |
Output is correct |
50 |
Correct |
57 ms |
19920 KB |
Output is correct |
51 |
Correct |
78 ms |
19764 KB |
Output is correct |
52 |
Correct |
408 ms |
20172 KB |
Output is correct |
53 |
Correct |
509 ms |
20168 KB |
Output is correct |
54 |
Correct |
417 ms |
19988 KB |
Output is correct |
55 |
Correct |
174 ms |
19892 KB |
Output is correct |
56 |
Correct |
258 ms |
19920 KB |
Output is correct |
57 |
Correct |
180 ms |
19912 KB |
Output is correct |
58 |
Correct |
244 ms |
19984 KB |
Output is correct |
59 |
Correct |
306 ms |
19920 KB |
Output is correct |
60 |
Correct |
408 ms |
19988 KB |
Output is correct |
61 |
Correct |
331 ms |
19920 KB |
Output is correct |
62 |
Correct |
270 ms |
19912 KB |
Output is correct |
63 |
Correct |
351 ms |
19984 KB |
Output is correct |
64 |
Correct |
120 ms |
19912 KB |
Output is correct |
65 |
Correct |
115 ms |
19896 KB |
Output is correct |
66 |
Correct |
196 ms |
19920 KB |
Output is correct |
67 |
Correct |
119 ms |
19984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4094 ms |
616504 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4114 ms |
1315448 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
19032 KB |
Output is correct |
2 |
Correct |
16 ms |
19148 KB |
Output is correct |
3 |
Correct |
14 ms |
19032 KB |
Output is correct |
4 |
Correct |
15 ms |
19032 KB |
Output is correct |
5 |
Correct |
15 ms |
19196 KB |
Output is correct |
6 |
Correct |
15 ms |
19132 KB |
Output is correct |
7 |
Correct |
14 ms |
19032 KB |
Output is correct |
8 |
Correct |
13 ms |
19032 KB |
Output is correct |
9 |
Correct |
13 ms |
19032 KB |
Output is correct |
10 |
Correct |
13 ms |
19032 KB |
Output is correct |
11 |
Correct |
14 ms |
19100 KB |
Output is correct |
12 |
Correct |
13 ms |
19032 KB |
Output is correct |
13 |
Correct |
16 ms |
19032 KB |
Output is correct |
14 |
Correct |
14 ms |
19032 KB |
Output is correct |
15 |
Correct |
13 ms |
19032 KB |
Output is correct |
16 |
Correct |
16 ms |
19228 KB |
Output is correct |
17 |
Correct |
14 ms |
19032 KB |
Output is correct |
18 |
Correct |
14 ms |
18984 KB |
Output is correct |
19 |
Correct |
14 ms |
19032 KB |
Output is correct |
20 |
Correct |
18 ms |
19032 KB |
Output is correct |
21 |
Correct |
18 ms |
19032 KB |
Output is correct |
22 |
Correct |
18 ms |
19020 KB |
Output is correct |
23 |
Correct |
15 ms |
19032 KB |
Output is correct |
24 |
Correct |
16 ms |
19052 KB |
Output is correct |
25 |
Correct |
16 ms |
19032 KB |
Output is correct |
26 |
Correct |
15 ms |
19032 KB |
Output is correct |
27 |
Correct |
16 ms |
19032 KB |
Output is correct |
28 |
Correct |
16 ms |
19032 KB |
Output is correct |
29 |
Correct |
16 ms |
19020 KB |
Output is correct |
30 |
Correct |
17 ms |
19032 KB |
Output is correct |
31 |
Correct |
15 ms |
19224 KB |
Output is correct |
32 |
Correct |
15 ms |
19192 KB |
Output is correct |
33 |
Correct |
16 ms |
19032 KB |
Output is correct |
34 |
Correct |
14 ms |
19160 KB |
Output is correct |
35 |
Correct |
16 ms |
19032 KB |
Output is correct |
36 |
Correct |
76 ms |
19716 KB |
Output is correct |
37 |
Correct |
58 ms |
19920 KB |
Output is correct |
38 |
Correct |
48 ms |
19920 KB |
Output is correct |
39 |
Correct |
37 ms |
19912 KB |
Output is correct |
40 |
Correct |
51 ms |
19920 KB |
Output is correct |
41 |
Correct |
204 ms |
19984 KB |
Output is correct |
42 |
Correct |
25 ms |
19912 KB |
Output is correct |
43 |
Correct |
86 ms |
19920 KB |
Output is correct |
44 |
Correct |
29 ms |
19852 KB |
Output is correct |
45 |
Correct |
24 ms |
19920 KB |
Output is correct |
46 |
Correct |
37 ms |
19896 KB |
Output is correct |
47 |
Correct |
137 ms |
20168 KB |
Output is correct |
48 |
Correct |
102 ms |
19920 KB |
Output is correct |
49 |
Correct |
47 ms |
19912 KB |
Output is correct |
50 |
Correct |
57 ms |
19920 KB |
Output is correct |
51 |
Correct |
78 ms |
19764 KB |
Output is correct |
52 |
Correct |
408 ms |
20172 KB |
Output is correct |
53 |
Correct |
509 ms |
20168 KB |
Output is correct |
54 |
Correct |
417 ms |
19988 KB |
Output is correct |
55 |
Correct |
174 ms |
19892 KB |
Output is correct |
56 |
Correct |
258 ms |
19920 KB |
Output is correct |
57 |
Correct |
180 ms |
19912 KB |
Output is correct |
58 |
Correct |
244 ms |
19984 KB |
Output is correct |
59 |
Correct |
306 ms |
19920 KB |
Output is correct |
60 |
Correct |
408 ms |
19988 KB |
Output is correct |
61 |
Correct |
331 ms |
19920 KB |
Output is correct |
62 |
Correct |
270 ms |
19912 KB |
Output is correct |
63 |
Correct |
351 ms |
19984 KB |
Output is correct |
64 |
Correct |
120 ms |
19912 KB |
Output is correct |
65 |
Correct |
115 ms |
19896 KB |
Output is correct |
66 |
Correct |
196 ms |
19920 KB |
Output is correct |
67 |
Correct |
119 ms |
19984 KB |
Output is correct |
68 |
Execution timed out |
4094 ms |
616504 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3160 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |