// In the name of Allah
#include <bits/stdc++.h>
#include "towers.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define F first
#define S second
#define all(x) (x).begin(),(x).end()
#define len(x) ((ll) (x).size())
#define Mp make_pair
#define pb push_back
#define endl '\n'
#define sep ' '
const int maxn = 2e5 + 7;
const int maxlg = 20;
const ll oo = 1e9 + 7;
int n;
ll A[maxn]; int L[maxn], R[maxn];
int rmq[maxn][maxlg];
vector<ll> arr; ll sm[maxn];
int GI(ll x) {
return lower_bound(all(arr), x) - arr.begin();
}
ll get_max(int l, int r) {
if (l >= r) return -oo;
int j = __lg(r - l);
return max(rmq[l][j], rmq[r - (1 << j)][j]);
}
void init(int N, vector<int> H) {
n = N;
for (int i = 0; i < n; i++) A[i] = H[i];
for (int i = n - 1; i >= 0; i--) {
rmq[i][0] = A[i];
for (int j = 1; j < maxlg; j++) {
if (i + (1 << j) - 1 >= n) break;
rmq[i][j] = max(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
}
}
for (int i = 0; i < n; i++) {
for (L[i] = i - 1; L[i] != -1 && A[i] <= A[L[i]]; L[i] = L[L[i]]);
}
for (int i = n - 1; i >= 0; i--) {
for (R[i] = i + 1; R[i] != n && A[i] <= A[R[i]]; R[i] = R[R[i]]);
}
arr.pb(oo);
for (int i = 0; i < n; i++) {
if (L[i] != -1) {
ll valx = get_max(L[i] + 1, i + 1) - A[i];
if (valx >= 1) arr.pb(valx);
}
if (R[i] != n) {
ll valx = get_max(i, R[i]) - A[i];
if (valx >= 1) arr.pb(valx);
}
}
sort(all(arr)); arr.resize(unique(all(arr)) - arr.begin());
for (int i = 0; i < n; i++) {
ll x = oo;
if (L[i] != -1) x = min(x, get_max(L[i] + 1, i + 1) - A[i]);
if (R[i] != n) x = min(x, get_max(i, R[i]) - A[i]);
if (x == 0) continue;
int j = GI(x); sm[j]++;
}
for (int j = len(arr) - 2; j >= 0; j--) sm[j] += sm[j + 1];
}
int max_towers(int L, int R, int D) {
int j = lower_bound(all(arr), D) - arr.begin();
return sm[j];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
259 ms |
7396 KB |
1st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
1st lines differ - on the 1st token, expected: '13', found: '131' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
1st lines differ - on the 1st token, expected: '13', found: '131' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
350 ms |
13104 KB |
1st lines differ - on the 1st token, expected: '11903', found: '33010' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
168 ms |
4716 KB |
Output is correct |
2 |
Correct |
526 ms |
12228 KB |
Output is correct |
3 |
Correct |
469 ms |
13008 KB |
Output is correct |
4 |
Correct |
551 ms |
13008 KB |
Output is correct |
5 |
Correct |
572 ms |
13000 KB |
Output is correct |
6 |
Correct |
572 ms |
12236 KB |
Output is correct |
7 |
Correct |
468 ms |
13008 KB |
Output is correct |
8 |
Correct |
522 ms |
10584 KB |
Output is correct |
9 |
Correct |
504 ms |
11864 KB |
Output is correct |
10 |
Correct |
547 ms |
13008 KB |
Output is correct |
11 |
Correct |
541 ms |
11976 KB |
Output is correct |
12 |
Correct |
25 ms |
12236 KB |
Output is correct |
13 |
Correct |
31 ms |
13004 KB |
Output is correct |
14 |
Correct |
26 ms |
13008 KB |
Output is correct |
15 |
Correct |
13 ms |
11864 KB |
Output is correct |
16 |
Correct |
15 ms |
12752 KB |
Output is correct |
17 |
Correct |
34 ms |
12724 KB |
Output is correct |
18 |
Correct |
31 ms |
13000 KB |
Output is correct |
19 |
Correct |
24 ms |
13088 KB |
Output is correct |
20 |
Correct |
25 ms |
13008 KB |
Output is correct |
21 |
Correct |
26 ms |
13008 KB |
Output is correct |
22 |
Correct |
25 ms |
13008 KB |
Output is correct |
23 |
Correct |
25 ms |
13032 KB |
Output is correct |
24 |
Correct |
13 ms |
10584 KB |
Output is correct |
25 |
Correct |
19 ms |
10584 KB |
Output is correct |
26 |
Correct |
21 ms |
11824 KB |
Output is correct |
27 |
Correct |
14 ms |
12120 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
600 KB |
Output is correct |
30 |
Correct |
1 ms |
600 KB |
Output is correct |
31 |
Correct |
0 ms |
600 KB |
Output is correct |
32 |
Correct |
0 ms |
600 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
36 |
Correct |
1 ms |
600 KB |
Output is correct |
37 |
Correct |
1 ms |
600 KB |
Output is correct |
38 |
Correct |
0 ms |
600 KB |
Output is correct |
39 |
Correct |
1 ms |
600 KB |
Output is correct |
40 |
Correct |
1 ms |
600 KB |
Output is correct |
41 |
Correct |
1 ms |
2392 KB |
Output is correct |
42 |
Correct |
1 ms |
2392 KB |
Output is correct |
43 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
1st lines differ - on the 1st token, expected: '13', found: '131' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
259 ms |
7396 KB |
1st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |