#include "towers.h"
#include "bits/stdc++.h"
using namespace std;
#ifndef EVAL
#include "stub.cpp"
#endif
#define ar array
typedef int64_t ll;
//~ #define int ll
const int inf = 1e9 + 7;
const int N = 1e5 + 5;
//~ int first, mount;
//~ vector<int> a, l, r, res;
vector<ar<int, 2>> res;
vector<int> a;
struct ST{
vector<int> tree;
int N;
ST(int N): N(N){
tree.resize(N << 2);
}
void set(int i, int v, int lx, int rx, int x){
if(lx == rx){
tree[x] = v;
return;
}
int m = (lx + rx) >> 1;
if(i <= m) set(i, v, lx, m, x << 1);
else set(i, v, m + 1, rx, x << 1 | 1);
tree[x] = max(tree[x << 1], tree[x << 1 | 1]);
}
void set(int i, int v){
set(i, v, 0, N, 1);
}
int get(int l, int r, int lx, int rx, int x){
if(lx > r || rx < l){
return -(N << 2);
}
if(lx >= l && rx <= r){
return tree[x];
}
int m = (lx + rx) >> 1;
return max(get(l, r, lx, m, x << 1), get(l, r, m + 1, rx, x << 1 | 1));
}
int get(int l, int r){
return get(l, r, 0, N, 1);
}
}Max(N), Min(N);
void init(int n, vector<int> h) {
a = h;
vector<int> pos;
for(int i=0;i<n;i++){
Max.set(i, a[i]);
if((!i || a[i] < a[i - 1]) && (i + 1 == n || a[i] < a[i + 1])){
pos.push_back(i);
}
}
set<int> ss;
multiset<ar<int, 2>> dif;
for(int i=0;i<(int)pos.size();i++){
if(i){
//~ a[pos[i]] a[pos[i - 1]]
int mx = Max.get(pos[i - 1], pos[i]);
dif.insert({mx - a[pos[i - 1]], pos[i - 1]});
dif.insert({mx - a[pos[i]], pos[i]});
}
ss.insert(pos[i]);
}
//~ for(auto x : ss){
//~ cout<<x<<" ";
//~ }
//~ cout<<"\n";
while(!dif.empty()){
auto [d, i] = *dif.begin();
res.push_back({d, i});
//~ cout<<i<<" "<<d<<"\n";
auto it = ss.lower_bound(i);
auto R = it; R++;
if(it == ss.begin() || R == ss.end()){
if(it == ss.begin()){
int mx = Max.get(*it, *R);
dif.erase(dif.find({mx - a[*it], *it}));
dif.erase(dif.find({mx - a[*R], *R}));
} else {
auto L = it; L--;
int mx = Max.get(*L, *it);
dif.erase(dif.find({mx - a[*it], *it}));
dif.erase(dif.find({mx - a[*L], *L}));
}
ss.erase(it);
continue;
}
auto L = it; --L;
int mx = Max.get(*L, *it);
dif.erase(dif.find({mx - a[*it], *it}));
dif.erase(dif.find({mx - a[*L], *L}));
mx = Max.get(*it, *R);
dif.erase(dif.find({mx - a[*it], *it}));
dif.erase(dif.find({mx - a[*R], *R}));
mx = Max.get(*L, *R);
dif.insert({mx - a[*L], *L});
dif.insert({mx - a[*R], *R});
ss.erase(it);
}
res.push_back({inf, *ss.begin()});
//~ for(auto x : res) cout<<x[0]<<" ";
//~ cout<<"\n";
for(int i=1;i<(int)res.size();i++){
assert(res[i - 1][0] <= res[i][0]);
}
}
int max_towers(int l_, int r_, int d) {
int n = a.size();
//~ assert(l_ == 0 && r_ == n - 1);
int j = lower_bound(res.begin(), res.end(), (ar<int, 2>){d, 0}) - res.begin();
return (int)res.size() - j;
}
Compilation message
towers.cpp: In function 'int max_towers(int, int, int)':
towers.cpp:138:6: warning: unused variable 'n' [-Wunused-variable]
138 | int n = a.size();
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
362 ms |
4072 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 |
2 ms |
3408 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 |
2 ms |
3408 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 |
689 ms |
10088 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 |
199 ms |
4984 KB |
Output is correct |
2 |
Correct |
733 ms |
10136 KB |
Output is correct |
3 |
Correct |
884 ms |
10204 KB |
Output is correct |
4 |
Correct |
860 ms |
12576 KB |
Output is correct |
5 |
Correct |
766 ms |
12612 KB |
Output is correct |
6 |
Correct |
850 ms |
12496 KB |
Output is correct |
7 |
Correct |
735 ms |
12536 KB |
Output is correct |
8 |
Correct |
495 ms |
4600 KB |
Output is correct |
9 |
Correct |
585 ms |
4584 KB |
Output is correct |
10 |
Correct |
659 ms |
4564 KB |
Output is correct |
11 |
Correct |
847 ms |
4580 KB |
Output is correct |
12 |
Correct |
104 ms |
10124 KB |
Output is correct |
13 |
Correct |
164 ms |
12584 KB |
Output is correct |
14 |
Correct |
177 ms |
12604 KB |
Output is correct |
15 |
Correct |
22 ms |
4500 KB |
Output is correct |
16 |
Correct |
22 ms |
4576 KB |
Output is correct |
17 |
Correct |
104 ms |
9576 KB |
Output is correct |
18 |
Correct |
112 ms |
10012 KB |
Output is correct |
19 |
Correct |
127 ms |
10056 KB |
Output is correct |
20 |
Correct |
169 ms |
12544 KB |
Output is correct |
21 |
Correct |
185 ms |
12576 KB |
Output is correct |
22 |
Correct |
204 ms |
12584 KB |
Output is correct |
23 |
Correct |
171 ms |
12584 KB |
Output is correct |
24 |
Correct |
25 ms |
4560 KB |
Output is correct |
25 |
Correct |
24 ms |
4584 KB |
Output is correct |
26 |
Correct |
16 ms |
4588 KB |
Output is correct |
27 |
Correct |
15 ms |
4580 KB |
Output is correct |
28 |
Correct |
3 ms |
3536 KB |
Output is correct |
29 |
Correct |
3 ms |
3536 KB |
Output is correct |
30 |
Correct |
3 ms |
3536 KB |
Output is correct |
31 |
Correct |
2 ms |
3408 KB |
Output is correct |
32 |
Correct |
2 ms |
3468 KB |
Output is correct |
33 |
Correct |
2 ms |
3408 KB |
Output is correct |
34 |
Correct |
3 ms |
3580 KB |
Output is correct |
35 |
Correct |
4 ms |
3536 KB |
Output is correct |
36 |
Correct |
3 ms |
3536 KB |
Output is correct |
37 |
Correct |
3 ms |
3564 KB |
Output is correct |
38 |
Correct |
3 ms |
3536 KB |
Output is correct |
39 |
Correct |
3 ms |
3536 KB |
Output is correct |
40 |
Correct |
2 ms |
3408 KB |
Output is correct |
41 |
Correct |
2 ms |
3408 KB |
Output is correct |
42 |
Correct |
2 ms |
3408 KB |
Output is correct |
43 |
Correct |
2 ms |
3408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
3408 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 |
362 ms |
4072 KB |
1st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |