結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー milanis48663220milanis48663220
提出日時 2020-12-03 01:17:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,489 ms / 3,500 ms
コード長 4,042 bytes
コンパイル時間 1,099 ms
コンパイル使用メモリ 102,264 KB
実行使用メモリ 13,284 KB
最終ジャッジ日時 2023-10-11 12:11:50
合計ジャッジ時間 61,928 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,448 KB
testcase_01 AC 2 ms
5,508 KB
testcase_02 AC 2 ms
5,436 KB
testcase_03 AC 34 ms
6,056 KB
testcase_04 AC 34 ms
5,804 KB
testcase_05 AC 33 ms
5,864 KB
testcase_06 AC 33 ms
5,796 KB
testcase_07 AC 33 ms
5,788 KB
testcase_08 AC 33 ms
5,992 KB
testcase_09 AC 34 ms
5,792 KB
testcase_10 AC 33 ms
5,808 KB
testcase_11 AC 1,157 ms
12,908 KB
testcase_12 AC 1,157 ms
12,984 KB
testcase_13 AC 1,433 ms
12,996 KB
testcase_14 AC 1,204 ms
13,128 KB
testcase_15 AC 1,339 ms
12,956 KB
testcase_16 AC 1,132 ms
13,004 KB
testcase_17 AC 1,345 ms
12,964 KB
testcase_18 AC 1,287 ms
13,072 KB
testcase_19 AC 1,358 ms
13,032 KB
testcase_20 AC 1,429 ms
12,992 KB
testcase_21 AC 1,289 ms
12,948 KB
testcase_22 AC 1,134 ms
12,916 KB
testcase_23 AC 1,172 ms
12,964 KB
testcase_24 AC 1,364 ms
12,968 KB
testcase_25 AC 1,416 ms
13,200 KB
testcase_26 AC 1,313 ms
12,952 KB
testcase_27 AC 1,217 ms
13,044 KB
testcase_28 AC 1,434 ms
13,204 KB
testcase_29 AC 1,439 ms
12,988 KB
testcase_30 AC 1,437 ms
13,108 KB
testcase_31 AC 1,433 ms
12,996 KB
testcase_32 AC 1,438 ms
13,052 KB
testcase_33 AC 1,437 ms
13,056 KB
testcase_34 AC 808 ms
13,000 KB
testcase_35 AC 1,463 ms
12,996 KB
testcase_36 AC 1,370 ms
13,004 KB
testcase_37 AC 1,489 ms
13,008 KB
testcase_38 AC 1,426 ms
12,988 KB
testcase_39 AC 1,383 ms
12,984 KB
testcase_40 AC 1,420 ms
12,980 KB
testcase_41 AC 1,417 ms
13,248 KB
testcase_42 AC 1,352 ms
13,048 KB
testcase_43 AC 1,436 ms
13,004 KB
testcase_44 AC 1,418 ms
13,176 KB
testcase_45 AC 1,378 ms
13,284 KB
testcase_46 AC 1,409 ms
12,992 KB
testcase_47 AC 1,317 ms
13,236 KB
testcase_48 AC 1,173 ms
13,008 KB
testcase_49 AC 1,465 ms
13,072 KB
testcase_50 AC 1,373 ms
12,996 KB
testcase_51 AC 1,193 ms
12,996 KB
testcase_52 AC 1,192 ms
13,020 KB
testcase_53 AC 1,199 ms
13,052 KB
testcase_54 AC 2 ms
5,452 KB
testcase_55 AC 2 ms
5,440 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(cur == -1) break;
        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