結果

問題 No.992 最長増加部分列の数え上げ
ユーザー MayimgMayimg
提出日時 2020-02-15 14:42:51
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 334 ms / 2,000 ms
コード長 12,435 bytes
コンパイル時間 2,624 ms
コンパイル使用メモリ 214,292 KB
実行使用メモリ 22,524 KB
最終ジャッジ日時 2024-04-16 03:10:58
合計ジャッジ時間 12,138 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 111 ms
11,676 KB
testcase_05 AC 67 ms
8,448 KB
testcase_06 AC 126 ms
12,512 KB
testcase_07 AC 86 ms
11,132 KB
testcase_08 AC 42 ms
7,424 KB
testcase_09 AC 88 ms
11,268 KB
testcase_10 AC 124 ms
12,512 KB
testcase_11 AC 163 ms
14,016 KB
testcase_12 AC 27 ms
6,940 KB
testcase_13 AC 80 ms
10,992 KB
testcase_14 AC 85 ms
10,992 KB
testcase_15 AC 27 ms
6,940 KB
testcase_16 AC 286 ms
21,552 KB
testcase_17 AC 41 ms
7,168 KB
testcase_18 AC 78 ms
10,988 KB
testcase_19 AC 163 ms
13,748 KB
testcase_20 AC 321 ms
22,392 KB
testcase_21 AC 327 ms
22,392 KB
testcase_22 AC 314 ms
22,396 KB
testcase_23 AC 324 ms
22,520 KB
testcase_24 AC 320 ms
22,392 KB
testcase_25 AC 324 ms
22,520 KB
testcase_26 AC 320 ms
22,396 KB
testcase_27 AC 322 ms
22,392 KB
testcase_28 AC 334 ms
22,524 KB
testcase_29 AC 320 ms
22,520 KB
testcase_30 AC 186 ms
21,496 KB
testcase_31 AC 188 ms
21,620 KB
testcase_32 AC 188 ms
21,624 KB
testcase_33 AC 186 ms
21,620 KB
testcase_34 AC 185 ms
21,624 KB
testcase_35 AC 186 ms
21,624 KB
testcase_36 AC 187 ms
21,500 KB
testcase_37 AC 186 ms
21,500 KB
testcase_38 AC 186 ms
21,500 KB
testcase_39 AC 187 ms
21,492 KB
testcase_40 AC 189 ms
21,492 KB
testcase_41 AC 188 ms
21,236 KB
testcase_42 AC 191 ms
21,364 KB
testcase_43 AC 189 ms
21,496 KB
testcase_44 AC 190 ms
21,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
template<int MOD> class Modular {
private:
  long long value;
public:
  constexpr Modular() : value() {};
  constexpr Modular(const Modular& other) : value(other.value) {}
  template <typename U> constexpr Modular(const U& x) { value = normalize(x); }

  template <typename U> static long long normalize(const U& x) {
    long long v;
    if (-MOD <= x && x < MOD) v = static_cast<long long>(x);
    else v = static_cast<long long>(x % MOD);
    if (v < 0) v += MOD;
    return v;
  }

  constexpr long long inverse(long long x) {
    x = (x % MOD + MOD) % MOD;
    long long y = MOD, u = 1, v = 0;
    while(y) {
      long long t = x / y;
      x -= t * y; swap(x, y);
      u -= t * v; swap(u, v);
    }
    return (u % MOD + MOD) % MOD;
  }
  
  static long long mul(long long a, long long b) {
    long long res;
    #ifdef _WIN32
    unsigned long long x = a * b;
    unsigned xh = (unsigned) (x >> 32), xl = (unsigned) x, d, m;
    asm(
      "divl %4; \n\t"
      : "=a" (d), "=d" (m)
      : "d" (xh), "a" (xl), "r" (MOD)
    );
    res = m;
    #else
    res = a * b % MOD;
    #endif
    return res;
  }

  explicit operator long long() const noexcept { return value;}
  template <typename U> explicit operator U() const noexcept { return static_cast<U>(value); }

  constexpr Modular& operator=(const Modular& other) & noexcept { value = other.value; return *this; }
  template <typename U> constexpr Modular& operator=(const U& other) & noexcept { return *this = Modular(other); }

  constexpr Modular& operator+=(const Modular& other) noexcept { if ((value += other.value) >= MOD) value -= MOD; return *this; }
  template <typename U> constexpr Modular& operator+=(const U& other) noexcept { return *this += Modular(other); }

  constexpr Modular& operator-=(const Modular& other) noexcept { if ((value -= other.value) < 0) value += MOD; return *this; }
  template <typename U> constexpr Modular& operator-=(const U& other) noexcept { return *this -= Modular(other); }

  constexpr Modular& operator*=(const Modular& other) noexcept { this->value = mul(this->value, other.value); return *this; }
  template <typename U> constexpr Modular& operator*=(const U& other) noexcept {return *this *= Modular(other); }

  constexpr Modular& operator/=(const Modular& other) noexcept { return *this *= Modular(inverse(other.value)); }
  template <typename U> constexpr Modular& operator/=(const U& other) noexcept { return *this *= Modular(inverse(normalize(other))); }

  constexpr Modular& operator++() noexcept {return *this += 1; }
  constexpr Modular operator++(int) noexcept { Modular ret(*this); *this += 1; return ret; }

  constexpr Modular& operator--() noexcept {return *this -= 1; }
  constexpr Modular operator--(int) noexcept { Modular ret(*this); *this += 1; return ret; }

  constexpr Modular operator-() const { return Modular(-value); }

  friend constexpr bool operator==(const Modular<MOD>& lhs, const Modular<MOD>& rhs) noexcept { return lhs.value == rhs.value; }
  template <typename U> friend constexpr bool operator==(const Modular<MOD>& lhs, U rhs) noexcept { return lhs == Modular<MOD>(rhs); }
  template <typename U> friend constexpr bool operator==(U lhs, const Modular<MOD>& rhs) noexcept { return Modular<MOD>(lhs) == rhs; }

  friend constexpr bool operator!=(const Modular<MOD>& lhs, const Modular<MOD>& rhs) noexcept { return !(lhs == rhs); }
  template <typename U> friend constexpr bool operator!=(const Modular<MOD>& lhs, U rhs) noexcept { return !(lhs == rhs); }
  template <typename U> friend constexpr bool operator!=(U lhs, const Modular<MOD> rhs) noexcept { return !(lhs == rhs); }

  friend constexpr bool operator<(const Modular<MOD>& lhs, const Modular<MOD>& rhs) noexcept { return lhs.value < rhs.value; }
  template <typename U> friend constexpr bool operator<(const Modular<MOD> &lhs, U rhs) noexcept { return lhs.value < rhs; }
  template <typename U> friend constexpr bool operator<(U lhs, const Modular<MOD> &rhs) noexcept { return lhs < rhs.value; }

  friend constexpr bool operator>(const Modular<MOD>& lhs, const Modular<MOD>& rhs) noexcept { return rhs.value < lhs.value; }
  template <typename U> friend constexpr bool operator>(const Modular<MOD> &lhs, U rhs) noexcept { return rhs.value < lhs; }
  template <typename U> friend constexpr bool operator>(U lhs, const Modular<MOD> &rhs) noexcept { return rhs < lhs.value; }

  friend constexpr bool operator<=(const Modular<MOD>& lhs, const Modular<MOD>& rhs) noexcept { return !(lhs.value > rhs.value); }
  template <typename U> friend constexpr bool operator<=(const Modular<MOD> &lhs, U rhs) noexcept { return !(lhs.value > rhs); }
  template <typename U> friend constexpr bool operator<=(U lhs, const Modular<MOD> &rhs) noexcept { return !(lhs < rhs.value); }

  friend constexpr bool operator>=(const Modular<MOD>& lhs, const Modular<MOD>& rhs) noexcept { return !(lhs.value < rhs.value); }
  template <typename U> friend constexpr bool operator>=(const Modular<MOD> &lhs, U rhs) noexcept { return !(lhs.value < rhs); }
  template <typename U> friend constexpr bool operator>=(U lhs, const Modular<MOD> &rhs) noexcept { return !(lhs < rhs.value); }

  friend constexpr Modular<MOD> operator+(const Modular<MOD>& lhs, const Modular<MOD>& rhs) noexcept { return Modular<MOD>(lhs) += rhs; }
  template <typename U> friend constexpr Modular<MOD> operator+(const Modular<MOD>& lhs, U rhs) noexcept { return Modular<MOD>(lhs) += rhs; }
  template <typename U> friend constexpr Modular<MOD> operator+(U lhs, const Modular<MOD> &rhs) noexcept { return Modular<MOD>(lhs) += rhs; }

  friend constexpr Modular<MOD> operator-(const Modular<MOD>& lhs, const Modular<MOD>& rhs) noexcept { return Modular<MOD>(lhs) -= rhs; }
  template <typename U> friend constexpr Modular<MOD> operator-(const Modular<MOD>& lhs, U rhs) noexcept { return Modular<MOD>(lhs) -= rhs; }
  template <typename U> friend constexpr Modular<MOD> operator-(U lhs, const Modular<MOD> &rhs) noexcept { return Modular<MOD>(lhs) -= rhs; }

  friend constexpr Modular<MOD> operator*(const Modular<MOD>& lhs, const Modular<MOD>& rhs) noexcept { return Modular<MOD>(lhs) *= rhs; }
  template <typename U> friend constexpr Modular<MOD> operator*(const Modular<MOD>& lhs, U rhs) noexcept { return Modular<MOD>(lhs) *= rhs; }
  template <typename U> friend constexpr Modular<MOD> operator*(U lhs, const Modular<MOD> &rhs) noexcept { return Modular<MOD>(lhs) *= rhs; }

  friend constexpr Modular<MOD> operator/(const Modular<MOD>& lhs, const Modular<MOD>& rhs) noexcept { return Modular<MOD>(lhs) /= rhs; }
  template <typename U> friend constexpr Modular<MOD> operator/(const Modular<MOD>& lhs, U rhs) noexcept { return Modular<MOD>(lhs) /= rhs; }
  template <typename U> friend constexpr Modular<MOD> operator/(U lhs, const Modular<MOD> &rhs) noexcept { return Modular<MOD>(lhs) /= rhs; }

  friend std::ostream& operator<<(std::ostream& stream, const Modular<MOD>& number) noexcept { return stream << number.value; }
  friend std::istream& operator>>(std::istream& stream, Modular<MOD>& number) { long long in; stream >> in; number.value = Modular<MOD>::normalize(in); return stream; }
  
  constexpr int getmod() const { return MOD; }
};

template<int MOD, typename U> Modular<MOD> power(const Modular<MOD>& x, const U& y) {
  assert(y >= 0);
  Modular<MOD> k = x, result = 1;
  U p = y;
  while (p > 0) {
    if (p & 1) result *= k;
    k *= k;
    p >>= 1;
  }
  return result;
}

template<int MOD> class BinaryCoefficients {
private:
  vector<Modular<MOD>> fact_, inv_, finv_;
public:
  constexpr BinaryCoefficients(int n = 2020200) : fact_(n, 1), inv_(n, 1), finv_(n, 1) {
    for (int i = 2; i < n; i++) {
      fact_[i] = fact_[i - 1] * i;
      inv_[i] = -inv_[MOD % i] * (MOD / i);
      finv_[i] = finv_[i - 1] * inv_[i];
    }
  }
  constexpr Modular<MOD> comb(int n, int k) const noexcept { if (n < k || n < 0 || k < 0) return 0; return fact_[n] * finv_[k] * finv_[n - k]; }
  constexpr Modular<MOD> fact(int n) const noexcept { if (n < 0) return 0; return fact_[n]; }
  constexpr Modular<MOD> inv(int n) const noexcept { if (n < 0) return 0; return inv_[n]; }
  constexpr Modular<MOD> finv(int n) const noexcept { if (n < 0) return 0; return finv_[n]; }
};

