結果

問題 No.1868 Teleporting Cyanmond
ユーザー kaichou243kaichou243
提出日時 2022-01-07 16:26:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 120 ms / 2,000 ms
コード長 1,703 bytes
コンパイル時間 2,589 ms
コンパイル使用メモリ 221,528 KB
実行使用メモリ 19,456 KB
最終ジャッジ日時 2024-04-26 13:22:52
合計ジャッジ時間 5,626 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 120 ms
17,408 KB
testcase_04 AC 83 ms
13,312 KB
testcase_05 AC 5 ms
5,376 KB
testcase_06 AC 17 ms
5,688 KB
testcase_07 AC 53 ms
10,112 KB
testcase_08 AC 33 ms
7,296 KB
testcase_09 AC 51 ms
9,472 KB
testcase_10 AC 110 ms
16,256 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 24 ms
6,656 KB
testcase_13 AC 44 ms
10,496 KB
testcase_14 AC 95 ms
18,048 KB
testcase_15 AC 62 ms
12,800 KB
testcase_16 AC 10 ms
5,376 KB
testcase_17 AC 19 ms
6,400 KB
testcase_18 AC 99 ms
19,328 KB
testcase_19 AC 98 ms
19,328 KB
testcase_20 AC 96 ms
19,328 KB
testcase_21 AC 101 ms
19,328 KB
testcase_22 AC 100 ms
19,456 KB
testcase_23 AC 95 ms
19,328 KB
testcase_24 AC 94 ms
19,328 KB
testcase_25 AC 98 ms
19,456 KB
testcase_26 AC 94 ms
19,456 KB
testcase_27 AC 93 ms
19,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)(c).size())
#define ten(x) ((int)1e##x)
#define all(v) (v).begin(), (v).end()
using namespace std;
using ll=long long;
using FLOW=int;
using P = pair<ll,ll>;
const long double PI=acos(-1);
const ll INF=1e18;
const int inf=1e9;
template<typename T> struct SegmentTree{
  using F=function<T(T,T)>;
  F fT;
  const T et;
  int n;
  vector<T> dat;
  SegmentTree(int N,F fT_,T et_) : fT(fT_),et(et_){
    n=1;
    while(n<N)n*=2;
    dat.assign(2*n-1,et_);
  }
  void add(int k,T x){
    k+=n-1;
    dat[k]+=x;
    while(k){
      k=(k-1)/2;
      dat[k]=fT(dat[k*2+1],dat[k*2+2]);
    }
  }
  void apply(int k,T x){
    k+=n-1;
    dat[k]=fT(dat[k],x);
    while(k){
      k=(k-1)/2;
      dat[k]=fT(dat[k*2+1],dat[k*2+2]);
    }
  }
  void update(int k,T x){
    k+=n-1;
    dat[k]=x;
    while(k){
      k=(k-1)/2;
      dat[k]=fT(dat[k*2+1],dat[k*2+2]);
    }
  }
  T query(int l,int r){
    return query_sub(l,r,0,0,n);
  }
  T query_sub(int l,int r,int k=0,int a=0,int b=-1){
    if(b<0)b=n;
    if(r<=a || b<=l)return et;
    if(l<=a && b<=r)return dat[k];
    return fT(query_sub(l,r,k*2+1,a,(a+b)/2),query_sub(l,r,k*2+2,(a+b)/2,b));
  }
};
auto f = [](ll x1,ll x2) -> ll { return min(x1,x2); };
const ll e=INF;
int main(){
  int n;
  cin>>n;
  map<int,set<int>> mp;
  for(int i=0;i<n-1;i++){
    int r;
    cin>>r;
    mp[r].insert(i+1);
  }
  SegmentTree<ll> dp(n+1,f,e);
  dp.update(1,0);
  for(int i=1;i<=n;i++){
    for(auto l : mp[i]){
      dp.apply(i,dp.query(l,i)+1);
    }
  }
  cout<<dp.query(n,n+1)<<endl;
}
0