結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-10-20 15:56:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 179 ms / 3,500 ms
コード長 4,540 bytes
コンパイル時間 2,374 ms
コンパイル使用メモリ 193,624 KB
実行使用メモリ 12,260 KB
最終ジャッジ日時 2023-09-28 13:42:35
合計ジャッジ時間 11,915 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 6 ms
4,384 KB
testcase_04 AC 6 ms
4,380 KB
testcase_05 AC 6 ms
4,376 KB
testcase_06 AC 6 ms
4,380 KB
testcase_07 AC 6 ms
4,380 KB
testcase_08 AC 6 ms
4,380 KB
testcase_09 AC 6 ms
4,376 KB
testcase_10 AC 7 ms
4,380 KB
testcase_11 AC 131 ms
10,320 KB
testcase_12 AC 136 ms
10,180 KB
testcase_13 AC 175 ms
11,372 KB
testcase_14 AC 138 ms
10,456 KB
testcase_15 AC 158 ms
10,984 KB
testcase_16 AC 131 ms
10,188 KB
testcase_17 AC 156 ms
10,976 KB
testcase_18 AC 158 ms
10,988 KB
testcase_19 AC 163 ms
11,120 KB
testcase_20 AC 174 ms
11,192 KB
testcase_21 AC 151 ms
11,012 KB
testcase_22 AC 135 ms
10,188 KB
testcase_23 AC 135 ms
10,124 KB
testcase_24 AC 162 ms
10,976 KB
testcase_25 AC 171 ms
11,188 KB
testcase_26 AC 156 ms
11,060 KB
testcase_27 AC 142 ms
10,584 KB
testcase_28 AC 173 ms
11,200 KB
testcase_29 AC 175 ms
11,760 KB
testcase_30 AC 176 ms
11,184 KB
testcase_31 AC 179 ms
11,192 KB
testcase_32 AC 174 ms
11,188 KB
testcase_33 AC 173 ms
11,308 KB
testcase_34 AC 139 ms
12,260 KB
testcase_35 AC 130 ms
11,956 KB
testcase_36 AC 131 ms
11,616 KB
testcase_37 AC 135 ms
11,768 KB
testcase_38 AC 119 ms
11,896 KB
testcase_39 AC 115 ms
11,580 KB
testcase_40 AC 120 ms
11,604 KB
testcase_41 AC 120 ms
11,676 KB
testcase_42 AC 118 ms
11,636 KB
testcase_43 AC 115 ms
11,320 KB
testcase_44 AC 114 ms
11,328 KB
testcase_45 AC 112 ms
11,124 KB
testcase_46 AC 130 ms
11,552 KB
testcase_47 AC 124 ms
11,892 KB
testcase_48 AC 124 ms
11,860 KB
testcase_49 AC 130 ms
11,840 KB
testcase_50 AC 122 ms
11,740 KB
testcase_51 AC 136 ms
11,772 KB
testcase_52 AC 137 ms
11,896 KB
testcase_53 AC 135 ms
11,724 KB
testcase_54 AC 2 ms
4,376 KB
testcase_55 AC 1 ms
4,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.10.20 15:56:17
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;


template < class Monoid >
struct SegmentTree {
private:
    using Func = std::function< Monoid(Monoid, Monoid) >;
    Func F;
    Monoid UNITY;
    int n;
    std::vector< Monoid > node;
    int _binary_search(int a, int b, const std::function<bool(Monoid)> &f, Monoid &s, int k = 0, int l = 0, int r = -1) {
        if (r < 0) r = n;
        if (r <= a || b <= l) return n;
        if (a <= l && r <= b && !f(F(s,node[k]))) {
            s = F(s,node[k]);
            return n;
        }
        if (l == r - 1) {s = F(s,node[k]); return l;}
        int ret = _binary_search(a, b, f, s, 2 * k + 1, l, (r - l) / 2 + l);
        return ret != n ? ret : _binary_search(a, b, f, s, 2 * k + 2, (r - l) / 2 + l, r);
    }
public:
    SegmentTree() {}
    SegmentTree(const std::vector< Monoid > &v, const Func f, const Monoid &unity) {
        F = f;
        UNITY = unity;
        int sz = v.size();
        n = 1;
        while (n < sz) n <<= 1;
        node.resize(n * 2 - 1, UNITY);
        for (int i = 0; i < sz; i++) node[i + n - 1] = v[i];
        build();
    }
    
