#include "towers.h"
#include "bits/stdc++.h"
using namespace std;
#ifndef EVAL
#include "stub.cpp"
#endif
const int N = 1e5 + 5;
int first, mount;
vector<int> a, l, r, res;
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) {
first = 1;
a = H;
vector<int> pos_mnt;
for(int i=0;i<n;i++){
int is = 0;
if(!i || a[i] > a[i - 1]) is++;
if(i + 1 == n || a[i] > a[i + 1]) is++;
if(is){
pos_mnt.push_back(i);
}
}
if((int)pos_mnt.size() == 1) mount = pos_mnt[0] + 1;
l.resize(n), r.resize(n);
ST Max(n), Min(n);
}
int max_towers(int l_, int r_, int d) {
if(mount){
int p = mount - 1;
if(l_ < p && p < r_ && a[l_] + d <= a[p] && a[r_] + d <= a[p]){
return 2;
}
return 1;
}
if(first){
int n = a.size();
vector<int> ss;
for(int i=0;i<n;i++){
while(!ss.empty() && a[ss.back()] < a[i]) ss.pop_back();
int l_ = 0, r_ = ss.size();
while(l_ < r_){
int m = (l_ + r_) >> 1;
if(a[ss[m]] >= a[i] + d) l_ = m + 1;
else r_ = m;
}
l[i] = 0;
if(l_) l[i] = ss[l_ - 1] + 1;
ss.push_back(i);
}
ss.clear();
for(int i=n-1;~i;i--){
while(!ss.empty() && a[ss.back()] < a[i]) ss.pop_back();
int l_ = 0, r_ = ss.size();
while(l_ < r_){
int m = (l_ + r_) >> 1;
if(a[ss[m]] >= a[i] + d) l_ = m + 1;
else r_ = m;
}
r[i] = n - 1;
if(l_) r[i] = ss[l_ - 1] - 1;
ss.push_back(i);
}
vector<int> p(n);
iota(p.begin(), p.end(), 0);
sort(p.begin(), p.end(), [&](int i, int j){
if(r[i] != r[j]) return r[i] < r[j];
if(l[i] != l[j]) return l[i] > l[j];
return a[i] < a[j];
});
int mxL = -1;
for(auto i : p){
if(l[i] <= mxL){
continue;
}
mxL = max(mxL, l[i]);
res.push_back(i);
}
for(int i=0;i<n;i++){
Min.set(i, -r[i]);
Max.set(i, l[i]);
}
first = 0;
}
int L = lower_bound(res.begin(), res.end(), l_) - res.begin();
int R = upper_bound(res.begin(), res.end(), r_) - res.begin() - 1;
if(L <= R){
int ans = R - L + 1;
if(l_ < l[res[L]]){
if(-Min.get(l_, l[res[L]] - 1) < l[res[L]]){
ans++;
}
}
if(r[res[R]] < r_){
if(r[res[R]] < Max.get(r[res[R]] + 1, r_)){
ans++;
}
}
return ans;
} else {
int mnR = -Min.get(l_, r_);
int mxL = Max.get(l_, r_);
if(mnR < mxL){
return 2;
}
return 1;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
352 ms |
6732 KB |
31st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3408 KB |
Output is correct |
2 |
Correct |
2 ms |
3536 KB |
Output is correct |
3 |
Correct |
2 ms |
3548 KB |
Output is correct |
4 |
Correct |
2 ms |
3536 KB |
Output is correct |
5 |
Correct |
2 ms |
3536 KB |
Output is correct |
6 |
Correct |
2 ms |
3536 KB |
Output is correct |
7 |
Correct |
2 ms |
3544 KB |
Output is correct |
8 |
Correct |
2 ms |
3536 KB |
Output is correct |
9 |
Correct |
2 ms |
3536 KB |
Output is correct |
10 |
Correct |
2 ms |
3536 KB |
Output is correct |
11 |
Correct |
2 ms |
3536 KB |
Output is correct |
12 |
Correct |
1 ms |
3408 KB |
Output is correct |
13 |
Correct |
2 ms |
3536 KB |
Output is correct |
14 |
Correct |
2 ms |
3536 KB |
Output is correct |
15 |
Correct |
2 ms |
3536 KB |
Output is correct |
16 |
Correct |
2 ms |
3536 KB |
Output is correct |
17 |
Correct |
3 ms |
3536 KB |
Output is correct |
18 |
Correct |
2 ms |
3536 KB |
Output is correct |
19 |
Correct |
2 ms |
3536 KB |
Output is correct |
20 |
Correct |
2 ms |
3536 KB |
Output is correct |
21 |
Correct |
2 ms |
3536 KB |
Output is correct |
22 |
Correct |
2 ms |
3536 KB |
Output is correct |
23 |
Correct |
2 ms |
3536 KB |
Output is correct |
24 |
Correct |
2 ms |
3536 KB |
Output is correct |
25 |
Correct |
2 ms |
3408 KB |
Output is correct |
26 |
Correct |
2 ms |
3536 KB |
Output is correct |
27 |
Correct |
2 ms |
3536 KB |
Output is correct |
28 |
Correct |
2 ms |
3552 KB |
Output is correct |
29 |
Correct |
2 ms |
3536 KB |
Output is correct |
30 |
Correct |
2 ms |
3536 KB |
Output is correct |
31 |
Correct |
3 ms |
3536 KB |
Output is correct |
32 |
Correct |
3 ms |
3536 KB |
Output is correct |
33 |
Correct |
2 ms |
3536 KB |
Output is correct |
34 |
Correct |
2 ms |
3536 KB |
Output is correct |
35 |
Correct |
2 ms |
3536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3408 KB |
Output is correct |
2 |
Correct |
2 ms |
3536 KB |
Output is correct |
3 |
Correct |
2 ms |
3548 KB |
Output is correct |
4 |
Correct |
2 ms |
3536 KB |
Output is correct |
5 |
Correct |
2 ms |
3536 KB |
Output is correct |
6 |
Correct |
2 ms |
3536 KB |
Output is correct |
7 |
Correct |
2 ms |
3544 KB |
Output is correct |
8 |
Correct |
2 ms |
3536 KB |
Output is correct |
9 |
Correct |
2 ms |
3536 KB |
Output is correct |
10 |
Correct |
2 ms |
3536 KB |
Output is correct |
11 |
Correct |
2 ms |
3536 KB |
Output is correct |
12 |
Correct |
1 ms |
3408 KB |
Output is correct |
13 |
Correct |
2 ms |
3536 KB |
Output is correct |
14 |
Correct |
2 ms |
3536 KB |
Output is correct |
15 |
Correct |
2 ms |
3536 KB |
Output is correct |
16 |
Correct |
2 ms |
3536 KB |
Output is correct |
17 |
Correct |
3 ms |
3536 KB |
Output is correct |
18 |
Correct |
2 ms |
3536 KB |
Output is correct |
19 |
Correct |
2 ms |
3536 KB |
Output is correct |
20 |
Correct |
2 ms |
3536 KB |
Output is correct |
21 |
Correct |
2 ms |
3536 KB |
Output is correct |
22 |
Correct |
2 ms |
3536 KB |
Output is correct |
23 |
Correct |
2 ms |
3536 KB |
Output is correct |
24 |
Correct |
2 ms |
3536 KB |
Output is correct |
25 |
Correct |
2 ms |
3408 KB |
Output is correct |
26 |
Correct |
2 ms |
3536 KB |
Output is correct |
27 |
Correct |
2 ms |
3536 KB |
Output is correct |
28 |
Correct |
2 ms |
3552 KB |
Output is correct |
29 |
Correct |
2 ms |
3536 KB |
Output is correct |
30 |
Correct |
2 ms |
3536 KB |
Output is correct |
31 |
Correct |
3 ms |
3536 KB |
Output is correct |
32 |
Correct |
3 ms |
3536 KB |
Output is correct |
33 |
Correct |
2 ms |
3536 KB |
Output is correct |
34 |
Correct |
2 ms |
3536 KB |
Output is correct |
35 |
Correct |
2 ms |
3536 KB |
Output is correct |
36 |
Correct |
28 ms |
6988 KB |
Output is correct |
37 |
Correct |
38 ms |
8816 KB |
Output is correct |
38 |
Correct |
36 ms |
8800 KB |
Output is correct |
39 |
Correct |
43 ms |
8820 KB |
Output is correct |
40 |
Correct |
40 ms |
8812 KB |
Output is correct |
41 |
Correct |
43 ms |
8792 KB |
Output is correct |
42 |
Correct |
31 ms |
8812 KB |
Output is correct |
43 |
Correct |
32 ms |
8936 KB |
Output is correct |
44 |
Correct |
28 ms |
8936 KB |
Output is correct |
45 |
Correct |
34 ms |
8940 KB |
Output is correct |
46 |
Correct |
37 ms |
8932 KB |
Output is correct |
47 |
Correct |
31 ms |
8904 KB |
Output is correct |
48 |
Correct |
37 ms |
8808 KB |
Output is correct |
49 |
Correct |
36 ms |
8808 KB |
Output is correct |
50 |
Correct |
27 ms |
8936 KB |
Output is correct |
51 |
Correct |
26 ms |
9024 KB |
Output is correct |
52 |
Correct |
38 ms |
8804 KB |
Output is correct |
53 |
Correct |
35 ms |
8800 KB |
Output is correct |
54 |
Correct |
29 ms |
8780 KB |
Output is correct |
55 |
Correct |
32 ms |
8944 KB |
Output is correct |
56 |
Correct |
33 ms |
8936 KB |
Output is correct |
57 |
Correct |
36 ms |
8652 KB |
Output is correct |
58 |
Correct |
42 ms |
8784 KB |
Output is correct |
59 |
Correct |
46 ms |
8904 KB |
Output is correct |
60 |
Correct |
33 ms |
8812 KB |
Output is correct |
61 |
Correct |
33 ms |
8808 KB |
Output is correct |
62 |
Correct |
34 ms |
8804 KB |
Output is correct |
63 |
Correct |
32 ms |
8800 KB |
Output is correct |
64 |
Correct |
30 ms |
9032 KB |
Output is correct |
65 |
Correct |
28 ms |
8944 KB |
Output is correct |
66 |
Correct |
36 ms |
8920 KB |
Output is correct |
67 |
Correct |
29 ms |
8924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
539 ms |
8816 KB |
Output is correct |
2 |
Correct |
682 ms |
8904 KB |
Output is correct |
3 |
Correct |
685 ms |
8904 KB |
Output is correct |
4 |
Correct |
605 ms |
8812 KB |
Output is correct |
5 |
Correct |
717 ms |
8792 KB |
Output is correct |
6 |
Correct |
721 ms |
8808 KB |
Output is correct |
7 |
Correct |
706 ms |
8808 KB |
Output is correct |
8 |
Correct |
739 ms |
9032 KB |
Output is correct |
9 |
Correct |
725 ms |
8924 KB |
Output is correct |
10 |
Correct |
708 ms |
8944 KB |
Output is correct |
11 |
Correct |
531 ms |
9032 KB |
Output is correct |
12 |
Correct |
789 ms |
8932 KB |
Output is correct |
13 |
Correct |
710 ms |
8928 KB |
Output is correct |
14 |
Correct |
1 ms |
3408 KB |
Output is correct |
15 |
Correct |
2 ms |
3536 KB |
Output is correct |
16 |
Correct |
2 ms |
3536 KB |
Output is correct |
17 |
Correct |
31 ms |
8804 KB |
Output is correct |
18 |
Correct |
38 ms |
8812 KB |
Output is correct |
19 |
Correct |
37 ms |
8808 KB |
Output is correct |
20 |
Correct |
29 ms |
8928 KB |
Output is correct |
21 |
Correct |
27 ms |
9024 KB |
Output is correct |
22 |
Correct |
38 ms |
8804 KB |
Output is correct |
23 |
Correct |
31 ms |
8780 KB |
Output is correct |
24 |
Correct |
29 ms |
8808 KB |
Output is correct |
25 |
Correct |
26 ms |
9032 KB |
Output is correct |
26 |
Correct |
33 ms |
8988 KB |
Output is correct |
27 |
Correct |
2 ms |
3536 KB |
Output is correct |
28 |
Correct |
2 ms |
3536 KB |
Output is correct |
29 |
Correct |
3 ms |
3536 KB |
Output is correct |
30 |
Correct |
2 ms |
3536 KB |
Output is correct |
31 |
Correct |
2 ms |
3536 KB |
Output is correct |
32 |
Correct |
2 ms |
3536 KB |
Output is correct |
33 |
Correct |
2 ms |
3536 KB |
Output is correct |
34 |
Correct |
2 ms |
3536 KB |
Output is correct |
35 |
Correct |
2 ms |
3556 KB |
Output is correct |
36 |
Correct |
2 ms |
3536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
223 ms |
4688 KB |
2nd lines differ - on the 1st token, expected: '7063', found: '7197' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3408 KB |
Output is correct |
2 |
Correct |
2 ms |
3536 KB |
Output is correct |
3 |
Correct |
2 ms |
3548 KB |
Output is correct |
4 |
Correct |
2 ms |
3536 KB |
Output is correct |
5 |
Correct |
2 ms |
3536 KB |
Output is correct |
6 |
Correct |
2 ms |
3536 KB |
Output is correct |
7 |
Correct |
2 ms |
3544 KB |
Output is correct |
8 |
Correct |
2 ms |
3536 KB |
Output is correct |
9 |
Correct |
2 ms |
3536 KB |
Output is correct |
10 |
Correct |
2 ms |
3536 KB |
Output is correct |
11 |
Correct |
2 ms |
3536 KB |
Output is correct |
12 |
Correct |
1 ms |
3408 KB |
Output is correct |
13 |
Correct |
2 ms |
3536 KB |
Output is correct |
14 |
Correct |
2 ms |
3536 KB |
Output is correct |
15 |
Correct |
2 ms |
3536 KB |
Output is correct |
16 |
Correct |
2 ms |
3536 KB |
Output is correct |
17 |
Correct |
3 ms |
3536 KB |
Output is correct |
18 |
Correct |
2 ms |
3536 KB |
Output is correct |
19 |
Correct |
2 ms |
3536 KB |
Output is correct |
20 |
Correct |
2 ms |
3536 KB |
Output is correct |
21 |
Correct |
2 ms |
3536 KB |
Output is correct |
22 |
Correct |
2 ms |
3536 KB |
Output is correct |
23 |
Correct |
2 ms |
3536 KB |
Output is correct |
24 |
Correct |
2 ms |
3536 KB |
Output is correct |
25 |
Correct |
2 ms |
3408 KB |
Output is correct |
26 |
Correct |
2 ms |
3536 KB |
Output is correct |
27 |
Correct |
2 ms |
3536 KB |
Output is correct |
28 |
Correct |
2 ms |
3552 KB |
Output is correct |
29 |
Correct |
2 ms |
3536 KB |
Output is correct |
30 |
Correct |
2 ms |
3536 KB |
Output is correct |
31 |
Correct |
3 ms |
3536 KB |
Output is correct |
32 |
Correct |
3 ms |
3536 KB |
Output is correct |
33 |
Correct |
2 ms |
3536 KB |
Output is correct |
34 |
Correct |
2 ms |
3536 KB |
Output is correct |
35 |
Correct |
2 ms |
3536 KB |
Output is correct |
36 |
Correct |
28 ms |
6988 KB |
Output is correct |
37 |
Correct |
38 ms |
8816 KB |
Output is correct |
38 |
Correct |
36 ms |
8800 KB |
Output is correct |
39 |
Correct |
43 ms |
8820 KB |
Output is correct |
40 |
Correct |
40 ms |
8812 KB |
Output is correct |
41 |
Correct |
43 ms |
8792 KB |
Output is correct |
42 |
Correct |
31 ms |
8812 KB |
Output is correct |
43 |
Correct |
32 ms |
8936 KB |
Output is correct |
44 |
Correct |
28 ms |
8936 KB |
Output is correct |
45 |
Correct |
34 ms |
8940 KB |
Output is correct |
46 |
Correct |
37 ms |
8932 KB |
Output is correct |
47 |
Correct |
31 ms |
8904 KB |
Output is correct |
48 |
Correct |
37 ms |
8808 KB |
Output is correct |
49 |
Correct |
36 ms |
8808 KB |
Output is correct |
50 |
Correct |
27 ms |
8936 KB |
Output is correct |
51 |
Correct |
26 ms |
9024 KB |
Output is correct |
52 |
Correct |
38 ms |
8804 KB |
Output is correct |
53 |
Correct |
35 ms |
8800 KB |
Output is correct |
54 |
Correct |
29 ms |
8780 KB |
Output is correct |
55 |
Correct |
32 ms |
8944 KB |
Output is correct |
56 |
Correct |
33 ms |
8936 KB |
Output is correct |
57 |
Correct |
36 ms |
8652 KB |
Output is correct |
58 |
Correct |
42 ms |
8784 KB |
Output is correct |
59 |
Correct |
46 ms |
8904 KB |
Output is correct |
60 |
Correct |
33 ms |
8812 KB |
Output is correct |
61 |
Correct |
33 ms |
8808 KB |
Output is correct |
62 |
Correct |
34 ms |
8804 KB |
Output is correct |
63 |
Correct |
32 ms |
8800 KB |
Output is correct |
64 |
Correct |
30 ms |
9032 KB |
Output is correct |
65 |
Correct |
28 ms |
8944 KB |
Output is correct |
66 |
Correct |
36 ms |
8920 KB |
Output is correct |
67 |
Correct |
29 ms |
8924 KB |
Output is correct |
68 |
Correct |
539 ms |
8816 KB |
Output is correct |
69 |
Correct |
682 ms |
8904 KB |
Output is correct |
70 |
Correct |
685 ms |
8904 KB |
Output is correct |
71 |
Correct |
605 ms |
8812 KB |
Output is correct |
72 |
Correct |
717 ms |
8792 KB |
Output is correct |
73 |
Correct |
721 ms |
8808 KB |
Output is correct |
74 |
Correct |
706 ms |
8808 KB |
Output is correct |
75 |
Correct |
739 ms |
9032 KB |
Output is correct |
76 |
Correct |
725 ms |
8924 KB |
Output is correct |
77 |
Correct |
708 ms |
8944 KB |
Output is correct |
78 |
Correct |
531 ms |
9032 KB |
Output is correct |
79 |
Correct |
789 ms |
8932 KB |
Output is correct |
80 |
Correct |
710 ms |
8928 KB |
Output is correct |
81 |
Correct |
1 ms |
3408 KB |
Output is correct |
82 |
Correct |
2 ms |
3536 KB |
Output is correct |
83 |
Correct |
2 ms |
3536 KB |
Output is correct |
84 |
Correct |
31 ms |
8804 KB |
Output is correct |
85 |
Correct |
38 ms |
8812 KB |
Output is correct |
86 |
Correct |
37 ms |
8808 KB |
Output is correct |
87 |
Correct |
29 ms |
8928 KB |
Output is correct |
88 |
Correct |
27 ms |
9024 KB |
Output is correct |
89 |
Correct |
38 ms |
8804 KB |
Output is correct |
90 |
Correct |
31 ms |
8780 KB |
Output is correct |
91 |
Correct |
29 ms |
8808 KB |
Output is correct |
92 |
Correct |
26 ms |
9032 KB |
Output is correct |
93 |
Correct |
33 ms |
8988 KB |
Output is correct |
94 |
Correct |
2 ms |
3536 KB |
Output is correct |
95 |
Correct |
2 ms |
3536 KB |
Output is correct |
96 |
Correct |
3 ms |
3536 KB |
Output is correct |
97 |
Correct |
2 ms |
3536 KB |
Output is correct |
98 |
Correct |
2 ms |
3536 KB |
Output is correct |
99 |
Correct |
2 ms |
3536 KB |
Output is correct |
100 |
Correct |
2 ms |
3536 KB |
Output is correct |
101 |
Correct |
2 ms |
3536 KB |
Output is correct |
102 |
Correct |
2 ms |
3556 KB |
Output is correct |
103 |
Correct |
2 ms |
3536 KB |
Output is correct |
104 |
Correct |
634 ms |
8248 KB |
Output is correct |
105 |
Correct |
769 ms |
8896 KB |
Output is correct |
106 |
Correct |
603 ms |
8812 KB |
Output is correct |
107 |
Correct |
773 ms |
8772 KB |
Output is correct |
108 |
Correct |
750 ms |
8780 KB |
Output is correct |
109 |
Correct |
726 ms |
8820 KB |
Output is correct |
110 |
Correct |
655 ms |
8804 KB |
Output is correct |
111 |
Correct |
681 ms |
8944 KB |
Output is correct |
112 |
Correct |
693 ms |
9032 KB |
Output is correct |
113 |
Correct |
696 ms |
9032 KB |
Output is correct |
114 |
Correct |
713 ms |
8932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
352 ms |
6732 KB |
31st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |