This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/1031
// competitive-verifier: TLE 0.5
// competitive-verifier: MLE 64
#include <iostream>
#include <vector>
#include <algorithm>
#include "src/Misc/CartesianTree.hpp"
using namespace std;
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
int N;
cin >> N;
vector<int> p(N);
for (int i= 0; i < N; ++i) cin >> p[i];
long long ans= 0;
for (int _= 2; _--;) {
reverse(p.begin(), p.end());
CartesianTree ct(p, false);
vector<array<int, 2>> st;
for (int i= 0; i < N; ++i) {
while (!st.empty() && st.back()[1] > p[i]) st.pop_back();
int l= ct.range(i)[0];
int n= lower_bound(st.begin(), st.end(), array{l, -1}) - st.begin();
ans+= st.size() - n;
st.push_back(array{i, p[i]});
}
}
cout << ans << '\n';
return 0;
}
#line 1 "test/yukicoder/1031.test.cpp"
// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/1031
// competitive-verifier: TLE 0.5
// competitive-verifier: MLE 64
#include <iostream>
#include <vector>
#include <algorithm>
#line 3 "src/Misc/CartesianTree.hpp"
#include <array>
class CartesianTree {
std::vector<std::array<int, 2>> rg, ch;
std::vector<int> par;
int rt;
public:
template <class Vec> CartesianTree(const Vec &a, bool is_min= 1): rg(a.size()), ch(a.size(), std::array{-1, -1}), par(a.size(), -1) {
const int n= a.size();
auto comp= [&](int l, int r) { return (is_min ? a[l] < a[r] : a[l] > a[r]) || (a[l] == a[r] && l < r); };
int st[n], t= 0;
for (int i= n; i--; rg[i][1]= (t ? st[t - 1] : n), st[t++]= i)
while (t && comp(i, st[t - 1])) ch[i][1]= st[--t];
for (int i= t= 0; i < n; rg[i][0]= (t ? st[t - 1] + 1 : 0), st[t++]= i++)
while (t && comp(i, st[t - 1])) ch[i][0]= st[--t];
for (int i= 0; i < n; ++i)
for (int b= 2; b--;)
if (ch[i][b] != -1) par[ch[i][b]]= i;
for (int i= 0; i < n; ++i)
if (par[i] == -1) rt= i;
}
std::array<int, 2> children(int i) const { return ch[i]; }
int parent(int i) const { return par[i]; }
int root() const { return rt; }
// [l,r)
std::array<int, 2> range(int i) const { return rg[i]; }
};
#line 8 "test/yukicoder/1031.test.cpp"
using namespace std;
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
int N;
cin >> N;
vector<int> p(N);
for (int i= 0; i < N; ++i) cin >> p[i];
long long ans= 0;
for (int _= 2; _--;) {
reverse(p.begin(), p.end());
CartesianTree ct(p, false);
vector<array<int, 2>> st;
for (int i= 0; i < N; ++i) {
while (!st.empty() && st.back()[1] > p[i]) st.pop_back();
int l= ct.range(i)[0];
int n= lower_bound(st.begin(), st.end(), array{l, -1}) - st.begin();
ans+= st.size() - n;
st.push_back(array{i, p[i]});
}
}
cout << ans << '\n';
return 0;
}
Env | Name | Status | Elapsed | Memory |
---|---|---|---|---|
g++-13 | 01_sample_01.txt |
![]() |
6 ms | 4 MB |
g++-13 | 01_sample_02.txt |
![]() |
5 ms | 4 MB |
g++-13 | 01_sample_03.txt |
![]() |
5 ms | 4 MB |
g++-13 | in1.txt |
![]() |
5 ms | 4 MB |
g++-13 | in10.txt |
![]() |
17 ms | 6 MB |
g++-13 | in11.txt |
![]() |
20 ms | 6 MB |
g++-13 | in12.txt |
![]() |
18 ms | 6 MB |
g++-13 | in13.txt |
![]() |
19 ms | 6 MB |
g++-13 | in14.txt |
![]() |
17 ms | 5 MB |
g++-13 | in15.txt |
![]() |
19 ms | 6 MB |
g++-13 | in16.txt |
![]() |
18 ms | 6 MB |
g++-13 | in17.txt |
![]() |
19 ms | 6 MB |
g++-13 | in18.txt |
![]() |
20 ms | 6 MB |
g++-13 | in19.txt |
![]() |
18 ms | 6 MB |
g++-13 | in2.txt |
![]() |
6 ms | 4 MB |
g++-13 | in20.txt |
![]() |
17 ms | 5 MB |
g++-13 | in21.txt |
![]() |
17 ms | 6 MB |
g++-13 | in22.txt |
![]() |
19 ms | 6 MB |
g++-13 | in23.txt |
![]() |
20 ms | 6 MB |
g++-13 | in24.txt |
![]() |
19 ms | 6 MB |
g++-13 | in25.txt |
![]() |
18 ms | 6 MB |
g++-13 | in26.txt |
![]() |
19 ms | 6 MB |
g++-13 | in27.txt |
![]() |
20 ms | 6 MB |
g++-13 | in28.txt |
![]() |
20 ms | 6 MB |
g++-13 | in29.txt |
![]() |
20 ms | 6 MB |
g++-13 | in3.txt |
![]() |
6 ms | 4 MB |
g++-13 | in30.txt |
![]() |
19 ms | 6 MB |
g++-13 | in31.txt |
![]() |
20 ms | 6 MB |
g++-13 | in32.txt |
![]() |
16 ms | 8 MB |
g++-13 | in33.txt |
![]() |
18 ms | 7 MB |
g++-13 | in34.txt |
![]() |
17 ms | 7 MB |
g++-13 | in35.txt |
![]() |
18 ms | 7 MB |
g++-13 | in36.txt |
![]() |
19 ms | 6 MB |
g++-13 | in37.txt |
![]() |
18 ms | 6 MB |
g++-13 | in38.txt |
![]() |
18 ms | 6 MB |
g++-13 | in39.txt |
![]() |
18 ms | 7 MB |
g++-13 | in4.txt |
![]() |
6 ms | 4 MB |
g++-13 | in40.txt |
![]() |
18 ms | 6 MB |
g++-13 | in41.txt |
![]() |
18 ms | 6 MB |
g++-13 | in42.txt |
![]() |
18 ms | 6 MB |
g++-13 | in43.txt |
![]() |
18 ms | 6 MB |
g++-13 | in44.txt |
![]() |
18 ms | 7 MB |
g++-13 | in45.txt |
![]() |
19 ms | 7 MB |
g++-13 | in46.txt |
![]() |
18 ms | 7 MB |
g++-13 | in47.txt |
![]() |
19 ms | 7 MB |
g++-13 | in48.txt |
![]() |
18 ms | 6 MB |
g++-13 | in49.txt |
![]() |
19 ms | 7 MB |
g++-13 | in5.txt |
![]() |
6 ms | 4 MB |
g++-13 | in50.txt |
![]() |
18 ms | 7 MB |
g++-13 | in51.txt |
![]() |
18 ms | 7 MB |
g++-13 | in52.txt |
![]() |
5 ms | 4 MB |
g++-13 | in53.txt |
![]() |
5 ms | 4 MB |
g++-13 | in6.txt |
![]() |
5 ms | 4 MB |
g++-13 | in7.txt |
![]() |
5 ms | 4 MB |
g++-13 | in8.txt |
![]() |
5 ms | 4 MB |
g++-13 | in9.txt |
![]() |
17 ms | 5 MB |
clang++-18 | 01_sample_01.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 01_sample_02.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 01_sample_03.txt |
![]() |
5 ms | 4 MB |
clang++-18 | in1.txt |
![]() |
5 ms | 4 MB |
clang++-18 | in10.txt |
![]() |
17 ms | 5 MB |
clang++-18 | in11.txt |
![]() |
20 ms | 6 MB |
clang++-18 | in12.txt |
![]() |
18 ms | 5 MB |
clang++-18 | in13.txt |
![]() |
19 ms | 5 MB |
clang++-18 | in14.txt |
![]() |
17 ms | 5 MB |
clang++-18 | in15.txt |
![]() |
19 ms | 5 MB |
clang++-18 | in16.txt |
![]() |
19 ms | 5 MB |
clang++-18 | in17.txt |
![]() |
19 ms | 6 MB |
clang++-18 | in18.txt |
![]() |
19 ms | 6 MB |
clang++-18 | in19.txt |
![]() |
18 ms | 5 MB |
clang++-18 | in2.txt |
![]() |
6 ms | 4 MB |
clang++-18 | in20.txt |
![]() |
16 ms | 5 MB |
clang++-18 | in21.txt |
![]() |
17 ms | 5 MB |
clang++-18 | in22.txt |
![]() |
19 ms | 6 MB |
clang++-18 | in23.txt |
![]() |
20 ms | 6 MB |
clang++-18 | in24.txt |
![]() |
18 ms | 5 MB |
clang++-18 | in25.txt |
![]() |
18 ms | 5 MB |
clang++-18 | in26.txt |
![]() |
20 ms | 6 MB |
clang++-18 | in27.txt |
![]() |
20 ms | 6 MB |
clang++-18 | in28.txt |
![]() |
20 ms | 6 MB |
clang++-18 | in29.txt |
![]() |
20 ms | 6 MB |
clang++-18 | in3.txt |
![]() |
6 ms | 4 MB |
clang++-18 | in30.txt |
![]() |
19 ms | 6 MB |
clang++-18 | in31.txt |
![]() |
20 ms | 6 MB |
clang++-18 | in32.txt |
![]() |
15 ms | 8 MB |
clang++-18 | in33.txt |
![]() |
17 ms | 7 MB |
clang++-18 | in34.txt |
![]() |
16 ms | 6 MB |
clang++-18 | in35.txt |
![]() |
17 ms | 7 MB |
clang++-18 | in36.txt |
![]() |
18 ms | 6 MB |
clang++-18 | in37.txt |
![]() |
17 ms | 6 MB |
clang++-18 | in38.txt |
![]() |
16 ms | 6 MB |
clang++-18 | in39.txt |
![]() |
17 ms | 6 MB |
clang++-18 | in4.txt |
![]() |
6 ms | 4 MB |
clang++-18 | in40.txt |
![]() |
16 ms | 6 MB |
clang++-18 | in41.txt |
![]() |
16 ms | 6 MB |
clang++-18 | in42.txt |
![]() |
16 ms | 6 MB |
clang++-18 | in43.txt |
![]() |
16 ms | 6 MB |
clang++-18 | in44.txt |
![]() |
16 ms | 7 MB |
clang++-18 | in45.txt |
![]() |
17 ms | 6 MB |
clang++-18 | in46.txt |
![]() |
16 ms | 6 MB |
clang++-18 | in47.txt |
![]() |
17 ms | 6 MB |
clang++-18 | in48.txt |
![]() |
17 ms | 6 MB |
clang++-18 | in49.txt |
![]() |
18 ms | 7 MB |
clang++-18 | in5.txt |
![]() |
6 ms | 4 MB |
clang++-18 | in50.txt |
![]() |
17 ms | 7 MB |
clang++-18 | in51.txt |
![]() |
17 ms | 7 MB |
clang++-18 | in52.txt |
![]() |
5 ms | 4 MB |
clang++-18 | in53.txt |
![]() |
5 ms | 4 MB |
clang++-18 | in6.txt |
![]() |
5 ms | 4 MB |
clang++-18 | in7.txt |
![]() |
5 ms | 4 MB |
clang++-18 | in8.txt |
![]() |
5 ms | 4 MB |
clang++-18 | in9.txt |
![]() |
17 ms | 5 MB |