Hashiryo's Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub hashiryo/Library

:heavy_check_mark: test/yukicoder/1031.test.cpp

Depends on

Code

// 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;
}

Test cases

Env Name Status Elapsed Memory
g++-13 01_sample_01.txt :heavy_check_mark: AC 6 ms 4 MB
g++-13 01_sample_02.txt :heavy_check_mark: AC 5 ms 4 MB
g++-13 01_sample_03.txt :heavy_check_mark: AC 5 ms 4 MB
g++-13 in1.txt :heavy_check_mark: AC 5 ms 4 MB
g++-13 in10.txt :heavy_check_mark: AC 17 ms 6 MB
g++-13 in11.txt :heavy_check_mark: AC 20 ms 6 MB
g++-13 in12.txt :heavy_check_mark: AC 18 ms 6 MB
g++-13 in13.txt :heavy_check_mark: AC 19 ms 6 MB
g++-13 in14.txt :heavy_check_mark: AC 17 ms 5 MB
g++-13 in15.txt :heavy_check_mark: AC 19 ms 6 MB
g++-13 in16.txt :heavy_check_mark: AC 18 ms 6 MB
g++-13 in17.txt :heavy_check_mark: AC 19 ms 6 MB
g++-13 in18.txt :heavy_check_mark: AC 20 ms 6 MB
g++-13 in19.txt :heavy_check_mark: AC 18 ms 6 MB
g++-13 in2.txt :heavy_check_mark: AC 6 ms 4 MB
g++-13 in20.txt :heavy_check_mark: AC 17 ms 5 MB
g++-13 in21.txt :heavy_check_mark: AC 17 ms 6 MB
g++-13 in22.txt :heavy_check_mark: AC 19 ms 6 MB
g++-13 in23.txt :heavy_check_mark: AC 20 ms 6 MB
g++-13 in24.txt :heavy_check_mark: AC 19 ms 6 MB
g++-13 in25.txt :heavy_check_mark: AC 18 ms 6 MB
g++-13 in26.txt :heavy_check_mark: AC 19 ms 6 MB
g++-13 in27.txt :heavy_check_mark: AC 20 ms 6 MB
g++-13 in28.txt :heavy_check_mark: AC 20 ms 6 MB
g++-13 in29.txt :heavy_check_mark: AC 20 ms 6 MB
g++-13 in3.txt :heavy_check_mark: AC 6 ms 4 MB
g++-13 in30.txt :heavy_check_mark: AC 19 ms 6 MB
g++-13 in31.txt :heavy_check_mark: AC 20 ms 6 MB
g++-13 in32.txt :heavy_check_mark: AC 16 ms 8 MB
g++-13 in33.txt :heavy_check_mark: AC 18 ms 7 MB
g++-13 in34.txt :heavy_check_mark: AC 17 ms 7 MB
g++-13 in35.txt :heavy_check_mark: AC 18 ms 7 MB
g++-13 in36.txt :heavy_check_mark: AC 19 ms 6 MB
g++-13 in37.txt :heavy_check_mark: AC 18 ms 6 MB
g++-13 in38.txt :heavy_check_mark: AC 18 ms 6 MB
g++-13 in39.txt :heavy_check_mark: AC 18 ms 7 MB
g++-13 in4.txt :heavy_check_mark: AC 6 ms 4 MB
g++-13 in40.txt :heavy_check_mark: AC 18 ms 6 MB
g++-13 in41.txt :heavy_check_mark: AC 18 ms 6 MB
g++-13 in42.txt :heavy_check_mark: AC 18 ms 6 MB
g++-13 in43.txt :heavy_check_mark: AC 18 ms 6 MB
g++-13 in44.txt :heavy_check_mark: AC 18 ms 7 MB
g++-13 in45.txt :heavy_check_mark: AC 19 ms 7 MB
g++-13 in46.txt :heavy_check_mark: AC 18 ms 7 MB
g++-13 in47.txt :heavy_check_mark: AC 19 ms 7 MB
g++-13 in48.txt :heavy_check_mark: AC 18 ms 6 MB
g++-13 in49.txt :heavy_check_mark: AC 19 ms 7 MB
g++-13 in5.txt :heavy_check_mark: AC 6 ms 4 MB
g++-13 in50.txt :heavy_check_mark: AC 18 ms 7 MB
g++-13 in51.txt :heavy_check_mark: AC 18 ms 7 MB
g++-13 in52.txt :heavy_check_mark: AC 5 ms 4 MB
g++-13 in53.txt :heavy_check_mark: AC 5 ms 4 MB
g++-13 in6.txt :heavy_check_mark: AC 5 ms 4 MB
g++-13 in7.txt :heavy_check_mark: AC 5 ms 4 MB
g++-13 in8.txt :heavy_check_mark: AC 5 ms 4 MB
g++-13 in9.txt :heavy_check_mark: AC 17 ms 5 MB
clang++-18 01_sample_01.txt :heavy_check_mark: AC 5 ms 4 MB
clang++-18 01_sample_02.txt :heavy_check_mark: AC 5 ms 4 MB
clang++-18 01_sample_03.txt :heavy_check_mark: AC 5 ms 4 MB
clang++-18 in1.txt :heavy_check_mark: AC 5 ms 4 MB
clang++-18 in10.txt :heavy_check_mark: AC 17 ms 5 MB
clang++-18 in11.txt :heavy_check_mark: AC 20 ms 6 MB
clang++-18 in12.txt :heavy_check_mark: AC 18 ms 5 MB
clang++-18 in13.txt :heavy_check_mark: AC 19 ms 5 MB
clang++-18 in14.txt :heavy_check_mark: AC 17 ms 5 MB
clang++-18 in15.txt :heavy_check_mark: AC 19 ms 5 MB
clang++-18 in16.txt :heavy_check_mark: AC 19 ms 5 MB
clang++-18 in17.txt :heavy_check_mark: AC 19 ms 6 MB
clang++-18 in18.txt :heavy_check_mark: AC 19 ms 6 MB
clang++-18 in19.txt :heavy_check_mark: AC 18 ms 5 MB
clang++-18 in2.txt :heavy_check_mark: AC 6 ms 4 MB
clang++-18 in20.txt :heavy_check_mark: AC 16 ms 5 MB
clang++-18 in21.txt :heavy_check_mark: AC 17 ms 5 MB
clang++-18 in22.txt :heavy_check_mark: AC 19 ms 6 MB
clang++-18 in23.txt :heavy_check_mark: AC 20 ms 6 MB
clang++-18 in24.txt :heavy_check_mark: AC 18 ms 5 MB
clang++-18 in25.txt :heavy_check_mark: AC 18 ms 5 MB
clang++-18 in26.txt :heavy_check_mark: AC 20 ms 6 MB
clang++-18 in27.txt :heavy_check_mark: AC 20 ms 6 MB
clang++-18 in28.txt :heavy_check_mark: AC 20 ms 6 MB
clang++-18 in29.txt :heavy_check_mark: AC 20 ms 6 MB
clang++-18 in3.txt :heavy_check_mark: AC 6 ms 4 MB
clang++-18 in30.txt :heavy_check_mark: AC 19 ms 6 MB
clang++-18 in31.txt :heavy_check_mark: AC 20 ms 6 MB
clang++-18 in32.txt :heavy_check_mark: AC 15 ms 8 MB
clang++-18 in33.txt :heavy_check_mark: AC 17 ms 7 MB
clang++-18 in34.txt :heavy_check_mark: AC 16 ms 6 MB
clang++-18 in35.txt :heavy_check_mark: AC 17 ms 7 MB
clang++-18 in36.txt :heavy_check_mark: AC 18 ms 6 MB
clang++-18 in37.txt :heavy_check_mark: AC 17 ms 6 MB
clang++-18 in38.txt :heavy_check_mark: AC 16 ms 6 MB
clang++-18 in39.txt :heavy_check_mark: AC 17 ms 6 MB
clang++-18 in4.txt :heavy_check_mark: AC 6 ms 4 MB
clang++-18 in40.txt :heavy_check_mark: AC 16 ms 6 MB
clang++-18 in41.txt :heavy_check_mark: AC 16 ms 6 MB
clang++-18 in42.txt :heavy_check_mark: AC 16 ms 6 MB
clang++-18 in43.txt :heavy_check_mark: AC 16 ms 6 MB
clang++-18 in44.txt :heavy_check_mark: AC 16 ms 7 MB
clang++-18 in45.txt :heavy_check_mark: AC 17 ms 6 MB
clang++-18 in46.txt :heavy_check_mark: AC 16 ms 6 MB
clang++-18 in47.txt :heavy_check_mark: AC 17 ms 6 MB
clang++-18 in48.txt :heavy_check_mark: AC 17 ms 6 MB
clang++-18 in49.txt :heavy_check_mark: AC 18 ms 7 MB
clang++-18 in5.txt :heavy_check_mark: AC 6 ms 4 MB
clang++-18 in50.txt :heavy_check_mark: AC 17 ms 7 MB
clang++-18 in51.txt :heavy_check_mark: AC 17 ms 7 MB
clang++-18 in52.txt :heavy_check_mark: AC 5 ms 4 MB
clang++-18 in53.txt :heavy_check_mark: AC 5 ms 4 MB
clang++-18 in6.txt :heavy_check_mark: AC 5 ms 4 MB
clang++-18 in7.txt :heavy_check_mark: AC 5 ms 4 MB
clang++-18 in8.txt :heavy_check_mark: AC 5 ms 4 MB
clang++-18 in9.txt :heavy_check_mark: AC 17 ms 5 MB
Back to top page