    SegmentTree(int m, const Monoid &val, const Func f, const Monoid &unity) {
        F = f;
        UNITY = unity;
        n = 1;
        while (n < m) n <<= 1;
        node.resize(n * 2 - 1, UNITY);
        if (val != UNITY) {
            for (int i = 0; i < m; i++) node[i + n - 1] = val;
            build();
        }
    }
    
    void set(int k, const Monoid &x) {
        node[n + k - 1] = x;
    }
    void build() {
        for (int i = n - 2; i >= 0; i--) node[i] = F(node[2 * i + 1], node[2 * i + 2]);
    }
    void update_query(int x, const Monoid &val) {
        if (x >= n || x < 0) return;
        x += n - 1;
        node[x] = val;
        while (x > 0) {
            x = (x - 1) >> 1;
            node[x] = F(node[2 * x + 1], node[2 * x + 2]);
        }
    }
    
    Monoid get_query(int l, int r) {
        Monoid L = UNITY, R = UNITY;
        if (l < 0) l = 0;
        if (r < 0) return UNITY;
        if (l >= n) return UNITY;
        if (r >= n) r = n;
        for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if (l & 1) L = F(L, node[l++ - 1]);
            if (r & 1) R = F(node[--r - 1], R);
        }
        return F(L, R);
    }
    
    
    int binary_search(int l, int r, const std::function<bool(Monoid)> &f) {
        Monoid s = UNITY;
        int ret = _binary_search(l,r,f,s);
        return ret != n ? ret : -1;
    }
    Monoid operator[](int x) const {
        return node[n + x - 1];
    }
    int size() {
        return n;
    }
    void print() {
        for (int i = 0; i < n; i++) {
            std::cout << i << "\t: " << node[n + i - 1] << std::endl;
        }
    }
};
int main() {
    int n;cin >> n;
    vector<pair<int,int>> a(n);
    vector<int> big(n),small(n);
    set<int> st;
    for (int i = 0; i < n; i++) {
        cin >> a[i].first;
        a[i].second = i;
    }
    sort(a.begin(), a.end());
    st.insert(-1);
    for (int i = 0; i < n; i++) {
        auto t = st.lower_bound(a[i].second);
        t--;
        small[a[i].second] = *t;
        st.insert(a[i].second);
    }
    st.clear();
    sort(a.rbegin(), a.rend());
    st.insert(-1);
    for (int i = 0; i < n; i++) {
        auto t = st.lower_bound(a[i].second);
        t--;
        big[a[i].second] = *t;
        st.insert(a[i].second);
    }
    SegmentTree<int> seg1(n,0,[](int a,int b){return a+b;},0);
    SegmentTree<int> seg2(n,0,[](int a,int b){return a+b;},0);
    stack<pair<int,int>> sta1, sta2;
    sort(a.begin(), a.end(), [](pair<int,int> a,pair<int,int> b){
        return a.second < b.second;
    });
    ll ans = 0;
    for (int i = 0; i < n; i++) {
        if (i != 0) {
            if (a[i-1].first < a[i].first) {
                ans += seg1.get_query(big[i]+1,i);
            }
            else {
                ans += seg2.get_query(small[i]+1,i);
            }
        }
        while(!sta1.empty() && sta1.top().first > a[i].first) {
            seg1.update_query(sta1.top().second,0);
            sta1.pop();
        }
        seg1.update_query(a[i].second,1);
        sta1.push(a[i]);
        while(!sta2.empty() && sta2.top().first < a[i].first) {
            seg2.update_query(sta2.top().second,0);
            sta2.pop();
        }
        seg2.update_query(a[i].second,1);
        sta2.push(a[i]);
    }
    cout << ans << endl;
    return 0;
}
0