#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifndef WAIMAI
#include "towers.h"
#else
#include "stub.cpp"
#endif
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
#define lpos pos*2
#define rpos pos*2+1
const int INF = 2e9 + 1;
const int SIZE = 1e5 + 5;
int n;
bool b;
int a[SIZE], ty[SIZE];
int pre[SIZE], pL[SIZE], pR[SIZE];
struct Segtree {
int mx[SIZE << 2], mn[SIZE << 2];
void build(int pos, int l, int r) {
if (l == r) {
mx[pos] = mn[pos] = a[l];
return;
}
int mid = (l + r) / 2;
build(lpos, l, mid);
build(rpos, mid + 1, r);
mx[pos] = max(mx[lpos], mx[rpos]);
mn[pos] = min(mn[lpos], mn[rpos]);
}
int que_mn(int pos, int l, int r, int L, int R) {
if (l == L && r == R) return mn[pos];
int mid = (L + R) / 2;
if (r <= mid) return que_mn(lpos, l, r, L, mid);
if (l > mid) return que_mn(rpos, l, r, mid + 1, R);
return min(que_mn(lpos, l, mid, L, mid), que_mn(rpos, mid + 1, r, mid + 1, R));
}
int schl(int pos, int l, int r, int L, int R, int x) {
if (l == L && r == R) {
if (mx[pos] < x) return n + 1;
if (l == r) return l;
int mid = (L + R) / 2, re = schl(lpos, l, mid, L, mid, x);
return re == n + 1 ? schl(rpos, mid + 1, r, mid + 1, R, x) : re;
}
int mid = (L + R) / 2, re;
if (r <= mid) return schl(lpos, l, r, L, mid, x);
if (l > mid) return schl(rpos, l, r, mid + 1, R, x);
re = schl(lpos, l, mid, L, mid, x);
return re == n + 1 ? schl(rpos, mid + 1, r, mid + 1, R, x) : re;
}
int schr(int pos, int l, int r, int L, int R, int x) {
if (l == L && r == R) {
if (mx[pos] < x) return 0;
if (l == r) return l;
int mid = (L + R) / 2, re = schr(rpos, mid + 1, r, mid + 1, R, x);
return re == 0 ? schr(lpos, l, mid, L, mid, x) : re;
}
int mid = (L + R) / 2, re;
if (r <= mid) return schr(lpos, l, r, L, mid, x);
if (l > mid) return schr(rpos, l, r, mid + 1, R, x);
re = schr(rpos, mid + 1, r, mid + 1, R, x);
return re == 0 ? schr(lpos, l, mid, L, mid, x) : re;
}
} tree;
void init(int N, vector<int> H) {
n = N;
if (n == 1) return;
FOR (i, 1, n) a[i] = H[i - 1];
FOR (i, 1, n) {
if ((i == 1 || a[i] < a[i - 1]) && (i == n || a[i] < a[i + 1])) ty[i] = 1;
if ((i == 1 || a[i] > a[i - 1]) && (i == n || a[i] > a[i + 1])) ty[i] = 2;
}
}
void build(int D) {
set<int> s;
multiset<pair<int, int>> ms;
for (int i = 1, x = 1; i <= n; i++) if (ty[i] == x) {
s.insert(i);
x ^= 3;
}
if (ty[*s.rbegin()] == 2) s.erase(*s.rbegin());
a[0] = a[n + 1] = INF;
s.insert(0), s.insert(n + 1);
bool f = 0;
for (auto it = s.begin(); it != s.end(); it++) {
f ^= 1;
if (f == 0) continue;
int i = *it;
if (it != s.begin()) {
int l = *prev(it);
ms.emplace(a[i] - a[l], i);
}
if (next(it) != s.end()) {
int r = *next(it);
ms.emplace(a[i] - a[r], i);
}
}
while (s.size() > 3) {
int d = ms.begin()->F;
if (d >= D) break;
while (ms.size() && ms.begin()->F == d) {
auto [_, i] = *ms.begin();
auto it = s.lower_bound(i), lit = prev(it), rit = next(it);
int l = *lit, r = *rit;
int il = *prev(lit), ir = *next(rit);
if (a[l] > a[r]) {
ms.erase(ms.find({a[i] - a[l], i}));
ms.erase(ms.find({a[il] - a[l], il}));
if (a[i] < a[il]) {
ms.erase({a[i] - a[r], i});
ms.emplace(a[il] - a[r], il);
s.erase(i);
} else {
int j = *prev(s.find(il));
ms.erase(ms.find({a[il] - a[j], il}));
ms.emplace(a[i] - a[j], i);
s.erase(il);
}
s.erase(l);
} else {
ms.erase(ms.find({a[i] - a[r], i}));
ms.erase(ms.find({a[ir] - a[r], ir}));
if (a[i] < a[ir]) {
ms.erase({a[i] - a[l], i});
ms.emplace(a[ir] - a[l], ir);
s.erase(i);
} else {
int j = *next(s.find(ir));
ms.erase(ms.find({a[ir] - a[j], ir}));
ms.emplace(a[i] - a[j], i);
s.erase(ir);
}
s.erase(r);
}
}
}
tree.build(1, 1, n);
for (int i : s) if (ty[i] == 1) pre[i] = 1;
pR[n + 1] = n + 1;
for (int i = n; i >= 1; i--) pR[i] = (pre[i] ? i : pR[i + 1]);
FOR (i, 1, n) {
pL[i] = (pre[i] ? i : pL[i - 1]);
pre[i] += pre[i - 1];
}
}
int max_towers(int L, int R, int D) {
L++, R++;
if (L == R) return 1;
if (b == 0) {
build(D);
b = 1;
}
int ans = pre[R] - pre[L - 1];
{
int i = pR[L];
if (L <= i && i <= R) {
int j = tree.schr(1, L, i, 1, n, a[i] + D);
if (L <= j) ans += (tree.que_mn(1, L, j, 1, n) <= a[j] - D);
}
}
{
int i = pL[R];
if (L <= i && i <= R) {
int j = tree.schl(1, i, R, 1, n, a[i] + D);
if (j <= R) ans += (tree.que_mn(1, j, R, 1, n) <= a[j] - D);
}
}
return ans;
}
/*
in1
7 3
10 20 60 40 50 30 70
1 5 10
2 2 100
0 6 17
out1
3
1
2
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
304 ms |
4972 KB |
1st lines differ - on the 1st token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4660 KB |
Output is correct |
4 |
Correct |
2 ms |
4612 KB |
Output is correct |
5 |
Correct |
2 ms |
4860 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Incorrect |
2 ms |
4696 KB |
1st lines differ - on the 1st token, expected: '34', found: '33' |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4660 KB |
Output is correct |
4 |
Correct |
2 ms |
4612 KB |
Output is correct |
5 |
Correct |
2 ms |
4860 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Incorrect |
2 ms |
4696 KB |
1st lines differ - on the 1st token, expected: '34', found: '33' |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
526 ms |
11988 KB |
29th lines differ - on the 1st token, expected: '9839', found: '9838' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
200 ms |
6488 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 |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4660 KB |
Output is correct |
4 |
Correct |
2 ms |
4612 KB |
Output is correct |
5 |
Correct |
2 ms |
4860 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Incorrect |
2 ms |
4696 KB |
1st lines differ - on the 1st token, expected: '34', found: '33' |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
304 ms |
4972 KB |
1st lines differ - on the 1st token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |