結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー milanis48663220milanis48663220
提出日時 2020-12-03 01:14:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 4,013 bytes
コンパイル時間 1,231 ms
コンパイル使用メモリ 101,028 KB
実行使用メモリ 13,000 KB
最終ジャッジ日時 2023-10-11 12:10:34
合計ジャッジ時間 31,536 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,428 KB
testcase_01 AC 2 ms
5,480 KB
testcase_02 AC 2 ms
5,428 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 RE -
testcase_49 RE -
testcase_50 RE -
testcase_51 RE -
testcase_52 RE -
testcase_53 RE -
testcase_54 AC 2 ms
5,476 KB
testcase_55 AC 2 ms
5,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template <typename T>
struct segtree{
    int n;
    T UNIT;
    vector<T> dat;
    T (*calc)(T, T);

    segtree(int n_, T unit, T (*_calc)(T, T)){
        UNIT = unit;
        calc = _calc;
        n = 1;
        while(n < n_) n *= 2;
        dat = vector<T>(2*n);
        for(int i = 0; i < 2*n; i++) dat[i] = UNIT;
    }

    void insert(int k, T a){
        dat[k+n-1] = a;
    }
    void update_all(){
        for(int i = n-2; i >= 0; i--){
            dat[i] = calc(dat[i*2+1], dat[i*2+2]);
        }
    }
    //k番目の値(0-indexed)をaに変更
    void update(int k, T a){
        k += n-1;
        dat[k] = a;
        while(k > 0){
            k = (k-1)/2;
            dat[k] = calc(dat[k*2+1], dat[k*2+2]);
        }
    }

    //[a, b)
    //区間[a, b]へのクエリに対してはquery(a, b+1)と呼ぶ
    T query(int a, int b, int k=0, int l=0, int r=-1){
        if(r < 0) r = n;
        if(r <= a || b <= l) return UNIT;
        if(a <= l && r <= b) return dat[k];
        else{
            T vl = query(a, b, k*2+1, l, (l+r)/2);
            T vr = query(a, b, k*2+2, (l+r)/2, r);
            return calc(vl, vr);
        }
    }
};

int calc_min(int a, int b){ return min(a, b); }
int calc_max(int a, int b){ return max(a, b); }

int n;
int p[100000];
int idx[100001];
int left_large[100000][20];

ll ans;

void reverse_p(){
    vector<int> q(n);
    for(int i = 0; i < n; i++) q[i] = p[i];
    for(int i = 0; i < n; i++) p[i] = q[n-i-1];
    for(int i = 0; i < n; i++) idx[p[i]] = i;
}

const int INF = 1e9;

void make_left_large(){
    left_large[0][0] = -1;
    segtree<int> sgt(n, -INF, calc_max);
    for(int i = 0; i < n; i++) sgt.update(i, p[i]);
    for(int i = 1; i < n; i++){
        int l = 0, r = i;
        if(sgt.query(l, i+1) == p[i]){
            left_large[i][0] = -1;
            continue;
        }
        while(r-l > 1){
            int c = (l+r)/2;
            if(sgt.query(c, i+1) == p[i]) r = c;
            else l = c;
        }
        left_large[i][0] = l;
    }
    for(int i = 1; i < 20; i++){
        for(int j = 0; j < n; j++){
            if(left_large[j][i-1] == -1){
                left_large[j][i] = -1;
                continue;
            }
            left_large[j][i] = left_large[left_large[j][i-1]][i-1];
        }
    }
}

// iからmだけ左に進む
int move_left(int i, int m){
    int cur = i;
    for(int j = 0; j < 20; j++){
        if(m&(1<<j)) cur = left_large[cur][j];
    }
    return cur;
}

void count(){
    make_left_large();
    segtree<int> sgt(n, INF, calc_min);
    for(int i = 0; i < n; i++) sgt.update(i, p[i]);
    for(int i = 1; i < n; i++){
        int l = 0, r = i;
        if(sgt.query(0, i+1) < p[i]){
            while(r-l > 1){
                int c = (l+r)/2;
                if(sgt.query(c, i+1) < p[i]) l = c;
                else r = c;
            }
        }else{
            r = 0;
        }
        if(r == i) continue;
        // [r, i]の範囲で何ペア作成できるかを数える
        int s = 0, t = n+1;
        while(t-s > 1){
            int c = (s+t)/2;
            if(move_left(i, c) < r) t = c;
            else s =c;
        }
        ans += s;
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> p[i];
        idx[p[i]] = i;
    }
    count();
    reverse_p();
    count();
    cout << ans << endl;
}
0