結果

問題 No.992 最長増加部分列の数え上げ
ユーザー LayCurseLayCurse
提出日時 2020-02-14 22:04:29
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 10,274 bytes
コンパイル時間 2,773 ms
コンパイル使用メモリ 213,112 KB
実行使用メモリ 13,040 KB
最終ジャッジ日時 2024-04-16 02:05:02
合計ジャッジ時間 5,614 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
8,448 KB
testcase_01 AC 6 ms
8,192 KB
testcase_02 AC 6 ms
8,320 KB
testcase_03 AC 5 ms
8,192 KB
testcase_04 AC 19 ms
9,728 KB
testcase_05 AC 16 ms
9,216 KB
testcase_06 AC 23 ms
9,856 KB
testcase_07 AC 18 ms
9,344 KB
testcase_08 AC 12 ms
8,832 KB
testcase_09 AC 18 ms
9,472 KB
testcase_10 AC 22 ms
9,728 KB
testcase_11 AC 27 ms
10,112 KB
testcase_12 AC 10 ms
8,576 KB
testcase_13 AC 17 ms
9,216 KB
testcase_14 AC 17 ms
9,344 KB
testcase_15 AC 10 ms
8,704 KB
testcase_16 AC 38 ms
11,136 KB
testcase_17 AC 12 ms
8,960 KB
testcase_18 AC 17 ms
9,344 KB
testcase_19 AC 27 ms
10,112 KB
testcase_20 AC 43 ms
11,392 KB
testcase_21 AC 43 ms
11,520 KB
testcase_22 AC 42 ms
11,520 KB
testcase_23 AC 42 ms
11,392 KB
testcase_24 AC 43 ms
11,392 KB
testcase_25 AC 43 ms
11,520 KB
testcase_26 AC 43 ms
11,520 KB
testcase_27 AC 42 ms
11,392 KB
testcase_28 AC 42 ms
11,392 KB
testcase_29 AC 43 ms
11,648 KB
testcase_30 AC 32 ms
12,776 KB
testcase_31 AC 32 ms
12,652 KB
testcase_32 AC 32 ms
12,648 KB
testcase_33 AC 32 ms
12,716 KB
testcase_34 AC 32 ms
12,900 KB
testcase_35 AC 26 ms
12,776 KB
testcase_36 AC 26 ms
12,904 KB
testcase_37 AC 26 ms
12,900 KB
testcase_38 AC 27 ms
12,780 KB
testcase_39 AC 26 ms
12,904 KB
testcase_40 AC 36 ms
13,040 KB
testcase_41 AC 35 ms
12,904 KB
testcase_42 AC 35 ms
12,784 KB
testcase_43 AC 36 ms
12,780 KB
testcase_44 AC 36 ms
12,780 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define MD (1000000007U)
void *wmem;
char memarr[96000000];
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
struct Modint{
  unsigned val;
  Modint(){
    val=0;
  }
  Modint(int a){
    val = ord(a);
  }
  Modint(unsigned a){
    val = ord(a);
  }
  Modint(long long a){
    val = ord(a);
  }
  Modint(unsigned long long a){
    val = ord(a);
  }
  inline unsigned ord(unsigned a){
    return a%MD;
  }
  inline unsigned ord(int a){
    a %= (int)MD;
    if(a < 0){
      a += MD;
    }
    return a;
  }
  inline unsigned ord(unsigned long long a){
    return a%MD;
  }
  inline unsigned ord(long long a){
    a %= (int)MD;
    if(a < 0){
      a += MD;
    }
    return a;
  }
  inline unsigned get(){
    return val;
  }
  inline Modint &operator+=(Modint a){
    val += a.val;
    if(val >= MD){
      val -= MD;
    }
    return *this;
  }
  inline Modint &operator-=(Modint a){
    if(val < a.val){
      val = val + MD - a.val;
    }
    else{
      val -= a.val;
    }
    return *this;
  }
  inline Modint &operator*=(Modint a){
    val = ((unsigned long long)val*a.val)%MD;
    return *this;
  }
  inline Modint &operator/=(Modint a){
    return *this *= a.inverse();
  }
  inline Modint operator+(Modint a){
    return Modint(*this)+=a;
  }
  inline Modint operator-(Modint a){
    return Modint(*this)-=a;
  }
  inline Modint operator*(Modint a){
    return Modint(*this)*=a;
  }
  inline Modint operator/(Modint a){
    return Modint(*this)/=a;
  }
  inline Modint operator+(int a){
    return Modint(*this)+=Modint(a);
  }
  inline Modint operator-(int a){
    return Modint(*this)-=Modint(a);
  }
  inline Modint operator*(int a){
    return Modint(*this)*=Modint(a);
  }
  inline Modint operator/(int a){
    return Modint(*this)/=Modint(a);
  }
  inline Modint operator+(long long a){
    return Modint(*this)+=Modint(a);
  }
  inline Modint operator-(long long a){
    return Modint(*this)-=Modint(a);
  }
  inline Modint operator*(long long a){
    return Modint(*this)*=Modint(a);
  }
  inline Modint operator/(long long a){
    return Modint(*this)/=Modint(a);
  }
  inline Modint operator-(void){
    Modint res;
    if(val){
      res.val=MD-val;
    }
    else{
      res.val=0;
    }
    return res;
  }
  inline operator bool(void){
    return val!=0;
  }
  inline operator int(void){
    return get();
  }
  inline operator long long(void){
    return get();
  }
  inline Modint inverse(){
    int a = val;
    int b = MD;
    int u = 1;
    int v = 0;
    int t;
    Modint res;
    while(b){
      t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    if(u < 0){
      u += MD;
    }
    res.val = u;
    return res;
  }
  inline Modint pw(unsigned long long b){
    Modint a(*this);
    Modint res;
    res.val = 1;
    while(b){
      if(b&1){
        res *= a;
      }
      b >>= 1;
      a *= a;
    }
    return res;
  }
  inline bool operator==(int a){
    return ord(a)==val;
  }
  inline bool operator!=(int a){
    return ord(a)!=val;
  }
}
;
inline Modint operator+(int a, Modint b){
  return Modint(a)+=b;
}
inline Modint operator-(int a, Modint b){
  return Modint(a)-=b;
}
inline Modint operator*(int a, Modint b){
  return Modint(a)*=b;
}
inline Modint operator/(int a, Modint b){
  return Modint(a)/=b;
}
inline Modint operator+(long long a, Modint b){
  return Modint(a)+=b;
}
inline Modint operator-(long long a, Modint b){
  return Modint(a)-=b;
}
inline Modint operator*(long long a, Modint b){
  return Modint(a)*=b;
}
inline Modint operator/(long long a, Modint b){
  return Modint(a)/=b;
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void wt_L(char a){
  putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    putchar_unlocked('-');
  }
  while(s--){
    putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(Modint x){
  int i;
  i = (int)x;
  wt_L(i);
}
template<class S> inline void arrInsert(const int k, int &sz, S a[], const S aval){
  int i;
  sz++;
  for(i=sz-1;i>k;i--){
    a[i] = a[i-1];
  }
  a[k] = aval;
}
template<class S, class T> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval){
  int i;
  sz++;
  for(i=sz-1;i>k;i--){
    a[i] = a[i-1];
  }
  for(i=sz-1;i>k;i--){
    b[i] = b[i-1];
  }
  a[k] = aval;
  b[k] = bval;
}
template<class S, class T, class U> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval){
  int i;
  sz++;
  for(i=sz-1;i>k;i--){
    a[i] = a[i-1];
  }
  for(i=sz-1;i>k;i--){
    b[i] = b[i-1];
  }
  for(i=sz-1;i>k;i--){
    c[i] = c[i-1];
  }
  a[k] = aval;
  b[k] = bval;
  c[k] = cval;
}
template<class S, class T, class U, class V> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval, V d[], const V dval){
  int i;
  sz++;
  for(i=sz-1;i>k;i--){
    a[i] = a[i-1];
  }
  for(i=sz-1;i>k;i--){
    b[i] = b[i-1];
  }
  for(i=sz-1;i>k;i--){
    c[i] = c[i-1];
  }
  for(i=sz-1;i>k;i--){
    d[i] = d[i-1];
  }
  a[k] = aval;
  b[k] = bval;
  c[k] = cval;
  d[k] = dval;
}
template<class T> struct fenwick{
  int size;
  int memory;
  T *data;
  void malloc(int mem);
  void walloc(int mem, void **workMemory = &wmem);
  void free(void);
  void init(int N);
  void add(int k, T val);
  T get(int k);
  T range(int a, int b);
  int kth(T k);
}
;
int N;
int A[200000+2];
int l1[200000+2];
int l2[200000+2];
int arr[200000+2];
int lev[200000+2];
fenwick<Modint> f[200000+2];
vector<int> d[200000+2];
int r[200000+2];
int LIS(int n, int a[]){
  int i;
  int k;
  int res;
  arr[0] = a[0];
  lev[0] = 1;
  res = 1;
  int Lj4PdHRW = n;
  for(i=(1);i<(Lj4PdHRW);i++){
    k = lower_bound(arr, arr+res, a[i]) - arr;
    arr[k] = a[i];
    lev[i] = k + 1;
    if(res==k){
      res++;
    }
  }
  return res;
}
int main(){
  wmem = memarr;
  int i;
  int j;
  int k;
  int len;
  Modint tmp;
  rd(N);
  {
    int e98WHCEY;
    for(e98WHCEY=(0);e98WHCEY<(N);e98WHCEY++){
      rd((A+1)[e98WHCEY]);
    }
  }
  N += 2;
  A[0] = -1073709056;
  A[N-1] = 1073709056;
  len = LIS(N, A);
  for(i=(0);i<(N);i++){
    l1[i] = lev[i];
  }
  for(i=(0);i<(N);i++){
    A[i] = -A[i];
  }
  reverse(A, A+N);
  len = LIS(N, A);
  for(i=(0);i<(N);i++){
    l2[i] = lev[N-1-i];
  }
  for(i=(0);i<(N);i++){
    A[i] = -A[i];
  }
  reverse(A, A+N);
  k = 0;
  for(i=(0);i<(N);i++){
    if(l1[i] + l2[i] == len + 1){
      arrInsert(k,k,lev,l1[i]-1,A,A[i]);
    }
  }
  N = k;
  for(i=(0);i<(N);i++){
    d[lev[i]].push_back(A[i]);
  }
  for(i=(0);i<(len);i++){
    r[i] = d[i].size();
    f[i].walloc(r[i]);
    f[i].init(r[i]);
  }
  f[len-1].add(0, 1);
  for(i=(N-1)-1;i>=(0);i--){
    k = lev[i];
    r[k]--;
    int AlM5nNnR;
    int XJIcIBrW;
    int jPV_0s1p;
    AlM5nNnR = -1;
    XJIcIBrW = d[k+1].size()-1;
    while(AlM5nNnR < XJIcIBrW){
      if((AlM5nNnR + XJIcIBrW)%2==0){
        jPV_0s1p = (AlM5nNnR + XJIcIBrW) / 2;
      }
      else{
        jPV_0s1p = (AlM5nNnR + XJIcIBrW + 1) / 2;
      }
      if(d[k+1][jPV_0s1p] > A[i]){
        AlM5nNnR = jPV_0s1p;
      }
      else{
        XJIcIBrW = jPV_0s1p - 1;
      }
    }
    j =XJIcIBrW;
    tmp = f[k+1].get(j);
    f[k].add(r[k],tmp);
  }
  wt_L(tmp);
  wt_L('\n');
  return 0;
}
template<class T> void fenwick<T>::malloc(int mem){
  memory = mem;
  data = (T*)std::malloc(sizeof(T)*mem);
}
template<class T> void fenwick<T>::walloc(int mem, void **workMemory /* = &wmem*/){
  memory = mem;
  walloc1d(&data, mem, workMemory);
}
template<class T> void fenwick<T>::free(void){
  memory = 0;
  free(data);
}
template<class T> void fenwick<T>::init(int N){
  size = N;
  memset(data,0,sizeof(T)*N);
}
template<class T> void fenwick<T>::add(int k, T val){
  while(k < size){
    data[k] += val;
    k |= k+1;
  }
}
template<class T> T fenwick<T>::get(int k){
  T res = 0;
  while(k>=0){
    res += data[k];
    k = (k&(k+1))-1;
  }
  return res;
}
template<class T> T fenwick<T>::range(int a, int b){
  if(b==-1){
    b=size-1;
  }
  return get(b) - get(a-1);
}
template<class T> int fenwick<T>::kth(T k){
  int i=0;
  int j=size;
  int c;
  T v;
  while(i<j){
    c = (i+j)/2;
    v = get(c);
    if(v <= k){
      i=c+1;
    }
    else{
      j=c;
    }
  }
  return i==size?-1:i;
}
// cLay varsion 20200214-1

// --- original code ---
// int N, A[2d5+2];
// int l1[2d5+2], l2[2d5+2];
// 
// int arr[2d5+2], lev[2d5+2];
// fenwick<Modint> f[2d5+2];
// vector<int> d[2d5+2];
// int r[2d5+2];
// 
// int LIS(int n, int a[]){
//   int i, k, res;
// 
//   arr[0] = a[0];
//   lev[0] = 1;
//   res = 1;
//   REP(i,1,n){
//     k = lower_bound(arr, arr+res, a[i]) - arr;
//     arr[k] = a[i];
//     lev[i] = k + 1;
//     if(res==k) res++;
//   }
// 
//   return res;
// }
// 
// {
//   int i, j, k, len;
//   Modint tmp;
// 
//   rd(N, ((A+1))(N));
//   N += 2;
//   A[0] = -int_inf;
//   A[N-1] = int_inf;
// 
//   len = LIS(N, A);
//   rep(i,N) l1[i] = lev[i];
//   rep(i,N) A[i] = -A[i];
//   reverse(A, A+N);
//   len = LIS(N, A);
//   rep(i,N) l2[i] = lev[N-1-i];
//   rep(i,N) A[i] = -A[i];
//   reverse(A, A+N);
// 
//   k = 0;
//   rep(i,N) if(l1[i] + l2[i] == len + 1){
//     arrInsert(k,k,lev,l1[i]-1,A,A[i]);
//   }
//   N = k;
// 
//   rep(i,N) d[lev[i]].push_back(A[i]);
//   rep(i,len){
//     r[i] = d[i].size();
//     f[i].walloc(r[i]);
//     f[i].init(r[i]);
//   }
//   f[len-1].add(0, 1);
// 
//   rrep(i,N-1){
//     k = lev[i];
//     r[k]--;
//     j = bsearch_max[int,x,-1,d[k+1].size()-1](d[k+1][x] > A[i]);
//     tmp = f[k+1].get(j);
//     f[k].add(r[k],tmp);
//   }
//   wt(tmp);
// }
0