191 lines
4.2 KiB
C++
191 lines
4.2 KiB
C++
/* Problem URL: https://cses.fi/problemset/task/3314 */
|
|
|
|
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
#define V vector
|
|
|
|
#define rmin(a, b) a = min(a, b)
|
|
#define rmax(a, b) a = max(a, b)
|
|
|
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
|
|
|
#define repv(i, v) for (auto &i : (v))
|
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
|
#define sortv(v) sort(v.begin(), v.end())
|
|
#define all(v) (v).begin(), (v).end()
|
|
|
|
using vi = vector<int>;
|
|
using vvi = vector<vi>;
|
|
using vvvi = vector<vvi>;
|
|
using vvvvi = vector<vvvi>;
|
|
|
|
using ll = long long;
|
|
|
|
using vl = vector<ll>;
|
|
using vvl = vector<vl>;
|
|
using vvvl = vector<vvl>;
|
|
using vvvvl = vector<vvvl>;
|
|
|
|
template<class v>
|
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
|
os << vec[0];
|
|
for (size_t i = 1; i < vec.size(); i++) {
|
|
os << ' ' << vec[i];
|
|
}
|
|
os << '\n';
|
|
return os;
|
|
}
|
|
|
|
template<class v>
|
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
|
for (auto &i : vec) {
|
|
is >> i;
|
|
}
|
|
return is;
|
|
}
|
|
|
|
template<class v>
|
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
|
for (auto &i : vec) {
|
|
os << i[0];
|
|
for (size_t j = 1; j < i.size(); j++) {
|
|
os << ' ' << i[j];
|
|
}
|
|
os << '\n';
|
|
}
|
|
return os;
|
|
}
|
|
|
|
template<class v>
|
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
|
for (auto &i : vec) {
|
|
for (auto &j : i) {
|
|
is >> j;
|
|
}
|
|
}
|
|
return is;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
ios::sync_with_stdio(false);
|
|
cin.tie(nullptr);
|
|
|
|
int n;
|
|
cin >> n;
|
|
|
|
vl fds(n);
|
|
cin >> fds;
|
|
|
|
while (__builtin_popcount(fds.size()) != 1) {
|
|
fds.push_back(0);
|
|
}
|
|
|
|
vi seg(fds.size() * 2, 1);
|
|
|
|
auto update = [&](int i, int v) {
|
|
seg[i + fds.size()] = v;
|
|
|
|
for (int j = (i + fds.size()) / 2; j > 0; j /= 2) {
|
|
seg[j] = max(seg[j * 2], seg[j * 2 + 1]);
|
|
}
|
|
};
|
|
|
|
function<int(int, int, int, int, int)> search = [&](int i, int l, int r, int tl, int tr) {
|
|
if (l > tr || r < tl) {
|
|
return 0;
|
|
}
|
|
|
|
if (l >= tl && r <= tr) {
|
|
return seg[i];
|
|
}
|
|
|
|
int mid = (l + r) / 2;
|
|
return max(search(i * 2, l, mid, tl, tr), search(i * 2 + 1, mid + 1, r, tl, tr));
|
|
};
|
|
|
|
priority_queue<pair<ll, ll>, V<pair<ll, ll>>, greater<>> order;
|
|
|
|
vvl sparse(n, vl(20));
|
|
rep(i, n) {
|
|
sparse[i][0] = fds[i];
|
|
}
|
|
|
|
nrep(j, 1, 20) {
|
|
for (int i = 0; i + (1 << (j - 1)) < n; i++) {
|
|
sparse[i][j] = max(sparse[i][j - 1], sparse[i + (1 << (j - 1))][j - 1]);
|
|
}
|
|
}
|
|
|
|
auto sparsequery = [&](int l, int r) {
|
|
if (l == r) {
|
|
return sparse[l][0];
|
|
}
|
|
|
|
int log = 31 - __builtin_clz(r - l + 1);
|
|
return max(sparse[l][log], sparse[r - (1 << log) + 1][log]);
|
|
};
|
|
|
|
rep(i, n) {
|
|
order.emplace(fds[i], i);
|
|
}
|
|
|
|
while (!order.empty()) {
|
|
int i = order.top().second;
|
|
order.pop();
|
|
int ans1 = 0;
|
|
int ans2 = 0;
|
|
|
|
if (i > 0 && fds[i - 1] < fds[i]) {
|
|
int ans = 0;
|
|
int low = 0;
|
|
int high = i - 1;
|
|
|
|
while (low <= high) {
|
|
int mid = (low + high) / 2;
|
|
|
|
if (sparsequery(mid, i - 1) >= fds[i]) {
|
|
ans = mid + 1;
|
|
low = mid + 1;
|
|
continue;
|
|
}
|
|
|
|
high = mid - 1;
|
|
}
|
|
|
|
if (ans != i) {
|
|
ans1 = search(1, 0, fds.size() - 1, ans, i - 1);
|
|
}
|
|
}
|
|
|
|
if (i < n - 1 && fds[i + 1] < fds[i]) {
|
|
int ans = n - 1;
|
|
int low = i + 1;
|
|
int high = n - 1;
|
|
|
|
while (low <= high) {
|
|
int mid = (low + high) / 2;
|
|
|
|
if (sparsequery(i + 1, mid) >= fds[i]) {
|
|
ans = mid - 1;
|
|
high = mid - 1;
|
|
continue;
|
|
}
|
|
|
|
low = mid + 1;
|
|
}
|
|
|
|
if (ans != i) {
|
|
ans2 = search(1, 0, fds.size() - 1, i + 1, ans);
|
|
}
|
|
}
|
|
|
|
update(i, max(ans1, ans2) + 1);
|
|
}
|
|
|
|
cout << seg[1] << '\n';
|
|
}
|