結果

問題 No.1868 Teleporting Cyanmond
ユーザー erbowlerbowl
提出日時 2022-08-10 20:01:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 2,285 bytes
コンパイル時間 2,226 ms
コンパイル使用メモリ 207,168 KB
実行使用メモリ 6,252 KB
最終ジャッジ日時 2023-10-21 05:18:13
合計ジャッジ時間 4,595 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 43 ms
5,988 KB
testcase_04 AC 31 ms
4,932 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 8 ms
4,348 KB
testcase_07 AC 22 ms
4,668 KB
testcase_08 AC 14 ms
4,348 KB
testcase_09 AC 19 ms
4,668 KB
testcase_10 AC 40 ms
5,988 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 11 ms
4,348 KB
testcase_13 AC 20 ms
4,668 KB
testcase_14 AC 36 ms
6,252 KB
testcase_15 AC 25 ms
4,932 KB
testcase_16 AC 5 ms
4,348 KB
testcase_17 AC 9 ms
4,348 KB
testcase_18 AC 36 ms
6,252 KB
testcase_19 AC 38 ms
6,252 KB
testcase_20 AC 39 ms
6,252 KB
testcase_21 AC 38 ms
6,252 KB
testcase_22 AC 38 ms
6,252 KB
testcase_23 AC 36 ms
6,252 KB
testcase_24 AC 38 ms
6,252 KB
testcase_25 AC 34 ms
6,252 KB
testcase_26 AC 35 ms
6,252 KB
testcase_27 AC 36 ms
6,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

typedef long long ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;
#define int long long


template<class Monoid> struct SegTree {
    using Func = function<Monoid(Monoid, Monoid)>;
    Func F;
    Monoid IDENTITY;
    int SIZE_R;
    vector<Monoid> dat;

    SegTree() {}
    SegTree(int n, const Func f, const Monoid &identity): F(f), IDENTITY(identity) {
        SIZE_R = 1;
        while (SIZE_R < n) SIZE_R *= 2;
        dat.assign(SIZE_R * 2, IDENTITY);
    }
    void init(int n, const Func f, const Monoid &identity) {
        IDENTITY = identity;
        F = f;
        SIZE_R = 1;
        while (SIZE_R < n) SIZE_R *= 2;
        dat.assign(SIZE_R * 2, IDENTITY);
    }
    
    /* set, a is 0-indexed */
    void set(int a, const Monoid &v) { dat[a + SIZE_R] = v; }
    void build() {
        for (int k = SIZE_R - 1; k > 0; --k)
            dat[k] = F(dat[k*2], dat[k*2+1]);
    }
    
    /* update a, a is 0-indexed */
    void update(int a, const Monoid &v) {
        int k = a + SIZE_R;
        dat[k] = v;
        while (k >>= 1) dat[k] = F(dat[k*2], dat[k*2+1]);
    }
    
    /* get [a, b), a and b are 0-indexed */
    Monoid get(int a, int b) {
        Monoid vleft = IDENTITY, vright = IDENTITY;
        for (int left = a + SIZE_R, right = b + SIZE_R; left < right; left >>= 1, right >>= 1) {
            if (left & 1) vleft = F(vleft, dat[left++]);
            if (right & 1) vright = F(dat[--right], vright);
        }                                                                                                              
        return F(vleft, vright);
    }
    inline Monoid operator [] (int a) { return dat[a + SIZE_R]; }
    
    /* debug */
    void print() {
        for (int i = 0; i < SIZE_R; ++i) {
            cout << (*this)[i];
            if (i != SIZE_R-1) cout << ",";
        }
        cout << endl;
    }
};


SegTree<ll> seg;
signed main(){
    ll n;
    std::cin >> n;
    vector<ll> r(n-1);
    for (int i = 0; i < n-1; i++) {
        std::cin >> r[i];
        r[i]--;
    }
    seg.init(n,[&](ll a, ll b){return min(a,b);},1e18);
    seg.update(n-1,0);
    for (int i = n-2; i >= 0; i--) {
        auto x = seg.get(i+1,r[i]+1);
        seg.update(i,x+1);
    }
    std::cout << seg.get(0,1) << std::endl;
}
0