結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー knshnbknshnb
提出日時 2020-04-19 19:07:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 28 ms / 3,500 ms
コード長 3,042 bytes
コンパイル時間 2,362 ms
コンパイル使用メモリ 202,904 KB
実行使用メモリ 8,304 KB
最終ジャッジ日時 2024-04-15 21:12:27
合計ジャッジ時間 5,428 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 22 ms
6,504 KB
testcase_12 AC 21 ms
6,240 KB
testcase_13 AC 27 ms
7,148 KB
testcase_14 AC 23 ms
6,560 KB
testcase_15 AC 25 ms
6,940 KB
testcase_16 AC 22 ms
6,360 KB
testcase_17 AC 24 ms
6,976 KB
testcase_18 AC 23 ms
6,940 KB
testcase_19 AC 25 ms
6,984 KB
testcase_20 AC 26 ms
7,020 KB
testcase_21 AC 22 ms
6,824 KB
testcase_22 AC 21 ms
6,356 KB
testcase_23 AC 22 ms
6,636 KB
testcase_24 AC 25 ms
6,864 KB
testcase_25 AC 27 ms
7,020 KB
testcase_26 AC 24 ms
6,828 KB
testcase_27 AC 23 ms
6,400 KB
testcase_28 AC 26 ms
7,152 KB
testcase_29 AC 26 ms
7,152 KB
testcase_30 AC 26 ms
7,152 KB
testcase_31 AC 27 ms
7,284 KB
testcase_32 AC 26 ms
7,152 KB
testcase_33 AC 27 ms
7,156 KB
testcase_34 AC 27 ms
8,304 KB
testcase_35 AC 26 ms
7,188 KB
testcase_36 AC 27 ms
7,432 KB
testcase_37 AC 27 ms
7,596 KB
testcase_38 AC 26 ms
7,024 KB
testcase_39 AC 25 ms
6,940 KB
testcase_40 AC 26 ms
7,048 KB
testcase_41 AC 27 ms
7,032 KB
testcase_42 AC 26 ms
7,088 KB
testcase_43 AC 26 ms
7,008 KB
testcase_44 AC 25 ms
7,128 KB
testcase_45 AC 25 ms
6,956 KB
testcase_46 AC 26 ms
7,552 KB
testcase_47 AC 26 ms
7,212 KB
testcase_48 AC 27 ms
7,672 KB
testcase_49 AC 27 ms
7,172 KB
testcase_50 AC 25 ms
7,200 KB
testcase_51 AC 27 ms
7,936 KB
testcase_52 AC 27 ms
7,940 KB
testcase_53 AC 28 ms
7,936 KB
testcase_54 AC 2 ms
5,376 KB
testcase_55 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>  // clang-format off
using Int = long long;
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef _MY_DEBUG
#define dump(...)
#endif  // clang-format on

/**
 *    author:  knshnb
 *    created: Sat Apr 18 21:27:51 JST 2020
 **/

/// @docs src/DataStructure/SegmentTree.md
template <class T, class F> struct SegmentTree {
    const F op;
    const T e;
    SegmentTree(F op_, T e_) : op(op_), e(e_) {}
    int n;
    std::vector<T> t;
    void set_by_identity(int n_) {
        n = n_;
        t.clear(), t.resize(2 * n, e);
    }
    void set_by_vector(const std::vector<T>& a) {
        n = a.size();
        t.clear(), t.resize(2 * n, e);
        for (int i = 0; i < n; i++) t[i + n] = a[i];
        build();
    }
    void build() {
        for (int i = n - 1; i; --i) t[i] = op(t[2 * i], t[2 * i + 1]);
    }
    T& operator[](int i) { return t[i + n]; }
    // [l, r)
    T query(int l, int r) const {
        assert(0 <= l && l <= r && r <= n);
        T resl = e, resr = e;
        for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if (l & 1) resl = op(resl, t[l++]);
            if (r & 1) resr = op(t[--r], resr);
        }
        return op(resl, resr);
    }
    // i番目をxに変更
    void update(int i, const T& x) {
        assert(0 <= i && i < n);
        t[i += n] = x;
        while (i >>= 1) t[i] = op(t[2 * i], t[2 * i + 1]);
    }
    // i番目にxを作用 (a[i] = op(a[i], x))
    void operate(int i, const T& x) {
        assert(0 <= i && i < n);
        i += n;
        t[i] = op(t[i], x);
        while (i >>= 1) t[i] = op(t[2 * i], t[2 * i + 1]);
    }
    friend std::string to_string(const SegmentTree<T, F>& seg) {
        return to_string(std::vector<T>(seg.t.begin() + seg.n, seg.t.end()));
    }
};
template <class T, class F> auto make_segment_tree(F op, T e) { return SegmentTree<T, F>(op, e); }

const Int INF = 1e18;
// 左端がmin, 右端がmax
Int solve(const std::vector<Int>& a) {
    Int n = a.size();
    std::vector<Int> mis, mas;
    auto seg = make_segment_tree<Int>(std::plus<Int>(), 0);
    seg.set_by_vector(std::vector<Int>(n, 1));
    Int ret = 0;
    REP(i, n) {
        while (mis.size() && a[mis.back()] > a[i]) {
            assert(seg[mis.back()] == 1);
            seg.update(mis.back(), 0);
            mis.pop_back();
        }
        mis.push_back(i);
        while (mas.size() && a[mas.back()] < a[i]) {
            mas.pop_back();
        }
        ret += seg.query(mas.empty() ? 0 : mas.back() + 1, i);
        mas.push_back(i);
    }
    return ret;
}

signed main() {
    Int n;
    std::cin >> n;
    std::vector<Int> p(n);
    REP(i, n) std::cin >> p[i];
    Int ans = solve(p);
    ans += solve(std::vector<Int>(p.rbegin(), p.rend()));
    std::cout << ans << std::endl;
}
0