#line 1 "main.cpp" #include #line 2 "/home/user/GitHub/competitive-programming-library/utils/macros.hpp" #define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i)) #define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i)) #define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i)) #define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i)) #define ALL(x) std::begin(x), std::end(x) #line 5 "/home/user/GitHub/competitive-programming-library/data_structure/sparse_table.hpp" /** * @brief Sparse Table (idempotent monoid) * @note the unit is required just for convenience * @note $O(N \log N)$ space */ template struct sparse_table { typedef typename IdempotentMonoid::value_type value_type; std::vector > table; IdempotentMonoid mon; sparse_table() = default; /** * @note $O(N \log N)$ time */ template sparse_table(InputIterator first, InputIterator last, const IdempotentMonoid & mon_ = IdempotentMonoid()) : mon(mon_) { table.emplace_back(first, last); int n = table[0].size(); int log_n = 32 - __builtin_clz(n); table.resize(log_n, std::vector(n)); REP (k, log_n - 1) { REP (i, n) { table[k + 1][i] = i + (1ll << k) < n ? mon.mult(table[k][i], table[k][i + (1ll << k)]) : table[k][i]; } } } /** * @note $O(1)$ */ value_type range_get(int l, int r) const { if (l == r) return mon.unit(); // if there is no unit, remove this line assert (0 <= l and l < r and r <= (int)table[0].size()); int k = 31 - __builtin_clz(r - l); // log2 return mon.mult(table[k][l], table[k][r - (1ll << k)]); } }; #line 6 "/home/user/GitHub/competitive-programming-library/data_structure/segment_tree.hpp" /** * @brief Segment Tree / セグメント木 (monoids, 完全二分木) * @docs data_structure/segment_tree.md * @tparam Monoid (commutativity is not required) */ template struct segment_tree { typedef typename Monoid::value_type value_type; const Monoid mon; int n; std::vector a; segment_tree() = default; segment_tree(int n_, const Monoid & mon_ = Monoid()) : mon(mon_) { n = 1; while (n < n_) n *= 2; a.resize(2 * n - 1, mon.unit()); } void point_set(int i, value_type b) { // 0-based assert (0 <= i and i < n); a[i + n - 1] = b; for (i = (i + n) / 2; i > 0; i /= 2) { // 1-based a[i - 1] = mon.mult(a[2 * i - 1], a[2 * i]); } } value_type range_get(int l, int r) { // 0-based, [l, r) assert (0 <= l and l <= r and r <= n); value_type lacc = mon.unit(), racc = mon.unit(); for (l += n, r += n; l < r; l /= 2, r /= 2) { // 1-based loop, 2x faster than recursion if (l % 2 == 1) lacc = mon.mult(lacc, a[(l ++) - 1]); if (r % 2 == 1) racc = mon.mult(a[(-- r) - 1], racc); } return mon.mult(lacc, racc); } value_type point_get(int i) { // 0-based assert (0 <= i and i < n); return a[i + n - 1]; } /** * @brief a fast & semigroup-friendly version constructor * @note $O(n)$ */ template segment_tree(InputIterator first, InputIterator last, const Monoid & mon_ = Monoid()) : mon(mon_) { int size = std::distance(first, last); n = 1; while (n < size) n *= 2; a.resize(2 * n - 1, mon.unit()); std::copy(first, last, a.begin() + (n - 1)); unsafe_rebuild(); } /** * @brief update a leaf node without updating ancestors * @note $O(1)$ */ void unsafe_point_set(int i, value_type b) { // 0-based assert (0 <= i and i < n); a[i + n - 1] = b; } /** * @brief re-build non-leaf nodes from leaf nodes * @note $O(n)$ */ void unsafe_rebuild() { REP_R (i, n - 1) { a[i] = mon.mult(a[2 * i + 1], a[2 * i + 2]); } } }; #line 4 "/home/user/GitHub/competitive-programming-library/monoids/max.hpp" template struct max_monoid { typedef T value_type; value_type unit() const { return std::numeric_limits::lowest(); } value_type mult(value_type a, value_type b) const { return std::max(a, b); } }; #line 6 "main.cpp" using namespace std; #line 1 "/home/user/GitHub/competitive-programming-library/old/rollback-square-decomposition.inc.cpp" /** * @brief the extended Mo's algorithm * @arg stupid is called O(Q) times, each length is O(\sqrt{N}) * @arg mo si the following: * struct rollback_mo_interface { * void reset(int l); // called O(N) times * void extend_left( int l, int r); // called O(Q) times, the sum of length is O(N \sqrt {N}) * void extend_right(int l, int r); // called O(Q) times, the sum of length is O(Q \sqrt {N}) * void snapshot(); // called O(Q) times * void rollback(); // called O(Q) times * void query(); // called O(Q) times * }; * @see http://snuke.hatenablog.com/entry/2016/07/01/000000 * @see http://codeforces.com/blog/entry/7383?#comment-161520 */ template void rollback_square_decomposition(int n, vector > const & range, RollbackMoInterface & mo, Func stupid) { int bucket_size = sqrt(n); int bucket_count = (n + bucket_size - 1) / bucket_size; vector > bucket(bucket_count); REP (i, int(range.size())) { int l, r; tie(l, r) = range[i]; if (r - l <= bucket_size) { stupid(l, r); } else { bucket[l / bucket_size].push_back(i); } } REP (b, bucket_count) { sort(ALL(bucket[b]), [&](int i, int j) { return range[i].second < range[j].second; }); int l = (b + 1) * bucket_size; mo.reset(l); int r = l; for (int i : bucket[b]) { int l_i, r_i; tie(l_i, r_i) = range[i]; mo.extend_right(r, r_i); mo.snapshot(); mo.extend_left(l_i, l); mo.query(); mo.rollback(); r = r_i; } } } #line 8 "main.cpp" template struct rollbackable_deque { deque data; vector > > history; rollbackable_deque() = default; bool empty() const { return data.empty(); } size_t size() const { return data.size(); } T operator [] (size_t i) const { return data[i]; } const T & front() const { return data.front(); } const T & back() const { return data.back(); } void push_front(T value) { history.emplace_back('f', optional()); data.push_front(value); } void pop_front() { history.emplace_back('F', data.front()); data.pop_front(); } void push_back(T value) { history.emplace_back('b', optional()); data.push_back(value); } void pop_back() { history.emplace_back('B', data.back()); data.pop_back(); } void snapshot() { history.emplace_back('$', optional()); } void rollback() { while (history.back().first != '$') { char op = history.back().first; optional value = history.back().second; history.pop_back(); if (op == 'f') { data.pop_front(); } else if (op == 'F') { data.push_front(*value); } else if (op == 'b') { data.pop_back(); } else if (op == 'B') { data.push_back(*value); } } history.pop_back(); } }; struct rollback_mo_inc { const vector & p; int64_t & ans; int l, r; vector > history; rollbackable_deque deq; rollback_mo_inc(const vector & p_, int64_t & ans_) : p(p_), ans(ans_) { reset(0); } void reset(int l_) { l = l_; r = l_; history.clear(); deq = rollbackable_deque(); } void extend_left(int nl, int r) { for (; nl < l; -- l) { if (deq.empty() or p[l - 1] < deq.front()) { deq.push_front(p[l - 1]); } } } void extend_right(int l, int nr) { for (; r < nr; ++ r) { while (not deq.empty() and p[r] < deq.back()) { deq.pop_back(); } deq.push_back(p[r]); } } void snapshot() { deq.snapshot(); history.emplace_back(l, r); } void rollback() { deq.rollback(); tie(l, r) = history.back(); history.pop_back(); } void query() { ans += deq.size(); } }; int64_t solve1(int n, const vector & p) { vector > query_inc; vector lookup(n); REP (i, n) { lookup[p[i]] = i; } sparse_table > table(ALL(p)); function go = [&](int l, int r) { if (r - l < 2) return; int m = lookup[table.range_get(l, r)]; query_inc.emplace_back(l, m); go(l, m); go(m + 1, r); }; go(0, n); int64_t ans = 0; rollback_mo_inc interface_inc(p, ans); rollback_square_decomposition(n, query_inc, interface_inc, [&](int l, int r) { int last = INT_MAX; REP3R (i, l, r) { if (p[i] < last) { last = p[i]; ++ ans; } } }); return ans; } int64_t solve(int n, vector p) { int64_t ans = solve1(n, p); reverse(ALL(p)); return ans + solve1(n, p); } // generated by online-judge-template-generator v4.1.0 (https://github.com/kmyk/online-judge-template-generator) int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); constexpr char endl = '\n'; int N; cin >> N; vector p(N); REP (i, N) { cin >> p[i]; -- p[i]; } auto ans = solve(N, p); cout << ans << endl; return 0; }