結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー はまやんはまやんはまやんはまやん
提出日時 2020-04-18 12:50:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 185 ms / 3,500 ms
コード長 6,030 bytes
コンパイル時間 2,512 ms
コンパイル使用メモリ 212,916 KB
実行使用メモリ 50,564 KB
最終ジャッジ日時 2024-10-03 23:32:18
合計ジャッジ時間 11,026 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
21,132 KB
testcase_01 AC 8 ms
21,008 KB
testcase_02 AC 8 ms
21,136 KB
testcase_03 AC 13 ms
21,792 KB
testcase_04 AC 13 ms
22,224 KB
testcase_05 AC 12 ms
21,916 KB
testcase_06 AC 13 ms
21,916 KB
testcase_07 AC 12 ms
21,916 KB
testcase_08 AC 12 ms
21,792 KB
testcase_09 AC 13 ms
21,924 KB
testcase_10 AC 12 ms
21,924 KB
testcase_11 AC 130 ms
46,292 KB
testcase_12 AC 134 ms
46,288 KB
testcase_13 AC 159 ms
46,632 KB
testcase_14 AC 134 ms
46,436 KB
testcase_15 AC 153 ms
46,660 KB
testcase_16 AC 136 ms
46,416 KB
testcase_17 AC 145 ms
46,596 KB
testcase_18 AC 138 ms
46,456 KB
testcase_19 AC 143 ms
46,728 KB
testcase_20 AC 148 ms
46,616 KB
testcase_21 AC 136 ms
46,584 KB
testcase_22 AC 126 ms
46,288 KB
testcase_23 AC 129 ms
46,428 KB
testcase_24 AC 145 ms
46,732 KB
testcase_25 AC 148 ms
46,616 KB
testcase_26 AC 139 ms
46,464 KB
testcase_27 AC 131 ms
46,440 KB
testcase_28 AC 150 ms
46,716 KB
testcase_29 AC 153 ms
46,620 KB
testcase_30 AC 150 ms
46,620 KB
testcase_31 AC 149 ms
46,620 KB
testcase_32 AC 150 ms
46,616 KB
testcase_33 AC 148 ms
46,624 KB
testcase_34 AC 172 ms
50,564 KB
testcase_35 AC 169 ms
47,236 KB
testcase_36 AC 167 ms
47,876 KB
testcase_37 AC 168 ms
48,640 KB
testcase_38 AC 163 ms
46,744 KB
testcase_39 AC 167 ms
46,932 KB
testcase_40 AC 164 ms
46,736 KB
testcase_41 AC 164 ms
47,092 KB
testcase_42 AC 166 ms
46,736 KB
testcase_43 AC 165 ms
46,620 KB
testcase_44 AC 166 ms
46,608 KB
testcase_45 AC 161 ms
46,700 KB
testcase_46 AC 169 ms
48,872 KB
testcase_47 AC 164 ms
46,940 KB
testcase_48 AC 167 ms
47,236 KB
testcase_49 AC 167 ms
47,232 KB
testcase_50 AC 167 ms
47,732 KB
testcase_51 AC 185 ms
48,388 KB
testcase_52 AC 184 ms
48,216 KB
testcase_53 AC 182 ms
48,260 KB
testcase_54 AC 9 ms
21,132 KB
testcase_55 AC 9 ms
21,412 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