結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2020-04-17 23:18:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 180 ms / 3,500 ms
コード長 6,135 bytes
コンパイル時間 2,445 ms
コンパイル使用メモリ 209,056 KB
実行使用メモリ 48,864 KB
最終ジャッジ日時 2023-07-27 02:12:53
合計ジャッジ時間 10,982 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
20,856 KB
testcase_01 AC 8 ms
20,816 KB
testcase_02 AC 9 ms
20,768 KB
testcase_03 AC 12 ms
21,556 KB
testcase_04 AC 12 ms
21,712 KB
testcase_05 AC 13 ms
21,552 KB
testcase_06 AC 12 ms
21,616 KB
testcase_07 AC 12 ms
21,588 KB
testcase_08 AC 13 ms
21,764 KB
testcase_09 AC 13 ms
21,572 KB
testcase_10 AC 13 ms
21,760 KB
testcase_11 AC 128 ms
46,268 KB
testcase_12 AC 127 ms
46,292 KB
testcase_13 AC 147 ms
46,512 KB
testcase_14 AC 132 ms
46,312 KB
testcase_15 AC 139 ms
46,628 KB
testcase_16 AC 126 ms
46,232 KB
testcase_17 AC 142 ms
46,492 KB
testcase_18 AC 137 ms
46,368 KB
testcase_19 AC 142 ms
46,524 KB
testcase_20 AC 148 ms
46,556 KB
testcase_21 AC 136 ms
46,540 KB
testcase_22 AC 126 ms
46,288 KB
testcase_23 AC 128 ms
46,300 KB
testcase_24 AC 143 ms
46,656 KB
testcase_25 AC 147 ms
46,524 KB
testcase_26 AC 140 ms
46,568 KB
testcase_27 AC 132 ms
46,236 KB
testcase_28 AC 147 ms
46,516 KB
testcase_29 AC 147 ms
46,536 KB
testcase_30 AC 146 ms
46,628 KB
testcase_31 AC 147 ms
46,544 KB
testcase_32 AC 147 ms
46,508 KB
testcase_33 AC 148 ms
46,600 KB
testcase_34 AC 166 ms
48,864 KB
testcase_35 AC 162 ms
46,924 KB
testcase_36 AC 164 ms
47,044 KB
testcase_37 AC 163 ms
47,520 KB
testcase_38 AC 160 ms
46,496 KB
testcase_39 AC 159 ms
46,636 KB
testcase_40 AC 159 ms
46,616 KB
testcase_41 AC 160 ms
46,600 KB
testcase_42 AC 162 ms
46,524 KB
testcase_43 AC 159 ms
46,552 KB
testcase_44 AC 162 ms
46,504 KB
testcase_45 AC 156 ms
46,592 KB
testcase_46 AC 163 ms
47,740 KB
testcase_47 AC 158 ms
46,624 KB
testcase_48 AC 161 ms
46,504 KB
testcase_49 AC 164 ms
46,520 KB
testcase_50 AC 162 ms
47,004 KB
testcase_51 AC 179 ms
47,212 KB
testcase_52 AC 180 ms
47,188 KB
testcase_53 AC 177 ms
47,284 KB
testcase_54 AC 8 ms
20,804 KB
testcase_55 AC 8 ms
20,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
#ifdef _MSC_VER
inline unsigned int __builtin_clz(unsigned int x){unsigned long r;_BitScanReverse(&r,x);return 31-r;}
#endif // _MSC_VER
template<class V> struct SparseTable { // [L,R)
    const V def = make_pair(-1,-1);
    inline V comp(V a, V b) { return max(a,b); }

    int n; vector<V> a, b[22]; inline int __lg(int x) { return 32 - 1 - __builtin_clz(x); }
    void init(vector<V> v) {
        int nn = v.size(); n = 1; while (n < nn) n *= 2; a.resize(n);
        rep(i, 0, 22) b[i].resize(n); rep(i, 0, nn) a[i] = v[i];

        int d = 1 << __lg(n - 1), e = d << 1;
        for (int h = 0, f; (f = 1 << h) <= d; ++h) {
            for (int i = f, r = f << 1; i < e; i += r) {
                b[h][i - 1] = a[i - 1];
                for (int j = i - 2; j >= i - f; --j) b[h][j] = comp(b[h][j + 1], a[j]);
                b[h][i] = a[i];
                for (int j = i + 1; j < i + f; ++j) b[h][j] = comp(b[h][j - 1], a[j]);
            }
        }
    }

    V get(int L,int R){assert(0<=L&&L<=R);if(L==R)return def;R--;if(L==R)return a[L];int h=__lg(L^R);
        return comp(b[h][L],b[h][R]);}};
