結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー kimiyukikimiyuki
提出日時 2020-04-17 23:41:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 367 ms / 3,500 ms
コード長 10,210 bytes
コンパイル時間 3,216 ms
コンパイル使用メモリ 227,712 KB
実行使用メモリ 23,504 KB
最終ジャッジ日時 2024-04-14 15:43:11
合計ジャッジ時間 10,349 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 3 ms
6,944 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 4 ms
6,944 KB
testcase_09 AC 4 ms
6,940 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 43 ms
12,556 KB
testcase_12 AC 45 ms
13,980 KB
testcase_13 AC 55 ms
14,484 KB
testcase_14 AC 47 ms
15,256 KB
testcase_15 AC 52 ms
16,472 KB
testcase_16 AC 41 ms
11,848 KB
testcase_17 AC 50 ms
15,776 KB
testcase_18 AC 48 ms
14,352 KB
testcase_19 AC 49 ms
14,688 KB
testcase_20 AC 53 ms
15,520 KB
testcase_21 AC 49 ms
14,348 KB
testcase_22 AC 42 ms
11,976 KB
testcase_23 AC 46 ms
15,176 KB
testcase_24 AC 50 ms
13,896 KB
testcase_25 AC 52 ms
14,360 KB
testcase_26 AC 50 ms
15,464 KB
testcase_27 AC 45 ms
13,468 KB
testcase_28 AC 52 ms
14,668 KB
testcase_29 AC 55 ms
16,412 KB
testcase_30 AC 53 ms
16,032 KB
testcase_31 AC 52 ms
14,548 KB
testcase_32 AC 53 ms
15,644 KB
testcase_33 AC 54 ms
15,908 KB
testcase_34 AC 367 ms
22,528 KB
testcase_35 AC 169 ms
18,228 KB
testcase_36 AC 200 ms
20,504 KB
testcase_37 AC 114 ms
21,436 KB
testcase_38 AC 153 ms
17,384 KB
testcase_39 AC 171 ms
19,388 KB
testcase_40 AC 149 ms
17,268 KB
testcase_41 AC 149 ms
17,904 KB
testcase_42 AC 168 ms
18,468 KB
testcase_43 AC 106 ms
18,344 KB
testcase_44 AC 115 ms
17,248 KB
testcase_45 AC 96 ms
17,028 KB
testcase_46 AC 105 ms
22,884 KB
testcase_47 AC 191 ms
20,392 KB
testcase_48 AC 237 ms
23,504 KB
testcase_49 AC 154 ms
22,256 KB
testcase_50 AC 194 ms
21,536 KB
testcase_51 AC 361 ms
17,656 KB
testcase_52 AC 363 ms
17,784 KB
testcase_53 AC 362 ms
17,796 KB
testcase_54 AC 2 ms
6,940 KB
testcase_55 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "main.cpp"
#include <bits/stdc++.h>
#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 <class IdempotentMonoid>
struct sparse_table {
    typedef typename IdempotentMonoid::value_type value_type;
    std::vector<std::vector<value_type> > table;
    IdempotentMonoid mon;
    sparse_table() = default;

    /**
     * @note $O(N \log N)$ time
     */
    template <class InputIterator>
    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<value_type>(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 <class Monoid>
struct segment_tree {
    typedef typename Monoid::value_type value_type;
    const Monoid mon;
    int n;
    std::vector<value_type> 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 <class InputIterator>
    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 <class T>
struct max_monoid {
    typedef T value_type;
    value_type unit() const { return std::numeric_limits<T>::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 <class Func, class RollbackMoInterface>
void rollback_square_decomposition(int n, vector<pair<int, int> > const & range, RollbackMoInterface & mo, Func stupid) {
    int bucket_size = sqrt(n);
    int bucket_count = (n + bucket_size - 1) / bucket_size;
    vector<vector<int> > 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 <class T>
struct rollbackable_deque {
    deque<T> data;
    vector<pair<char, optional<T> > > 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<T>());
        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<T>());
        data.push_back(value);
    }
    void pop_back() {
        history.emplace_back('B', data.back());
        data.pop_back();
    }
    void snapshot() {
        history.emplace_back('$', optional<T>());
    }
    void rollback() {
        while (history.back().first != '$') {
            char op = history.back().first;
            optional<T> 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<int> & p;
    int64_t & ans;
    int l, r;
    vector<pair<int, int> > history;
    rollbackable_deque<int> deq;
    rollback_mo_inc(const vector<int> & p_, int64_t & ans_) : p(p_), ans(ans_) {
        reset(0);
    }
    void reset(int l_) {
        l = l_;
        r = l_;
        history.clear();
        deq = rollbackable_deque<int>();
    }
    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<int> & p) {
    vector<pair<int, int> > query_inc;
    vector<int> lookup(n);
    REP (i, n) {
        lookup[p[i]] = i;
    }
    sparse_table<max_monoid<int> > table(ALL(p));
    function<void (int, int)> 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<int> 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<int> p(N);
    REP (i, N) {
        cin >> p[i];
        -- p[i];
    }
    auto ans = solve(N, p);
    cout << ans << endl;
    return 0;
}
0