#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 1e5+10, MOD = 1e9+7;
const ll INF = 1e18+10;
const int L = 20;
struct D1 {
int sum, mn, mx;
D1() {
sum = 0;
mn = MOD;
mx = -1;
}
D1(int x, int loc) {
sum = x;
if (x) mn = mx = loc;
else mn = MOD, mx = -1;
}
friend D1 operator + (const D1& one, const D1& two) {
D1 ans;
ans.sum = one.sum + two.sum;
ans.mn = min(one.mn, two.mn);
ans.mx = max(one.mx, two.mx);
return ans;
}
};
struct T1 {
D1 d;
T1 *l, *r;
T1(D1 _d): d(_d) {
l = nullptr;
r = nullptr;
}
T1(T1* _l, T1* _r): l(_l), r(_r) {
d = l->d + r->d;
}
};
T1* build1(int tl, int tr, const vector<int>& v) {
if (tl == tr) {
return new T1(D1(v[tl], tl));
}
else {
int tm = (tl + tr) / 2;
build1(tl, tm, v);
build1(tm+1, tr, v);
T1* ans = new T1(build1(tl, tm, v), build1(tm+1, tr, v));
return ans;
}
}
T1* upd1(T1* t, int tl, int tr, int pos, int x) {
if (tl == tr) {
return new T1(D1(x, tl));
}
int tm = (tl + tr) / 2;
if (pos <= tm)
return new T1(upd1(t->l, tl, tm, pos, x), t->r);
else
return new T1(t->l, upd1(t->r, tm+1, tr, pos, x));
}
D1 qry1(T1* t, int tl, int tr, int l, int r) {
if (r < tl || l > tr) return D1();
if (l <= tl && tr <= r) return t->d;
int tm = (tl + tr) / 2;
return qry1(t->l, tl, tm, l, r) + qry1(t->r, tm+1, tr, l, r);
}
int n, a[N], st[N][L];
vector<pair<int, int>> store;
vector<int> base;
vector<int> ord;
T1* vers[N];
void build_rmq() {
for (int i = 0; i < n; i++) st[i][0] = a[i];
for (int k = 2, l = 1; k <= n; k *= 2, l++) {
for (int i = 0; i + k <= n; i++) {
st[i][l] = max(st[i][l-1], st[i + k / 2][l-1]);
}
}
}
int qry_max(int l, int r) {
int len = r - l + 1;
int use = 31 - __builtin_clz(len);
return max(st[l][use], st[r - (1 << use) + 1][use]);
}
void init(int _n, vector<int> H) {
n = _n;
for (int i = 0; i < n; i++) {
a[i] = H[i];
}
build_rmq();
set<int> s;
for (int i = 0; i < n; i++) {
bool use = 1;
if (i && a[i-1] < a[i]) use = 0;
if (i < n-1 && a[i+1] < a[i]) use = 0;
if (use) s.insert(i);
}
base = vector<int>(s.begin(), s.end());
auto f = [&](int i) {
auto it = s.lower_bound(i); assert(*it == i);
--it;
return qry_max(*it, i) - max(a[*it], a[i]);
};
set<pair<int, int>> q;
auto get_v = [&](int x) {
return pair<int, int>{f(x), x};
};
for (auto it = next(s.begin()); it != s.end(); it++) {
q.insert(get_v(*it));
}
vector<pair<int, int>> v;
v.emplace_back(0, sz(s)); // d > 0, ans = sz(s)
while (sz(q)) {
auto [d, i] = *q.begin(); q.erase(q.begin());
v.emplace_back(d, -1); // need to update v.back().second
auto it = s.lower_bound(i); assert(*it == i);
int one = i;
int two = *prev(it);
if (next(it) != s.end()) {
q.erase(get_v(*next(it)));
}
if (prev(it) != s.begin()) {
q.erase(get_v(two));
}
if (a[one] > a[two]) {
s.erase(one);
ord.push_back(one);
it = s.lower_bound(two);
if (next(it) != s.end()) {
q.insert(get_v(*next(it)));
}
if (it != s.begin()) {
q.insert(get_v(*it));
}
} else {
s.erase(two);
ord.push_back(two);
it = s.lower_bound(one);
if (next(it) != s.end()) {
q.insert(get_v(*next(it)));
}
if (it != s.begin()) {
q.insert(get_v(*it));
}
}
v.back().second = sz(s);
}
store.push_back(v[0]);
for (int i = 1; i < sz(v); i++) {
if (v[i].first <= store.back().first) {
store.back().second = v[i].second;
} else {
store.push_back(v[i]);
}
}
vector<int> aux_base(n);
for (int x : base) aux_base[x] = 1;
vers[0] = build1(0, n-1, aux_base);
// cerr << "done\n";
for (int i = 1; i <= sz(ord); i++) {
vers[i] = upd1(vers[i-1], 0, n-1, ord[i-1], 0);
}
// cerr << "done\n";
}
bool has[N];
int max_towers(int l, int r, int d) {
// find the last thing < d
int idx = lower_bound(store.begin(), store.end(), pair<int, int>{d, -1}) - store.begin() - 1;
int gone = sz(base) - store[idx].second;
D1 dd = qry1(vers[gone], 0, n-1, l, r);
int ans = dd.sum;
int first = dd.mn, last = dd.mx;
/*
memset(has, 0, sizeof(has));
for (int x : base) has[x] = 1;
for (int i = 0; i < gone; i++) has[ord[i]] = 0;
int ans = 0;
int first = n, last = -1;
for (int i = l; i <= r; i++) {
ans += has[i];
if (has[i]) {
first = min(first, i);
last = max(last, i);
}
}
// cerr << ans << ' ' << ' ' << first << ' ' << last << endl;
*/
if (ans == 0) {
int mx = qry_max(l, r);
return max(1, (mx - d >= a[l]) + (mx - d >= a[r]));
}
bool one = 0, two = 0;
for (int i = l; i < first; i++) {
int x = qry_max(i, first);
if (a[i] <= x - d && x - d >= a[first]) {
one = 1;
break;
}
}
for (int i = last+1; i <= r; i++) {
int x = qry_max(last, i);
if (a[i] <= x - d && x - d >= a[last]) {
two = 1;
break;
}
}
ans += one;
ans += two;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1556 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12624 KB |
Output is correct |
2 |
Correct |
200 ms |
254244 KB |
Output is correct |
3 |
Correct |
190 ms |
254200 KB |
Output is correct |
4 |
Correct |
186 ms |
254408 KB |
Output is correct |
5 |
Correct |
186 ms |
254440 KB |
Output is correct |
6 |
Correct |
188 ms |
254476 KB |
Output is correct |
7 |
Correct |
187 ms |
254392 KB |
Output is correct |
8 |
Correct |
198 ms |
253888 KB |
Output is correct |
9 |
Correct |
193 ms |
253904 KB |
Output is correct |
10 |
Correct |
214 ms |
253872 KB |
Output is correct |
11 |
Correct |
186 ms |
253808 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
189 ms |
253808 KB |
Output is correct |
14 |
Correct |
183 ms |
253796 KB |
Output is correct |
15 |
Correct |
189 ms |
254300 KB |
Output is correct |
16 |
Correct |
198 ms |
254664 KB |
Output is correct |
17 |
Correct |
196 ms |
254368 KB |
Output is correct |
18 |
Correct |
186 ms |
253884 KB |
Output is correct |
19 |
Correct |
189 ms |
253860 KB |
Output is correct |
20 |
Correct |
199 ms |
254204 KB |
Output is correct |
21 |
Correct |
188 ms |
254436 KB |
Output is correct |
22 |
Correct |
187 ms |
254440 KB |
Output is correct |
23 |
Correct |
185 ms |
253816 KB |
Output is correct |
24 |
Correct |
185 ms |
253820 KB |
Output is correct |
25 |
Correct |
40 ms |
53828 KB |
Output is correct |
26 |
Correct |
213 ms |
254304 KB |
Output is correct |
27 |
Correct |
194 ms |
254308 KB |
Output is correct |
28 |
Correct |
187 ms |
254512 KB |
Output is correct |
29 |
Correct |
192 ms |
254404 KB |
Output is correct |
30 |
Correct |
225 ms |
254420 KB |
Output is correct |
31 |
Correct |
193 ms |
254404 KB |
Output is correct |
32 |
Correct |
190 ms |
253784 KB |
Output is correct |
33 |
Correct |
198 ms |
253856 KB |
Output is correct |
34 |
Correct |
186 ms |
253840 KB |
Output is correct |
35 |
Correct |
186 ms |
253860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12624 KB |
Output is correct |
2 |
Correct |
200 ms |
254244 KB |
Output is correct |
3 |
Correct |
190 ms |
254200 KB |
Output is correct |
4 |
Correct |
186 ms |
254408 KB |
Output is correct |
5 |
Correct |
186 ms |
254440 KB |
Output is correct |
6 |
Correct |
188 ms |
254476 KB |
Output is correct |
7 |
Correct |
187 ms |
254392 KB |
Output is correct |
8 |
Correct |
198 ms |
253888 KB |
Output is correct |
9 |
Correct |
193 ms |
253904 KB |
Output is correct |
10 |
Correct |
214 ms |
253872 KB |
Output is correct |
11 |
Correct |
186 ms |
253808 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
189 ms |
253808 KB |
Output is correct |
14 |
Correct |
183 ms |
253796 KB |
Output is correct |
15 |
Correct |
189 ms |
254300 KB |
Output is correct |
16 |
Correct |
198 ms |
254664 KB |
Output is correct |
17 |
Correct |
196 ms |
254368 KB |
Output is correct |
18 |
Correct |
186 ms |
253884 KB |
Output is correct |
19 |
Correct |
189 ms |
253860 KB |
Output is correct |
20 |
Correct |
199 ms |
254204 KB |
Output is correct |
21 |
Correct |
188 ms |
254436 KB |
Output is correct |
22 |
Correct |
187 ms |
254440 KB |
Output is correct |
23 |
Correct |
185 ms |
253816 KB |
Output is correct |
24 |
Correct |
185 ms |
253820 KB |
Output is correct |
25 |
Correct |
40 ms |
53828 KB |
Output is correct |
26 |
Correct |
213 ms |
254304 KB |
Output is correct |
27 |
Correct |
194 ms |
254308 KB |
Output is correct |
28 |
Correct |
187 ms |
254512 KB |
Output is correct |
29 |
Correct |
192 ms |
254404 KB |
Output is correct |
30 |
Correct |
225 ms |
254420 KB |
Output is correct |
31 |
Correct |
193 ms |
254404 KB |
Output is correct |
32 |
Correct |
190 ms |
253784 KB |
Output is correct |
33 |
Correct |
198 ms |
253856 KB |
Output is correct |
34 |
Correct |
186 ms |
253840 KB |
Output is correct |
35 |
Correct |
186 ms |
253860 KB |
Output is correct |
36 |
Runtime error |
1372 ms |
2097152 KB |
Execution killed with signal 9 |
37 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1400 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1355 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12624 KB |
Output is correct |
2 |
Correct |
200 ms |
254244 KB |
Output is correct |
3 |
Correct |
190 ms |
254200 KB |
Output is correct |
4 |
Correct |
186 ms |
254408 KB |
Output is correct |
5 |
Correct |
186 ms |
254440 KB |
Output is correct |
6 |
Correct |
188 ms |
254476 KB |
Output is correct |
7 |
Correct |
187 ms |
254392 KB |
Output is correct |
8 |
Correct |
198 ms |
253888 KB |
Output is correct |
9 |
Correct |
193 ms |
253904 KB |
Output is correct |
10 |
Correct |
214 ms |
253872 KB |
Output is correct |
11 |
Correct |
186 ms |
253808 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
189 ms |
253808 KB |
Output is correct |
14 |
Correct |
183 ms |
253796 KB |
Output is correct |
15 |
Correct |
189 ms |
254300 KB |
Output is correct |
16 |
Correct |
198 ms |
254664 KB |
Output is correct |
17 |
Correct |
196 ms |
254368 KB |
Output is correct |
18 |
Correct |
186 ms |
253884 KB |
Output is correct |
19 |
Correct |
189 ms |
253860 KB |
Output is correct |
20 |
Correct |
199 ms |
254204 KB |
Output is correct |
21 |
Correct |
188 ms |
254436 KB |
Output is correct |
22 |
Correct |
187 ms |
254440 KB |
Output is correct |
23 |
Correct |
185 ms |
253816 KB |
Output is correct |
24 |
Correct |
185 ms |
253820 KB |
Output is correct |
25 |
Correct |
40 ms |
53828 KB |
Output is correct |
26 |
Correct |
213 ms |
254304 KB |
Output is correct |
27 |
Correct |
194 ms |
254308 KB |
Output is correct |
28 |
Correct |
187 ms |
254512 KB |
Output is correct |
29 |
Correct |
192 ms |
254404 KB |
Output is correct |
30 |
Correct |
225 ms |
254420 KB |
Output is correct |
31 |
Correct |
193 ms |
254404 KB |
Output is correct |
32 |
Correct |
190 ms |
253784 KB |
Output is correct |
33 |
Correct |
198 ms |
253856 KB |
Output is correct |
34 |
Correct |
186 ms |
253840 KB |
Output is correct |
35 |
Correct |
186 ms |
253860 KB |
Output is correct |
36 |
Runtime error |
1372 ms |
2097152 KB |
Execution killed with signal 9 |
37 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1556 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |