#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
const int INF = 1e9;
int n;
vi H;
vi St, Dr;
vi DeltaSt, DeltaDr;
struct RMQ {
int n;
vector<vi> A;
RMQ(vi V0) : n((int)V0.size()) {
A.push_back(V0);
for(int k = 1; (1 << k) <= n; ++k) {
A.push_back(A.back());
for(int i = 0; i < n; ++i) {
if(i + (1 << (k - 1)) < n)
A[k][i] = max(A[k][i], A[k - 1][i + (1 << (k - 1))]);
}
}
}
int query(int l, int r) {
int d = 31 - __builtin_clz(r - l + 1);
return max(A[d][l], A[d][r - (1 << d) + 1]);
}
};
pair<vi, vi> descomp(vi A) {
RMQ R(A);
vi Ant(n, -1);
vi Delta(n, INF); /// cel mai mare delta posibil
stack<int> S;
for(int i = 0; i < n; ++i) {
while(!S.empty() && A[S.top()] > A[i]) {
S.pop();
}
if(!S.empty()) {
Ant[i] = S.top();
Delta[i] = R.query(S.top(), i) - A[i];
}
S.push(i);
}
return make_pair(Ant, Delta);
}
vi S;
const int MN = 100000;
struct AIB2D {
int n;
vi V[4 * MN];
AIB2D() {}
AIB2D(const vi &V0) : n((int)V0.size()) {
build(V0, 1, 0, n - 1);
}
void build(const vi &V0, int u, int s, int d) {
for(int i = s; i <= d; ++i)
V[u].push_back(V0[i]);
sort(V[u].begin(), V[u].end());
if(s != d) {
build(V0, u << 1, s, (s + d) >> 1);
build(V0, u << 1 | 1, ((s + d) >> 1) + 1, d);
}
}
int query(int l, int r, int D, int u, int s, int d) {
if(d < l || r < s) return 0;
if(l <= s && d <= r) {
int v = lower_bound(V[u].begin(), V[u].end(), D) - V[u].begin();
return int(V[u].size()) - v;
}
return query(l, r, D, u << 1, s, (s + d) >> 1)
+ query(l, r, D, u << 1 | 1, ((s + d) >> 1) + 1, d);
}
int query(int l, int r, int D) {
return query(l, r, D, 1, 0, n - 1);
}
};
AIB2D ASt, ADr;
AIB2D ATot;
void init(int N0, vi H0) {
n = N0;
H = H0;
tie(St, DeltaSt) = descomp(H);
reverse(H.begin(), H.end());
tie(Dr, DeltaDr) = descomp(H);
reverse(H.begin(), H.end());
reverse(Dr.begin(), Dr.end());
reverse(DeltaDr.begin(), DeltaDr.end());
for(auto &it : Dr)
it = n - it - 1;
S.resize(n);
for(int i = 0; i < n; ++i)
S[i] = min(DeltaSt[i], DeltaDr[i]);
ASt = AIB2D(DeltaSt);
ADr = AIB2D(DeltaDr);
ATot = AIB2D(S);
}
int max_towers(int l, int r, int D) {
vi Sol;
for(int i = l; i <= r; ++i) {
if(S[i] >= D) {
Sol.push_back(i);
}
}
if(ATot.query(l, r, D) == 0) {
///poate gasim un SolDr >= D si dupa un SolSt >= D
int st = l, dr = r, mij;
if(ADr.query(l, r, D) == 0) return 1;
while(st < dr) {
mij = (st + dr) / 2;
if(ADr.query(l, r, D)) dr = mij;
else st = mij + 1;
}
if(ASt.query(st + 1, r, D)) return 2;
return 1;
}
int st, dr, mij;
int p1;
st = l, dr = r;
while(st < dr) {
mij = (st + dr) / 2;
if(ATot.query(l, mij, D)) dr = mij;
else st = mij + 1;
}
p1 = st;
int p2;
st = l; dr = r;
while(st < dr) {
mij = (st + dr + 1) / 2;
if(ATot.query(mij, r, D)) st = mij;
else dr = mij - 1;
}
p2 = st;
int found_st = 0, found_dr = 0;
found_st = !!ADr.query(l, p1 - 1, D);
found_dr = !!ASt.query(p2 + 1, r, D);
return int(Sol.size()) + found_st + found_dr;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1562 ms |
60848 KB |
Output is correct |
2 |
Execution timed out |
4050 ms |
80704 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
38096 KB |
Output is correct |
2 |
Correct |
27 ms |
38436 KB |
Output is correct |
3 |
Correct |
27 ms |
38488 KB |
Output is correct |
4 |
Correct |
37 ms |
38580 KB |
Output is correct |
5 |
Correct |
24 ms |
38488 KB |
Output is correct |
6 |
Correct |
25 ms |
38528 KB |
Output is correct |
7 |
Correct |
25 ms |
38488 KB |
Output is correct |
8 |
Correct |
25 ms |
38580 KB |
Output is correct |
9 |
Correct |
25 ms |
38488 KB |
Output is correct |
10 |
Correct |
24 ms |
38484 KB |
Output is correct |
11 |
Correct |
26 ms |
38480 KB |
Output is correct |
12 |
Correct |
33 ms |
37936 KB |
Output is correct |
13 |
Correct |
28 ms |
38488 KB |
Output is correct |
14 |
Correct |
37 ms |
38480 KB |
Output is correct |
15 |
Correct |
36 ms |
38480 KB |
Output is correct |
16 |
Correct |
24 ms |
38488 KB |
Output is correct |
17 |
Correct |
33 ms |
38480 KB |
Output is correct |
18 |
Correct |
23 ms |
38488 KB |
Output is correct |
19 |
Correct |
36 ms |
38488 KB |
Output is correct |
20 |
Correct |
23 ms |
38732 KB |
Output is correct |
21 |
Correct |
26 ms |
38480 KB |
Output is correct |
22 |
Correct |
25 ms |
38488 KB |
Output is correct |
23 |
Correct |
24 ms |
38488 KB |
Output is correct |
24 |
Correct |
22 ms |
38740 KB |
Output is correct |
25 |
Correct |
34 ms |
38232 KB |
Output is correct |
26 |
Correct |
39 ms |
38488 KB |
Output is correct |
27 |
Correct |
38 ms |
38404 KB |
Output is correct |
28 |
Correct |
26 ms |
38588 KB |
Output is correct |
29 |
Correct |
29 ms |
38488 KB |
Output is correct |
30 |
Correct |
31 ms |
38488 KB |
Output is correct |
31 |
Correct |
40 ms |
38428 KB |
Output is correct |
32 |
Correct |
31 ms |
38480 KB |
Output is correct |
33 |
Correct |
26 ms |
38488 KB |
Output is correct |
34 |
Correct |
37 ms |
38488 KB |
Output is correct |
35 |
Correct |
24 ms |
38488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
38096 KB |
Output is correct |
2 |
Correct |
27 ms |
38436 KB |
Output is correct |
3 |
Correct |
27 ms |
38488 KB |
Output is correct |
4 |
Correct |
37 ms |
38580 KB |
Output is correct |
5 |
Correct |
24 ms |
38488 KB |
Output is correct |
6 |
Correct |
25 ms |
38528 KB |
Output is correct |
7 |
Correct |
25 ms |
38488 KB |
Output is correct |
8 |
Correct |
25 ms |
38580 KB |
Output is correct |
9 |
Correct |
25 ms |
38488 KB |
Output is correct |
10 |
Correct |
24 ms |
38484 KB |
Output is correct |
11 |
Correct |
26 ms |
38480 KB |
Output is correct |
12 |
Correct |
33 ms |
37936 KB |
Output is correct |
13 |
Correct |
28 ms |
38488 KB |
Output is correct |
14 |
Correct |
37 ms |
38480 KB |
Output is correct |
15 |
Correct |
36 ms |
38480 KB |
Output is correct |
16 |
Correct |
24 ms |
38488 KB |
Output is correct |
17 |
Correct |
33 ms |
38480 KB |
Output is correct |
18 |
Correct |
23 ms |
38488 KB |
Output is correct |
19 |
Correct |
36 ms |
38488 KB |
Output is correct |
20 |
Correct |
23 ms |
38732 KB |
Output is correct |
21 |
Correct |
26 ms |
38480 KB |
Output is correct |
22 |
Correct |
25 ms |
38488 KB |
Output is correct |
23 |
Correct |
24 ms |
38488 KB |
Output is correct |
24 |
Correct |
22 ms |
38740 KB |
Output is correct |
25 |
Correct |
34 ms |
38232 KB |
Output is correct |
26 |
Correct |
39 ms |
38488 KB |
Output is correct |
27 |
Correct |
38 ms |
38404 KB |
Output is correct |
28 |
Correct |
26 ms |
38588 KB |
Output is correct |
29 |
Correct |
29 ms |
38488 KB |
Output is correct |
30 |
Correct |
31 ms |
38488 KB |
Output is correct |
31 |
Correct |
40 ms |
38428 KB |
Output is correct |
32 |
Correct |
31 ms |
38480 KB |
Output is correct |
33 |
Correct |
26 ms |
38488 KB |
Output is correct |
34 |
Correct |
37 ms |
38488 KB |
Output is correct |
35 |
Correct |
24 ms |
38488 KB |
Output is correct |
36 |
Correct |
154 ms |
62232 KB |
Output is correct |
37 |
Correct |
267 ms |
80680 KB |
Output is correct |
38 |
Correct |
227 ms |
80680 KB |
Output is correct |
39 |
Correct |
257 ms |
80680 KB |
Output is correct |
40 |
Correct |
230 ms |
80576 KB |
Output is correct |
41 |
Correct |
261 ms |
80684 KB |
Output is correct |
42 |
Correct |
251 ms |
80696 KB |
Output is correct |
43 |
Correct |
150 ms |
80688 KB |
Output is correct |
44 |
Correct |
142 ms |
80664 KB |
Output is correct |
45 |
Correct |
147 ms |
80772 KB |
Output is correct |
46 |
Correct |
170 ms |
80620 KB |
Output is correct |
47 |
Correct |
252 ms |
80524 KB |
Output is correct |
48 |
Correct |
210 ms |
80684 KB |
Output is correct |
49 |
Correct |
252 ms |
80672 KB |
Output is correct |
50 |
Correct |
191 ms |
80724 KB |
Output is correct |
51 |
Correct |
172 ms |
80684 KB |
Output is correct |
52 |
Correct |
265 ms |
80620 KB |
Output is correct |
53 |
Correct |
234 ms |
80680 KB |
Output is correct |
54 |
Correct |
278 ms |
80680 KB |
Output is correct |
55 |
Correct |
146 ms |
80652 KB |
Output is correct |
56 |
Correct |
173 ms |
80676 KB |
Output is correct |
57 |
Correct |
208 ms |
79632 KB |
Output is correct |
58 |
Correct |
211 ms |
80560 KB |
Output is correct |
59 |
Correct |
271 ms |
80684 KB |
Output is correct |
60 |
Correct |
224 ms |
80684 KB |
Output is correct |
61 |
Correct |
231 ms |
80676 KB |
Output is correct |
62 |
Correct |
236 ms |
80664 KB |
Output is correct |
63 |
Correct |
256 ms |
80744 KB |
Output is correct |
64 |
Correct |
141 ms |
80680 KB |
Output is correct |
65 |
Correct |
156 ms |
80600 KB |
Output is correct |
66 |
Correct |
157 ms |
80704 KB |
Output is correct |
67 |
Correct |
163 ms |
80544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4091 ms |
80560 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2542 ms |
47716 KB |
Output is correct |
2 |
Execution timed out |
4018 ms |
80628 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
38096 KB |
Output is correct |
2 |
Correct |
27 ms |
38436 KB |
Output is correct |
3 |
Correct |
27 ms |
38488 KB |
Output is correct |
4 |
Correct |
37 ms |
38580 KB |
Output is correct |
5 |
Correct |
24 ms |
38488 KB |
Output is correct |
6 |
Correct |
25 ms |
38528 KB |
Output is correct |
7 |
Correct |
25 ms |
38488 KB |
Output is correct |
8 |
Correct |
25 ms |
38580 KB |
Output is correct |
9 |
Correct |
25 ms |
38488 KB |
Output is correct |
10 |
Correct |
24 ms |
38484 KB |
Output is correct |
11 |
Correct |
26 ms |
38480 KB |
Output is correct |
12 |
Correct |
33 ms |
37936 KB |
Output is correct |
13 |
Correct |
28 ms |
38488 KB |
Output is correct |
14 |
Correct |
37 ms |
38480 KB |
Output is correct |
15 |
Correct |
36 ms |
38480 KB |
Output is correct |
16 |
Correct |
24 ms |
38488 KB |
Output is correct |
17 |
Correct |
33 ms |
38480 KB |
Output is correct |
18 |
Correct |
23 ms |
38488 KB |
Output is correct |
19 |
Correct |
36 ms |
38488 KB |
Output is correct |
20 |
Correct |
23 ms |
38732 KB |
Output is correct |
21 |
Correct |
26 ms |
38480 KB |
Output is correct |
22 |
Correct |
25 ms |
38488 KB |
Output is correct |
23 |
Correct |
24 ms |
38488 KB |
Output is correct |
24 |
Correct |
22 ms |
38740 KB |
Output is correct |
25 |
Correct |
34 ms |
38232 KB |
Output is correct |
26 |
Correct |
39 ms |
38488 KB |
Output is correct |
27 |
Correct |
38 ms |
38404 KB |
Output is correct |
28 |
Correct |
26 ms |
38588 KB |
Output is correct |
29 |
Correct |
29 ms |
38488 KB |
Output is correct |
30 |
Correct |
31 ms |
38488 KB |
Output is correct |
31 |
Correct |
40 ms |
38428 KB |
Output is correct |
32 |
Correct |
31 ms |
38480 KB |
Output is correct |
33 |
Correct |
26 ms |
38488 KB |
Output is correct |
34 |
Correct |
37 ms |
38488 KB |
Output is correct |
35 |
Correct |
24 ms |
38488 KB |
Output is correct |
36 |
Correct |
154 ms |
62232 KB |
Output is correct |
37 |
Correct |
267 ms |
80680 KB |
Output is correct |
38 |
Correct |
227 ms |
80680 KB |
Output is correct |
39 |
Correct |
257 ms |
80680 KB |
Output is correct |
40 |
Correct |
230 ms |
80576 KB |
Output is correct |
41 |
Correct |
261 ms |
80684 KB |
Output is correct |
42 |
Correct |
251 ms |
80696 KB |
Output is correct |
43 |
Correct |
150 ms |
80688 KB |
Output is correct |
44 |
Correct |
142 ms |
80664 KB |
Output is correct |
45 |
Correct |
147 ms |
80772 KB |
Output is correct |
46 |
Correct |
170 ms |
80620 KB |
Output is correct |
47 |
Correct |
252 ms |
80524 KB |
Output is correct |
48 |
Correct |
210 ms |
80684 KB |
Output is correct |
49 |
Correct |
252 ms |
80672 KB |
Output is correct |
50 |
Correct |
191 ms |
80724 KB |
Output is correct |
51 |
Correct |
172 ms |
80684 KB |
Output is correct |
52 |
Correct |
265 ms |
80620 KB |
Output is correct |
53 |
Correct |
234 ms |
80680 KB |
Output is correct |
54 |
Correct |
278 ms |
80680 KB |
Output is correct |
55 |
Correct |
146 ms |
80652 KB |
Output is correct |
56 |
Correct |
173 ms |
80676 KB |
Output is correct |
57 |
Correct |
208 ms |
79632 KB |
Output is correct |
58 |
Correct |
211 ms |
80560 KB |
Output is correct |
59 |
Correct |
271 ms |
80684 KB |
Output is correct |
60 |
Correct |
224 ms |
80684 KB |
Output is correct |
61 |
Correct |
231 ms |
80676 KB |
Output is correct |
62 |
Correct |
236 ms |
80664 KB |
Output is correct |
63 |
Correct |
256 ms |
80744 KB |
Output is correct |
64 |
Correct |
141 ms |
80680 KB |
Output is correct |
65 |
Correct |
156 ms |
80600 KB |
Output is correct |
66 |
Correct |
157 ms |
80704 KB |
Output is correct |
67 |
Correct |
163 ms |
80544 KB |
Output is correct |
68 |
Execution timed out |
4091 ms |
80560 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1562 ms |
60848 KB |
Output is correct |
2 |
Execution timed out |
4050 ms |
80704 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |