結果

問題 No.992 最長増加部分列の数え上げ
ユーザー beetbeet
提出日時 2020-02-14 22:00:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 340 ms / 2,000 ms
コード長 5,033 bytes
コンパイル時間 2,847 ms
コンパイル使用メモリ 216,976 KB
実行使用メモリ 26,204 KB
最終ジャッジ日時 2024-04-16 02:01:18
合計ジャッジ時間 12,807 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 119 ms
10,744 KB
testcase_05 AC 82 ms
7,840 KB
testcase_06 AC 144 ms
11,428 KB
testcase_07 AC 104 ms
10,540 KB
testcase_08 AC 52 ms
7,100 KB
testcase_09 AC 106 ms
10,480 KB
testcase_10 AC 146 ms
11,412 KB
testcase_11 AC 187 ms
12,496 KB
testcase_12 AC 35 ms
5,376 KB
testcase_13 AC 100 ms
10,228 KB
testcase_14 AC 100 ms
10,236 KB
testcase_15 AC 35 ms
5,376 KB
testcase_16 AC 303 ms
19,448 KB
testcase_17 AC 52 ms
6,968 KB
testcase_18 AC 100 ms
10,208 KB
testcase_19 AC 182 ms
12,464 KB
testcase_20 AC 339 ms
20,256 KB
testcase_21 AC 339 ms
20,260 KB
testcase_22 AC 338 ms
20,256 KB
testcase_23 AC 333 ms
20,260 KB
testcase_24 AC 337 ms
20,256 KB
testcase_25 AC 335 ms
20,256 KB
testcase_26 AC 340 ms
20,260 KB
testcase_27 AC 336 ms
20,388 KB
testcase_28 AC 337 ms
20,264 KB
testcase_29 AC 336 ms
20,260 KB
testcase_30 AC 168 ms
25,888 KB
testcase_31 AC 167 ms
25,888 KB
testcase_32 AC 183 ms
25,768 KB
testcase_33 AC 168 ms
25,892 KB
testcase_34 AC 176 ms
26,020 KB
testcase_35 AC 173 ms
25,380 KB
testcase_36 AC 162 ms
25,384 KB
testcase_37 AC 172 ms
25,380 KB
testcase_38 AC 185 ms
25,380 KB
testcase_39 AC 165 ms
25,508 KB
testcase_40 AC 201 ms
26,080 KB
testcase_41 AC 184 ms
25,980 KB
testcase_42 AC 194 ms
26,068 KB
testcase_43 AC 189 ms
25,948 KB
testcase_44 AC 187 ms
26,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


template<typename T,T MOD = 1000000007>
struct Mint{
  static constexpr T mod = MOD;
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}

  Mint pow(long long k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1) res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }

  static Mint add_identity(){return Mint(0);}
  static Mint mul_identity(){return Mint(1);}

  Mint inv(){return pow(MOD-2);}

  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}

  Mint operator+(Mint a) const{return Mint(v)+=a;}
  Mint operator-(Mint a) const{return Mint(v)-=a;}
  Mint operator*(Mint a) const{return Mint(v)*=a;}
  Mint operator/(Mint a) const{return Mint(v)/=a;}

  Mint operator-() const{return v?Mint(MOD-v):Mint(v);}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}
  bool operator <(const Mint a)const{return v <a.v;}

  static Mint comb(long long n,Int k){
    Mint num(1),dom(1);
    for(Int i=0;i<k;i++){
      num*=Mint(n-i);
      dom*=Mint(i+1);
    }
    return num/dom;
  }
};
template<typename T,T MOD> constexpr T Mint<T, MOD>::mod;
template<typename T,T MOD>
ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;}


template <typename T,typename E>
struct SegmentTree{
  using F = function<T(T,T)>;
  using G = function<T(T,E)>;
  using H = function<E(E,E)>;
  Int n,height;
  F f;
  G g;
  H h;
  T ti;
  E ei;
  vector<T> dat;
  vector<E> laz;
  SegmentTree(F f,G g,H h,T ti,E ei):
    f(f),g(g),h(h),ti(ti),ei(ei){}

  void init(Int n_){
    n=1;height=0;
    while(n<n_) n<<=1,height++;
    dat.assign(2*n,ti);
    laz.assign(2*n,ei);
  }

  void build(const vector<T> &v){
    Int n_=v.size();
    init(n_);
    for(Int i=0;i<n_;i++) dat[n+i]=v[i];
    for(Int i=n-1;i;i--)
      dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
  }

  inline T reflect(Int k){
    return laz[k]==ei?dat[k]:g(dat[k],laz[k]);
  }

  inline void propagate(Int k){
    if(laz[k]==ei) return;
    laz[(k<<1)|0]=h(laz[(k<<1)|0],laz[k]);
    laz[(k<<1)|1]=h(laz[(k<<1)|1],laz[k]);
    dat[k]=reflect(k);
    laz[k]=ei;
  }

  inline void thrust(Int k){
    for(Int i=height;i;i--) propagate(k>>i);
  }

  inline void recalc(Int k){
    while(k>>=1)
      dat[k]=f(reflect((k<<1)|0),reflect((k<<1)|1));
  }

  void update(Int a,Int b,E x){
    if(a>=b) return;
    thrust(a+=n);
    thrust(b+=n-1);
    for(Int l=a,r=b+1;l<r;l>>=1,r>>=1){
      if(l&1) laz[l]=h(laz[l],x),l++;
      if(r&1) --r,laz[r]=h(laz[r],x);
    }
    recalc(a);
    recalc(b);
  }

  void set_val(Int a,T x){
    thrust(a+=n);
    dat[a]=x;laz[a]=ei;
    recalc(a);
  }

  T query(Int a,Int b){
    if(a>=b) return ti;
    thrust(a+=n);
    thrust(b+=n-1);
    T vl=ti,vr=ti;
    for(Int l=a,r=b+1;l<r;l>>=1,r>>=1) {
      if(l&1) vl=f(vl,reflect(l++));
      if(r&1) vr=f(reflect(--r),vr);
    }
    return f(vl,vr);
  }

  template<typename C>
  Int find(Int st,C &check,T &acc,Int k,Int l,Int r){
    if(l+1==r){
      acc=f(acc,reflect(k));
      return check(acc)?k-n:-1;
    }
    propagate(k);
    Int m=(l+r)>>1;
    if(m<=st) return find(st,check,acc,(k<<1)|1,m,r);
    if(st<=l&&!check(f(acc,dat[k]))){
      acc=f(acc,dat[k]);
      return -1;
    }
    Int vl=find(st,check,acc,(k<<1)|0,l,m);
    if(~vl) return vl;
    return find(st,check,acc,(k<<1)|1,m,r);
  }

  template<typename C>
  Int find(Int st,C &check){
    T acc=ti;
    return find(st,check,acc,1,0,n);
  }
};

//INSERT ABOVE HERE
signed main(){
  Int n;
  cin>>n;
  vector<Int> as(n);
  for(Int i=0;i<n;i++) cin>>as[i];

  const Int INF = 1e15;
  vector<Int> vs(n,INF);
  vector<Int> ps(n);
  for(Int i=0;i<n;i++){
    ps[i]=lower_bound(vs.begin(),vs.end(),as[i])-vs.begin();
    vs[ps[i]]=as[i];
  }

  using M = Mint<Int>;
  auto f=[&](M a,M b){return a+b;};
  auto g=[&](M  ,M b){return b;};
  M ti(0);
  M ei;
  ei.v=-1;
  SegmentTree<M, M> seg(f,g,g,ti,ei);
  seg.build(vector<M>(n,ti));

  Int m=*max_element(ps.begin(),ps.end());
  vector< vector<Int> > G(m+1);
  for(Int i=0;i<n;i++)
    G[ps[i]].emplace_back(i);

  vector<M> dp(n);
  for(Int i=0;i<n;i++)
    if(ps[i]==0) dp[i]=M(1);

  for(Int t=0;t<m;t++){
    seg.update(0,n,M(0));

    using P = pair<Int, Int>;
    vector<P> vp;
    for(Int i:G[t])
      vp.emplace_back(as[i],n+i);

    for(Int i:G[t+1])
      vp.emplace_back(as[i],i);

    sort(vp.begin(),vp.end());
    for(auto p:vp){
      Int k=p.second%n;
      if(ps[k]==t) seg.set_val(k,dp[k]);
      else dp[k]=seg.query(0,k);
    }
  }

  M ans{0};
  for(Int i=0;i<n;i++)
    if(ps[i]==m) ans+=dp[i];
  cout<<ans<<endl;
  return 0;
}
0