constexpr int mod = 1e9 + 7;
//constexpr int mod = 998244353;
using mint = Modular<mod>;
using bicoef = BinaryCoefficients<mod>;

template<typename Monoid> class SegmentTree {
private:
  using Func = function<Monoid(Monoid, Monoid)>;
  Func func;
  Monoid unity;
  int siz;
  vector<Monoid> data;
  int n;
  int height;
  inline void build() {
    siz = 1;
    while (siz < n) siz <<= 1, ++height;
    data.assign(siz << 1, unity);
    for (int i = 0; i < n; i++) set(i, unity);
    for (int idx = siz - 1; idx > 0; idx--) data[idx] = func(data[(idx << 1) | 0], data[(idx << 1) | 1]);
  }
  template<typename T> inline void build(const T& arr) {
    if (!~n) n = (int) arr.size();
    siz = 1;
    while (siz < n) siz <<= 1, ++height;
    data.assign(siz << 1, unity);
    for (int i = 0; i < n; i++) set(i, arr[i]);
    for (int idx = siz - 1; idx > 0; idx--) data[idx] = func(data[(idx << 1) | 0], data[(idx << 1) | 1]);
  }
  inline void set(int idx, const Monoid& val) { assert(idx + siz < (int) data.size()); data[idx + siz] = val;}

public:
  SegmentTree() {}
  SegmentTree (const Func f, const Monoid& u, const int& n_) : func(f), unity(u), n(n_) { build(); }
  template<typename T> SegmentTree (const T& arr, const Func f, const Monoid& u, int n_ = -1) : func(f), unity(u), n(n_) { build(arr); }
  

  void initialize (const Func f, const Monoid &u, const int& n_) { func = f; unity = u; n = n_; build(); }
  template<typename T> void initialize (const T& arr, const Func f, const Monoid& u, int n_ = -1) { func = f; unity = u; n = n_; buld(arr); }

  void modify (int idx, const Monoid& val) {
    idx += siz;
    data[idx] = val;
    while (idx >>= 1) data[idx] = func(data[(idx << 1) | 0], data[(idx << 1) | 1]);
  }
  //[left, right) 
  Monoid get (int left, int right) {
    if (left > right) swap(left, right);
    Monoid val_left = unity, val_right = unity;
    for (int l = left + siz, r = right + siz; l < r; l >>= 1, r >>= 1) {
      if (l & 1) val_left = func(val_left, data[l++]);
      if (r & 1) val_right = func(data[--r], val_right);
    }
    return func(val_left, val_right);
  }

  template<typename U>
  int find (int start, int left, int right, int cur, Monoid& val, const U& f) {
    if (left + 1 == right) {
      val = func(val, data[cur]);
      return f(val) ? cur - siz : -1;
    }
    int mid = (left + right) >> 1;
    if (mid <= start) return find(start, mid, right, (cur << 1) | 1, val, f);
    if (start <= left && !f(func(val, data[cur]))) {
      val = func(val, data[cur]);
      return -1;
    }
    int val_left = find(start, left, mid, (cur << 1) | 0, val, f);
    if (~val_left) return val_left;
    return find(start, mid, right, (cur << 1) | 1, val, f);
  }
  template<typename U>
  int find (int start, const U& f) {
    Monoid val = unity;
    return find(start, 0, siz, 1, val, f);
  }

  inline Monoid operator[] (int idx) { return data[idx + siz]; }
  void print() {
    for (int i = 0; i < siz; i++) {
      cout << (*this)[i];
      if (i != siz - 1) cout << ", ";
    }
    cout << '\n';
  }
};

template <typename T> class compress {
public:
  map<T, int> zip;
  vector<T> unzip;
  compress (const vector<T>& v) {
    for (int i = 0; i < (int) v.size(); i++) unzip.push_back(v[i]);
    sort(unzip.begin(), unzip.end());
    unzip.erase(unique(unzip.begin(), unzip.end()), unzip.end());
    for (int i = 0; i < (int) unzip.size(); i++) zip[unzip[i]] = i;
  }
};

signed main() { 
  ios::sync_with_stdio(false); cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  auto f = [] (pair<int, mint> x, pair<int, mint> y) {
    if (x.first == y.first) return make_pair(x.first, x.second + y.second);
    else if (x.first > y.first) return x;
    else return y;
  };
  SegmentTree<pair<int, mint>> sgt(f, {-1, 0}, n + 10);
  compress<int> comp(a);
  for (int i = 0; i < n; i++) a[i] = comp.zip[a[i]] + 1;
  sgt.modify(0, {0, 1});
  for (int i = 0; i < n; i++) {
    auto p = sgt.get(0, a[i]);
    p.first++;
    sgt.modify(a[i],f(sgt.get(a[i], a[i] + 1), p));
  }
  cout << sgt.get(0, n + 5).second << endl;
  return 0;
}
0