結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2020-04-18 12:50:02
言語 C++17(1z)
(gcc 10.2.0 + boost 1.73.0)
結果
AC  
実行時間 179 ms / 3,500 ms
コード長 6,030 Byte
コンパイル時間 2,284 ms
使用メモリ 50,360 KB
最終ジャッジ日時 2021-03-04 01:13:51
合計ジャッジ時間 11,093 ms
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 10 ms
20,556 KB
testcase_01 AC 10 ms
20,552 KB
testcase_02 AC 9 ms
20,580 KB
testcase_03 AC 13 ms
21,316 KB
testcase_04 AC 13 ms
21,376 KB
testcase_05 AC 14 ms
21,460 KB
testcase_06 AC 13 ms
21,436 KB
testcase_07 AC 13 ms
21,320 KB
testcase_08 AC 14 ms
21,428 KB
testcase_09 AC 14 ms
21,228 KB
testcase_10 AC 13 ms
21,328 KB
testcase_11 AC 127 ms
46,008 KB
testcase_12 AC 126 ms
45,964 KB
testcase_13 AC 147 ms
46,556 KB
testcase_14 AC 130 ms
45,968 KB
testcase_15 AC 138 ms
46,168 KB
testcase_16 AC 126 ms
45,968 KB
testcase_17 AC 139 ms
46,284 KB
testcase_18 AC 136 ms
45,984 KB
testcase_19 AC 141 ms
46,368 KB
testcase_20 AC 147 ms
46,568 KB
testcase_21 AC 134 ms
46,248 KB
testcase_22 AC 126 ms
46,004 KB
testcase_23 AC 127 ms
45,956 KB
testcase_24 AC 141 ms
46,180 KB
testcase_25 AC 144 ms
46,340 KB
testcase_26 AC 137 ms
46,352 KB
testcase_27 AC 130 ms
46,192 KB
testcase_28 AC 145 ms
46,296 KB
testcase_29 AC 146 ms
46,192 KB
testcase_30 AC 148 ms
46,456 KB
testcase_31 AC 145 ms
46,400 KB
testcase_32 AC 145 ms
46,192 KB
testcase_33 AC 146 ms
46,308 KB
testcase_34 AC 166 ms
50,360 KB
testcase_35 AC 163 ms
46,724 KB
testcase_36 AC 162 ms
47,608 KB
testcase_37 AC 164 ms
48,336 KB
testcase_38 AC 159 ms
46,412 KB
testcase_39 AC 159 ms
46,488 KB
testcase_40 AC 159 ms
46,280 KB
testcase_41 AC 157 ms
46,700 KB
testcase_42 AC 162 ms
46,336 KB
testcase_43 AC 159 ms
46,344 KB
testcase_44 AC 162 ms
46,552 KB
testcase_45 AC 156 ms
46,404 KB
testcase_46 AC 161 ms
48,432 KB
testcase_47 AC 157 ms
46,496 KB
testcase_48 AC 161 ms
46,488 KB
testcase_49 AC 162 ms
46,700 KB
testcase_50 AC 160 ms
47,116 KB
testcase_51 AC 178 ms
47,816 KB
testcase_52 AC 179 ms
47,780 KB
testcase_53 AC 179 ms
47,756 KB
testcase_54 AC 10 ms
20,580 KB
testcase_55 AC 10 ms
20,588 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