int defmin = inf;
template<class V, int NV> struct SegTreeMin { //[l,r)
    V comp(V& l, V& r) { return min(l, r); };

    vector<V> val; SegTreeMin() { val = vector<V>(NV * 2, defmin); }
    V get(int x, int y, int l = 0, int r = NV, int k = 1) {
        if (r <= x || y <= l)return defmin; if (x <= l && r <= y)return val[k];
        auto a = get(x, y, l, (l + r) / 2, k * 2); 
        auto b = get(x, y, (l + r) / 2, r, k * 2 + 1);
        return comp(a, b);
    }
    void update(int i, V v) {
        i += NV; val[i] = v;
        while (i>1) i >>= 1, val[i] = comp(val[i * 2], val[i * 2 + 1]);
    }
    void add(int i, V v) { update(i, val[i + NV] + v); }
    V operator[](int x) { return get(x, x + 1); }
};
#define def 0
template<class V, int NV> struct SegTreeMax { //[l,r)
    V comp(V& l, V& r) { return max(l, r); };

    vector<V> val; SegTreeMax() { val = vector<V>(NV * 2, def); }
    V get(int x, int y, int l = 0, int r = NV, int k = 1) {
        if (r <= x || y <= l)return def; if (x <= l && r <= y)return val[k];
        auto a = get(x, y, l, (l + r) / 2, k * 2);
        auto b = get(x, y, (l + r) / 2, r, k * 2 + 1);
        return comp(a, b);
    }
    void update(int i, V v) {
        i += NV; val[i] = v;
        while (i > 1) i >>= 1, val[i] = comp(val[i * 2], val[i * 2 + 1]);
    }
    void add(int i, V v) { update(i, val[i + NV] + v); }
    V operator[](int x) { return get(x, x + 1); }
};
/*---------------------------------------------------------------------------------------------------
            ∧_∧
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     @hamayanhamayan0
    /   \     | |
    /   / ̄ ̄ ̄ ̄/  |
  __(__ニつ/     _/ .| .|____
     \/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/














int N, A[101010];
SparseTable<pair<int, int>> st;
//---------------------------------------------------------------------------------------------------
int pre[20][101010];
int post[20][101010];
SegTreeMin<int, 1 << 17> stmin;
SegTreeMax<int, 1 << 17> stmax;
void init() {
    rep(p, 0, 20) rep(idx, 0, 101010) pre[p][idx] = 0;
    rep(i, 0, N) {
        pre[0][i + 1] = stmax.get(0, A[i]);
        stmax.update(A[i], i + 1);
    }
    rep(p, 1, 20) rep(idx, 0, N + 1) pre[p][idx] = pre[p - 1][pre[p - 1][idx]];

    defmin = N + 1;
    rep(p, 0, 20) rep(idx, 0, 101010) post[p][idx] = N + 1;
    rrep(i, N - 1, 0) {
        post[0][i + 1] = stmin.get(0, A[i]);
        stmin.update(A[i], i + 1);
    }
    rep(p, 1, 20) rep(idx, 0, N + 1) post[p][idx] = post[p - 1][post[p - 1][idx]];
}
int getpre(int idx, int cnt) {
    int res = idx;
    rep(p, 0, 20) if (cnt & (1 << p)) res = pre[p][res];
    return res;
}
int getpost(int idx, int cnt) {
    int res = idx;
    rep(p, 0, 20) if (cnt & (1 << p)) res = post[p][res];
    return res;
}
//---------------------------------------------------------------------------------------------------
ll ans = 0;
void f(int L, int R) {
    if (L == R) return;
    if (L + 1 == R) return;
    auto ma = st.get(L, R);

    int C = ma.second;

    {
        int ok = 0, ng = N + 1;
        while (ok + 1 != ng) {
            int md = (ok + ng) / 2;
            if (L <= getpre(C, md)) ok = md;
            else ng = md;
        }
        ans += ok;

        //printf("[%d %d] left %d\n", L, R, ok);
    }

    {
        int ok = 0, ng = N + 1;
        while (ok + 1 != ng) {
            int md = (ok + ng) / 2;
            if (getpost(C, md) < R) ok = md;
            else ng = md;
        }
        ans += ok;

        //printf("[%d %d] right %d\n", L, R, ok);
    }

    f(L, C);
    f(C + 1, R);
}
//---------------------------------------------------------------------------------------------------
void _main() {
	cin >> N;
	rep(i, 0, N) cin >> A[i];

    init();

    vector<pair<int, int>> dic;
    dic.push_back({ -1, -1 });
    rep(i, 0, N) dic.push_back({ A[i], i + 1 });
    st.init(dic);

	f(1, N + 1);
	cout << ans << endl;
}





0