#include "towers.h"
#include <bits/stdc++.h>
using std::vector;
using std::string;
using std::cin;
using std::cout;
using ll = long long;
using vi = vector<ll>;
using vii = vector<vi>;
using pii = std::pair<ll,ll>;
#define ln "\n"
#define rep(i,j,k) for(ll i=ll(j); i<ll(k); i++)
#define REP(i,j,k) for(ll i=ll(j); i<=ll(k); i++)
#define per(i,j,k) for(ll i=ll(j); i>=ll(k); i--)
#define all(a) a.begin(), a.end()
#define mp std::make_pair
#define pb emplace_back
constexpr ll inf = (1ll<<60);
ll N;
vi H;
ll D;
vi right, left;
constexpr ll di = 30;
ll sz;
vii pr, mr, pl, ml;
vii sum;
void init(int n, std::vector<int> h) {
N = n;
H.resize(N);
right.resize(N);
left.resize(N);
rep(i,0,N) H[i] = h[i];
D = -1;
}
int max_towers(int L, int R, int d) {
if(D == -1){
D = d;
{
sz = N/di+100;
sum.resize(sz, vi(sz));
pr.resize(N+2);
mr.resize(N+2);
pl.resize(N+2);
ml.resize(N+2);
std::deque<ll> ima, vma, imi, vmi;
ima.pb(-3);
vma.pb(-inf);
imi.pb(-2);
vmi.pb(inf);
rep(i,0,N){
ll large = lower_bound(all(vmi), H[i]+D)-vmi.begin();
large = imi[large];
ll small = lower_bound(all(vma),H[i])-vma.begin()-1;
small = ima[small];
if(small >= large) left[i] = small+1;
else left[i] = -1;
while(true){
if(vma[vma.size()-1] > H[i]) vma.pop_back(), ima.pop_back();
else break;
}
vma.pb(H[i]);
ima.pb(i);
while(true){
if(vmi[0] < H[i]) vmi.pop_front(), imi.pop_front();
else break;
}
vmi.emplace_front(H[i]);
imi.emplace_front(i);
}
}
{
std::deque<ll> ima, vma, imi, vmi;
ima.pb(N+3);
vma.pb(-inf);
imi.pb(N+2);
vmi.pb(inf);
per(i,N-1,0){
ll large = lower_bound(all(vmi), H[i]+D)-vmi.begin();
large = imi[large];
ll small = lower_bound(all(vma),H[i])-vma.begin()-1;
small = ima[small];
if(small <= large) right[i] = small-1;
else right[i] = N;
while(true){
if(vma[vma.size()-1] > H[i]) vma.pop_back(), ima.pop_back();
else break;
}
vma.pb(H[i]);
ima.pb(i);
while(true){
if(vmi[0] < H[i]) vmi.pop_front(), imi.pop_front();
else break;
}
vmi.emplace_front(H[i]);
imi.emplace_front(i);
}
}
rep(i,0,N){
left[i]++;
right[i]++;
ll ld = (left[i]+di-1)/di;
ll rd = right[i]/di;
sum[ld][rd]++;
ll id = i/di;
sum[ld][id]--;
id = (i+2+di-1)/di;
sum[id][rd]--;
pr[right[i]].pb(left[i]);
pl[left[i]].pb(right[i]);
ml[i+2].pb(right[i]);
ml[left[i]].pb(i);
mr[i].pb(left[i]);
mr[right[i]].pb(i+2);
}
per(l,sz,1){
rep(i,0,sz){
ll j = i+l;
if(j >= sz) break;
if(i) sum[i][j] += sum[i-1][j];
if(j+1 < sz) sum[i][j] += sum[i][j+1];
if(i && j+1<sz) sum[i][j] -= sum[i-1][j+1];
}
}
rep(i,0,pr.size()){
sort(all(pr[i]));
sort(all(pl[i]));
sort(all(mr[i]));
sort(all(ml[i]));
}
}
ll ans = 0;
if(R-L <= 1000){
REP(i,L,R){
if(left[i]-1 <= L && R <= right[i]-1) ans++;
}
return ans;
}
L++, R++;
ll ld = L/di;
ll rd = (R+di-1)/di;
ans = sum[ld][rd];
REP(i, ld*di+1, L){
ans += pl[i].end()-lower_bound(all(pl[i]), rd*di);
ans -= ml[i].end()-lower_bound(all(ml[i]), rd*di);
}
rep(i,R,rd*di){
if(i >= int(pr.size())) continue;
ans += upper_bound(all(pr[i]), L)-pr[i].begin();
ans -= upper_bound(all(mr[i]), L)-mr[i].begin();
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
525 ms |
48312 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 |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
1 ms |
976 KB |
Output is correct |
3 |
Correct |
1 ms |
976 KB |
Output is correct |
4 |
Correct |
2 ms |
976 KB |
Output is correct |
5 |
Correct |
2 ms |
976 KB |
Output is correct |
6 |
Correct |
2 ms |
976 KB |
Output is correct |
7 |
Correct |
2 ms |
976 KB |
Output is correct |
8 |
Correct |
2 ms |
952 KB |
Output is correct |
9 |
Correct |
1 ms |
976 KB |
Output is correct |
10 |
Correct |
1 ms |
976 KB |
Output is correct |
11 |
Correct |
1 ms |
976 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
3 ms |
976 KB |
Output is correct |
14 |
Correct |
2 ms |
984 KB |
Output is correct |
15 |
Correct |
2 ms |
976 KB |
Output is correct |
16 |
Correct |
2 ms |
940 KB |
Output is correct |
17 |
Correct |
1 ms |
976 KB |
Output is correct |
18 |
Correct |
2 ms |
976 KB |
Output is correct |
19 |
Correct |
2 ms |
976 KB |
Output is correct |
20 |
Correct |
1 ms |
976 KB |
Output is correct |
21 |
Correct |
1 ms |
976 KB |
Output is correct |
22 |
Correct |
1 ms |
976 KB |
Output is correct |
23 |
Correct |
1 ms |
976 KB |
Output is correct |
24 |
Correct |
1 ms |
976 KB |
Output is correct |
25 |
Correct |
1 ms |
592 KB |
Output is correct |
26 |
Correct |
2 ms |
976 KB |
Output is correct |
27 |
Correct |
1 ms |
976 KB |
Output is correct |
28 |
Correct |
1 ms |
976 KB |
Output is correct |
29 |
Correct |
2 ms |
976 KB |
Output is correct |
30 |
Correct |
2 ms |
988 KB |
Output is correct |
31 |
Correct |
2 ms |
976 KB |
Output is correct |
32 |
Correct |
1 ms |
976 KB |
Output is correct |
33 |
Correct |
1 ms |
976 KB |
Output is correct |
34 |
Correct |
2 ms |
976 KB |
Output is correct |
35 |
Correct |
2 ms |
976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
1 ms |
976 KB |
Output is correct |
3 |
Correct |
1 ms |
976 KB |
Output is correct |
4 |
Correct |
2 ms |
976 KB |
Output is correct |
5 |
Correct |
2 ms |
976 KB |
Output is correct |
6 |
Correct |
2 ms |
976 KB |
Output is correct |
7 |
Correct |
2 ms |
976 KB |
Output is correct |
8 |
Correct |
2 ms |
952 KB |
Output is correct |
9 |
Correct |
1 ms |
976 KB |
Output is correct |
10 |
Correct |
1 ms |
976 KB |
Output is correct |
11 |
Correct |
1 ms |
976 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
3 ms |
976 KB |
Output is correct |
14 |
Correct |
2 ms |
984 KB |
Output is correct |
15 |
Correct |
2 ms |
976 KB |
Output is correct |
16 |
Correct |
2 ms |
940 KB |
Output is correct |
17 |
Correct |
1 ms |
976 KB |
Output is correct |
18 |
Correct |
2 ms |
976 KB |
Output is correct |
19 |
Correct |
2 ms |
976 KB |
Output is correct |
20 |
Correct |
1 ms |
976 KB |
Output is correct |
21 |
Correct |
1 ms |
976 KB |
Output is correct |
22 |
Correct |
1 ms |
976 KB |
Output is correct |
23 |
Correct |
1 ms |
976 KB |
Output is correct |
24 |
Correct |
1 ms |
976 KB |
Output is correct |
25 |
Correct |
1 ms |
592 KB |
Output is correct |
26 |
Correct |
2 ms |
976 KB |
Output is correct |
27 |
Correct |
1 ms |
976 KB |
Output is correct |
28 |
Correct |
1 ms |
976 KB |
Output is correct |
29 |
Correct |
2 ms |
976 KB |
Output is correct |
30 |
Correct |
2 ms |
988 KB |
Output is correct |
31 |
Correct |
2 ms |
976 KB |
Output is correct |
32 |
Correct |
1 ms |
976 KB |
Output is correct |
33 |
Correct |
1 ms |
976 KB |
Output is correct |
34 |
Correct |
2 ms |
976 KB |
Output is correct |
35 |
Correct |
2 ms |
976 KB |
Output is correct |
36 |
Correct |
83 ms |
55744 KB |
Output is correct |
37 |
Correct |
189 ms |
115900 KB |
Output is correct |
38 |
Correct |
188 ms |
116416 KB |
Output is correct |
39 |
Correct |
186 ms |
115948 KB |
Output is correct |
40 |
Correct |
186 ms |
115856 KB |
Output is correct |
41 |
Correct |
184 ms |
115712 KB |
Output is correct |
42 |
Correct |
184 ms |
115848 KB |
Output is correct |
43 |
Correct |
189 ms |
115644 KB |
Output is correct |
44 |
Correct |
191 ms |
115760 KB |
Output is correct |
45 |
Correct |
188 ms |
115672 KB |
Output is correct |
46 |
Correct |
186 ms |
115692 KB |
Output is correct |
47 |
Correct |
185 ms |
115760 KB |
Output is correct |
48 |
Correct |
184 ms |
115884 KB |
Output is correct |
49 |
Correct |
180 ms |
115884 KB |
Output is correct |
50 |
Correct |
187 ms |
115664 KB |
Output is correct |
51 |
Correct |
209 ms |
115656 KB |
Output is correct |
52 |
Correct |
183 ms |
115884 KB |
Output is correct |
53 |
Correct |
186 ms |
115932 KB |
Output is correct |
54 |
Correct |
178 ms |
115840 KB |
Output is correct |
55 |
Correct |
190 ms |
115720 KB |
Output is correct |
56 |
Correct |
189 ms |
115864 KB |
Output is correct |
57 |
Correct |
179 ms |
109164 KB |
Output is correct |
58 |
Correct |
198 ms |
115828 KB |
Output is correct |
59 |
Correct |
182 ms |
115744 KB |
Output is correct |
60 |
Correct |
176 ms |
115848 KB |
Output is correct |
61 |
Correct |
185 ms |
115748 KB |
Output is correct |
62 |
Correct |
181 ms |
116008 KB |
Output is correct |
63 |
Correct |
182 ms |
115944 KB |
Output is correct |
64 |
Correct |
190 ms |
115772 KB |
Output is correct |
65 |
Correct |
193 ms |
115760 KB |
Output is correct |
66 |
Correct |
193 ms |
115852 KB |
Output is correct |
67 |
Correct |
184 ms |
115768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
705 ms |
114448 KB |
Output is correct |
2 |
Correct |
871 ms |
115884 KB |
Output is correct |
3 |
Correct |
916 ms |
115988 KB |
Output is correct |
4 |
Correct |
901 ms |
115780 KB |
Output is correct |
5 |
Correct |
878 ms |
115776 KB |
Output is correct |
6 |
Correct |
1052 ms |
115776 KB |
Output is correct |
7 |
Correct |
917 ms |
115900 KB |
Output is correct |
8 |
Correct |
953 ms |
115776 KB |
Output is correct |
9 |
Correct |
1014 ms |
115764 KB |
Output is correct |
10 |
Correct |
944 ms |
115772 KB |
Output is correct |
11 |
Correct |
969 ms |
115768 KB |
Output is correct |
12 |
Correct |
990 ms |
115772 KB |
Output is correct |
13 |
Correct |
822 ms |
115660 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
976 KB |
Output is correct |
16 |
Correct |
1 ms |
976 KB |
Output is correct |
17 |
Correct |
185 ms |
115760 KB |
Output is correct |
18 |
Correct |
179 ms |
115776 KB |
Output is correct |
19 |
Correct |
179 ms |
115884 KB |
Output is correct |
20 |
Correct |
183 ms |
115768 KB |
Output is correct |
21 |
Correct |
187 ms |
115768 KB |
Output is correct |
22 |
Correct |
186 ms |
115852 KB |
Output is correct |
23 |
Correct |
189 ms |
116104 KB |
Output is correct |
24 |
Correct |
177 ms |
115820 KB |
Output is correct |
25 |
Correct |
190 ms |
115764 KB |
Output is correct |
26 |
Correct |
183 ms |
115844 KB |
Output is correct |
27 |
Correct |
1 ms |
976 KB |
Output is correct |
28 |
Correct |
1 ms |
976 KB |
Output is correct |
29 |
Correct |
1 ms |
976 KB |
Output is correct |
30 |
Correct |
1 ms |
976 KB |
Output is correct |
31 |
Correct |
1 ms |
976 KB |
Output is correct |
32 |
Correct |
1 ms |
976 KB |
Output is correct |
33 |
Correct |
1 ms |
976 KB |
Output is correct |
34 |
Correct |
1 ms |
976 KB |
Output is correct |
35 |
Correct |
2 ms |
976 KB |
Output is correct |
36 |
Correct |
1 ms |
976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
261 ms |
12144 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 |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
1 ms |
976 KB |
Output is correct |
3 |
Correct |
1 ms |
976 KB |
Output is correct |
4 |
Correct |
2 ms |
976 KB |
Output is correct |
5 |
Correct |
2 ms |
976 KB |
Output is correct |
6 |
Correct |
2 ms |
976 KB |
Output is correct |
7 |
Correct |
2 ms |
976 KB |
Output is correct |
8 |
Correct |
2 ms |
952 KB |
Output is correct |
9 |
Correct |
1 ms |
976 KB |
Output is correct |
10 |
Correct |
1 ms |
976 KB |
Output is correct |
11 |
Correct |
1 ms |
976 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
3 ms |
976 KB |
Output is correct |
14 |
Correct |
2 ms |
984 KB |
Output is correct |
15 |
Correct |
2 ms |
976 KB |
Output is correct |
16 |
Correct |
2 ms |
940 KB |
Output is correct |
17 |
Correct |
1 ms |
976 KB |
Output is correct |
18 |
Correct |
2 ms |
976 KB |
Output is correct |
19 |
Correct |
2 ms |
976 KB |
Output is correct |
20 |
Correct |
1 ms |
976 KB |
Output is correct |
21 |
Correct |
1 ms |
976 KB |
Output is correct |
22 |
Correct |
1 ms |
976 KB |
Output is correct |
23 |
Correct |
1 ms |
976 KB |
Output is correct |
24 |
Correct |
1 ms |
976 KB |
Output is correct |
25 |
Correct |
1 ms |
592 KB |
Output is correct |
26 |
Correct |
2 ms |
976 KB |
Output is correct |
27 |
Correct |
1 ms |
976 KB |
Output is correct |
28 |
Correct |
1 ms |
976 KB |
Output is correct |
29 |
Correct |
2 ms |
976 KB |
Output is correct |
30 |
Correct |
2 ms |
988 KB |
Output is correct |
31 |
Correct |
2 ms |
976 KB |
Output is correct |
32 |
Correct |
1 ms |
976 KB |
Output is correct |
33 |
Correct |
1 ms |
976 KB |
Output is correct |
34 |
Correct |
2 ms |
976 KB |
Output is correct |
35 |
Correct |
2 ms |
976 KB |
Output is correct |
36 |
Correct |
83 ms |
55744 KB |
Output is correct |
37 |
Correct |
189 ms |
115900 KB |
Output is correct |
38 |
Correct |
188 ms |
116416 KB |
Output is correct |
39 |
Correct |
186 ms |
115948 KB |
Output is correct |
40 |
Correct |
186 ms |
115856 KB |
Output is correct |
41 |
Correct |
184 ms |
115712 KB |
Output is correct |
42 |
Correct |
184 ms |
115848 KB |
Output is correct |
43 |
Correct |
189 ms |
115644 KB |
Output is correct |
44 |
Correct |
191 ms |
115760 KB |
Output is correct |
45 |
Correct |
188 ms |
115672 KB |
Output is correct |
46 |
Correct |
186 ms |
115692 KB |
Output is correct |
47 |
Correct |
185 ms |
115760 KB |
Output is correct |
48 |
Correct |
184 ms |
115884 KB |
Output is correct |
49 |
Correct |
180 ms |
115884 KB |
Output is correct |
50 |
Correct |
187 ms |
115664 KB |
Output is correct |
51 |
Correct |
209 ms |
115656 KB |
Output is correct |
52 |
Correct |
183 ms |
115884 KB |
Output is correct |
53 |
Correct |
186 ms |
115932 KB |
Output is correct |
54 |
Correct |
178 ms |
115840 KB |
Output is correct |
55 |
Correct |
190 ms |
115720 KB |
Output is correct |
56 |
Correct |
189 ms |
115864 KB |
Output is correct |
57 |
Correct |
179 ms |
109164 KB |
Output is correct |
58 |
Correct |
198 ms |
115828 KB |
Output is correct |
59 |
Correct |
182 ms |
115744 KB |
Output is correct |
60 |
Correct |
176 ms |
115848 KB |
Output is correct |
61 |
Correct |
185 ms |
115748 KB |
Output is correct |
62 |
Correct |
181 ms |
116008 KB |
Output is correct |
63 |
Correct |
182 ms |
115944 KB |
Output is correct |
64 |
Correct |
190 ms |
115772 KB |
Output is correct |
65 |
Correct |
193 ms |
115760 KB |
Output is correct |
66 |
Correct |
193 ms |
115852 KB |
Output is correct |
67 |
Correct |
184 ms |
115768 KB |
Output is correct |
68 |
Correct |
705 ms |
114448 KB |
Output is correct |
69 |
Correct |
871 ms |
115884 KB |
Output is correct |
70 |
Correct |
916 ms |
115988 KB |
Output is correct |
71 |
Correct |
901 ms |
115780 KB |
Output is correct |
72 |
Correct |
878 ms |
115776 KB |
Output is correct |
73 |
Correct |
1052 ms |
115776 KB |
Output is correct |
74 |
Correct |
917 ms |
115900 KB |
Output is correct |
75 |
Correct |
953 ms |
115776 KB |
Output is correct |
76 |
Correct |
1014 ms |
115764 KB |
Output is correct |
77 |
Correct |
944 ms |
115772 KB |
Output is correct |
78 |
Correct |
969 ms |
115768 KB |
Output is correct |
79 |
Correct |
990 ms |
115772 KB |
Output is correct |
80 |
Correct |
822 ms |
115660 KB |
Output is correct |
81 |
Correct |
1 ms |
336 KB |
Output is correct |
82 |
Correct |
1 ms |
976 KB |
Output is correct |
83 |
Correct |
1 ms |
976 KB |
Output is correct |
84 |
Correct |
185 ms |
115760 KB |
Output is correct |
85 |
Correct |
179 ms |
115776 KB |
Output is correct |
86 |
Correct |
179 ms |
115884 KB |
Output is correct |
87 |
Correct |
183 ms |
115768 KB |
Output is correct |
88 |
Correct |
187 ms |
115768 KB |
Output is correct |
89 |
Correct |
186 ms |
115852 KB |
Output is correct |
90 |
Correct |
189 ms |
116104 KB |
Output is correct |
91 |
Correct |
177 ms |
115820 KB |
Output is correct |
92 |
Correct |
190 ms |
115764 KB |
Output is correct |
93 |
Correct |
183 ms |
115844 KB |
Output is correct |
94 |
Correct |
1 ms |
976 KB |
Output is correct |
95 |
Correct |
1 ms |
976 KB |
Output is correct |
96 |
Correct |
1 ms |
976 KB |
Output is correct |
97 |
Correct |
1 ms |
976 KB |
Output is correct |
98 |
Correct |
1 ms |
976 KB |
Output is correct |
99 |
Correct |
1 ms |
976 KB |
Output is correct |
100 |
Correct |
1 ms |
976 KB |
Output is correct |
101 |
Correct |
1 ms |
976 KB |
Output is correct |
102 |
Correct |
2 ms |
976 KB |
Output is correct |
103 |
Correct |
1 ms |
976 KB |
Output is correct |
104 |
Correct |
782 ms |
94060 KB |
Output is correct |
105 |
Correct |
903 ms |
115996 KB |
Output is correct |
106 |
Correct |
1018 ms |
115904 KB |
Output is correct |
107 |
Correct |
1060 ms |
115780 KB |
Output is correct |
108 |
Correct |
1066 ms |
115924 KB |
Output is correct |
109 |
Correct |
995 ms |
115956 KB |
Output is correct |
110 |
Correct |
1154 ms |
115884 KB |
Output is correct |
111 |
Correct |
1029 ms |
115776 KB |
Output is correct |
112 |
Correct |
1097 ms |
115764 KB |
Output is correct |
113 |
Correct |
855 ms |
115772 KB |
Output is correct |
114 |
Correct |
1016 ms |
115780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
525 ms |
48312 KB |
31st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |