結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー はまやんはまやんはまやんはまやん
提出日時 2020-04-18 12:50:02
言語 C++17
(gcc 11.2.0 + boost 1.78.0)
結果
AC  
実行時間 179 ms / 3,500 ms
コード長 6,030 Byte
コンパイル時間 2,232 ms
使用メモリ 50,520 KB
最終ジャッジ日時 2022-06-15 12:33:37
合計ジャッジ時間 11,213 ms
ジャッジサーバーID
(参考情報)
judge11 / judge10
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 15 ms
21,032 KB
testcase_01 AC 15 ms
21,036 KB
testcase_02 AC 15 ms
20,916 KB
testcase_03 AC 19 ms
21,840 KB
testcase_04 AC 18 ms
21,796 KB
testcase_05 AC 19 ms
21,716 KB
testcase_06 AC 18 ms
21,804 KB
testcase_07 AC 19 ms
21,792 KB
testcase_08 AC 19 ms
21,836 KB
testcase_09 AC 18 ms
21,716 KB
testcase_10 AC 18 ms
21,904 KB
testcase_11 AC 128 ms
46,400 KB
testcase_12 AC 125 ms
46,252 KB
testcase_13 AC 146 ms
46,636 KB
testcase_14 AC 130 ms
46,432 KB
testcase_15 AC 137 ms
46,656 KB
testcase_16 AC 127 ms
46,180 KB
testcase_17 AC 141 ms
46,564 KB
testcase_18 AC 136 ms
46,432 KB
testcase_19 AC 140 ms
46,508 KB
testcase_20 AC 149 ms
46,748 KB
testcase_21 AC 136 ms
46,508 KB
testcase_22 AC 125 ms
46,408 KB
testcase_23 AC 129 ms
46,312 KB
testcase_24 AC 142 ms
46,580 KB
testcase_25 AC 149 ms
46,652 KB
testcase_26 AC 140 ms
46,488 KB
testcase_27 AC 134 ms
46,220 KB
testcase_28 AC 150 ms
46,524 KB
testcase_29 AC 149 ms
46,516 KB
testcase_30 AC 148 ms
46,668 KB
testcase_31 AC 148 ms
46,572 KB
testcase_32 AC 152 ms
46,620 KB
testcase_33 AC 146 ms
46,528 KB
testcase_34 AC 168 ms
50,520 KB
testcase_35 AC 160 ms
47,032 KB
testcase_36 AC 160 ms
47,860 KB
testcase_37 AC 163 ms
48,636 KB
testcase_38 AC 157 ms
46,644 KB
testcase_39 AC 156 ms
46,692 KB
testcase_40 AC 160 ms
46,640 KB
testcase_41 AC 159 ms
46,724 KB
testcase_42 AC 159 ms
46,640 KB
testcase_43 AC 158 ms
46,640 KB
testcase_44 AC 159 ms
46,592 KB
testcase_45 AC 156 ms
46,504 KB
testcase_46 AC 162 ms
48,912 KB
testcase_47 AC 158 ms
46,644 KB
testcase_48 AC 161 ms
46,784 KB
testcase_49 AC 161 ms
46,940 KB
testcase_50 AC 163 ms
47,496 KB
testcase_51 AC 179 ms
48,148 KB
testcase_52 AC 179 ms
48,000 KB
testcase_53 AC 179 ms
48,084 KB
testcase_54 AC 14 ms
20,920 KB
testcase_55 AC 15 ms
21,020 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;
    }

    {
        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;
    }

    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