結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー betrue12betrue12
提出日時 2020-04-17 23:19:07
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 151 ms / 3,500 ms
コード長 3,853 bytes
コンパイル時間 2,598 ms
コンパイル使用メモリ 212,040 KB
実行使用メモリ 10,616 KB
最終ジャッジ日時 2024-04-14 15:34:07
合計ジャッジ時間 10,102 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 6 ms
6,940 KB
testcase_04 AC 6 ms
6,940 KB
testcase_05 AC 7 ms
6,944 KB
testcase_06 AC 6 ms
6,940 KB
testcase_07 AC 6 ms
6,944 KB
testcase_08 AC 6 ms
6,944 KB
testcase_09 AC 7 ms
6,944 KB
testcase_10 AC 6 ms
6,940 KB
testcase_11 AC 121 ms
8,320 KB
testcase_12 AC 122 ms
8,248 KB
testcase_13 AC 150 ms
9,124 KB
testcase_14 AC 127 ms
8,452 KB
testcase_15 AC 141 ms
8,752 KB
testcase_16 AC 119 ms
8,092 KB
testcase_17 AC 143 ms
8,776 KB
testcase_18 AC 135 ms
8,704 KB
testcase_19 AC 143 ms
8,764 KB
testcase_20 AC 151 ms
9,012 KB
testcase_21 AC 133 ms
8,628 KB
testcase_22 AC 121 ms
8,100 KB
testcase_23 AC 122 ms
8,192 KB
testcase_24 AC 141 ms
8,832 KB
testcase_25 AC 147 ms
8,980 KB
testcase_26 AC 136 ms
8,576 KB
testcase_27 AC 131 ms
8,320 KB
testcase_28 AC 141 ms
9,280 KB
testcase_29 AC 142 ms
8,992 KB
testcase_30 AC 145 ms
9,032 KB
testcase_31 AC 143 ms
9,032 KB
testcase_32 AC 143 ms
9,120 KB
testcase_33 AC 144 ms
9,092 KB
testcase_34 AC 111 ms
10,588 KB
testcase_35 AC 120 ms
10,544 KB
testcase_36 AC 118 ms
10,616 KB
testcase_37 AC 121 ms
10,504 KB
testcase_38 AC 129 ms
9,600 KB
testcase_39 AC 124 ms
9,640 KB
testcase_40 AC 130 ms
9,472 KB
testcase_41 AC 125 ms
9,736 KB
testcase_42 AC 116 ms
9,472 KB
testcase_43 AC 129 ms
9,476 KB
testcase_44 AC 125 ms
9,272 KB
testcase_45 AC 121 ms
9,140 KB
testcase_46 AC 128 ms
9,808 KB
testcase_47 AC 126 ms
9,876 KB
testcase_48 AC 123 ms
9,564 KB
testcase_49 AC 131 ms
9,516 KB
testcase_50 AC 125 ms
9,492 KB
testcase_51 AC 138 ms
9,544 KB
testcase_52 AC 136 ms
9,672 KB
testcase_53 AC 136 ms
9,436 KB
testcase_54 AC 2 ms
6,940 KB
testcase_55 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<typename T>
struct BIT {
    int n;
    vector<T> dat;

    BIT(int n=0){
        initialize(n);
    }

    void initialize(int nin){
        n = nin;
        dat.resize(n, 0);
    }

    T sum(int i){
        T s = 0;
        while(i >= 0){
            s += dat[i];
            i = (i & (i+1)) - 1;
        }
        return s;
    }

    T sum_between(int i, int j){
        return sum(j) - sum(i-1);
    }

    void plus(int i, T x){
        while(i < n){
            dat[i] += x;
            i |= i+1;
        }
    }

    // a[0]+...+a[ret] >= x
    int lower_bound(T x){
        if(x < 0) return -1;
        int ret = -1;
        int k = 1;
        while(2*k <= n) k <<= 1;
        for( ;k>0; k>>=1){
            if(ret+k < n && dat[ret+k] < x){
                x -= dat[ret+k];
                ret += k;
            }
        }
        return ret + 1;
    }
};

template<typename T>
struct Segtree {
    int n, n_org;
    T e;
    vector<T> dat;
    typedef function<T(T a, T b)> Func;
    Func f;

    Segtree(){}
    Segtree(int n_input, Func f_input, T e_input){
        initialize(n_input, f_input, e_input);
    }
    void initialize(int n_input, Func f_input, T e_input){
        n_org = n_input;
        f = f_input;
        e = e_input;
        n = 1;
        while(n < n_input) n <<= 1;
        dat.resize(2*n-1, e);
    }

    void build(vector<T>& A){
        for(int k=0; k<int(A.size()); k++) dat[k+n-1] = A[k];
        for(int k=n-2; k>=0; k--) dat[k] = f(dat[2*k+1], dat[2*k+2]);
    }

    void update(int k, T a){
        k += n - 1;
        dat[k] = a;
        while(k > 0){
            k = (k - 1)/2;
            dat[k] = f(dat[2*k+1], dat[2*k+2]);
        }
    }

    T get(int k){
        return dat[k+n-1];
    }

    T between(int a, int b){
        return query(a, b+1, 0, 0, n);
    }

    T query(int a, int b, int k, int l, int r){
        if(r<=a || b<=l) return e;
        if(a<=l && r<=b) return dat[k];
        T vl = query(a, b, 2*k+1, l, (l+r)/2);
        T vr = query(a, b, 2*k+2, (l+r)/2, r);
        return f(vl, vr);
    }

    // [S, t] が条件checkを満たす最大のtを求める
    int bisect(int S, function<bool(T a)> check){
        T val = get(S);
        int k = S+n-1, l = S, r = S+1;
        while(true){
            while(k%2) k = (k-1)/2, r += r-l;
            T val2 = f(val, dat[k]);
            if(check(val2)){
                if(r == n) return n_org-1;
                val = val2;
                k++;
                int d = r-l;
                l += d, r += d;
            }else{
                break;
            }
        }
        while(k<n-1){
            T val2 = f(val, dat[2*k+1]);
            if(check(val2)){
                val = val2;
                k = 2*k+2;
                l = (l+r)/2;
            }else{
                k = 2*k+1;
                r = (l+r)/2;
            }
        }
        return min(l, n_org)-1;
    }
};

int main(){
    int N;
    cin >> N;
    vector<int> P(N);
    for(int i=0; i<N; i++) cin >> P[i], P[i]--;
    int64_t ans = 0;
    for(int t=0; t<2; t++){
        priority_queue<pair<int, int>> que;
        vector<vector<int>> out(N);
        for(int i=0; i<N; i++){
            while(que.size() && que.top().first > P[i]){
                out[i].push_back(que.top().second);
                que.pop();
            }
            que.emplace(P[i], i);
        }
        BIT<int> bit(N);
        Segtree<int> st(N, [](int a, int b){ return max(a, b); }, -1);
        for(int i=0; i<N; i++){
            int L = st.between(P[i]+1, N-1) + 1;
            ans += bit.sum_between(L, i);
            bit.plus(i, 1);
            for(int a : out[i]) bit.plus(a, -1);
            st.update(P[i], i);
        }
        reverse(P.begin(), P.end());
    }
    cout << ans << endl;
    return 0;
}
0