結果

問題 No.1868 Teleporting Cyanmond
ユーザー erbowl
提出日時 2022-08-10 20:01:43
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 2,285 bytes
コンパイル時間 2,109 ms
コンパイル使用メモリ 199,332 KB
最終ジャッジ日時 2025-01-30 20:03:55
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

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