結果

問題 No.992 最長増加部分列の数え上げ
ユーザー knshnb
提出日時 2020-02-20 21:36:46
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 207 ms / 2,000 ms
コード長 5,541 bytes
コンパイル時間 2,546 ms
コンパイル使用メモリ 216,036 KB
最終ジャッジ日時 2025-01-09 01:11:43
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <bits/stdc++.h> // clang-format off
using namespace std;
using Int = long long;
#define REP2(i, n) for (Int i = 0, max_i = (n); i < max_i; i++)
#define REP3(i, a, b) for (Int i = (a), max_i = (b); i < max_i; i++)
#define OVERLOAD2(_1, _2, _3, name, ...) name
#define REP(...) OVERLOAD2(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
struct SetupIO { SetupIO() { cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(13); } } setup_io;
#ifndef _MY_DEBUG
#define dump(...)
#endif // clang-format on
/**
* author: knshnb
* created: Thu Feb 20 21:05:46 JST 2020
**/
template <class T> T pow(T x, int n, const T UNION = 1) {
T ret = UNION;
while (n) {
if (n & 1) ret *= x;
x *= x;
n >>= 1;
}
return ret;
}
template <int MD> struct ModInt {
int x;
static unordered_map<int, int> to_inv;
ModInt() : x(0) {}
ModInt(long long x_) {
if ((x = x_ % MD + MD) >= MD) x -= MD;
}
ModInt& operator+=(ModInt that) {
if ((x += that.x) >= MD) x -= MD;
return *this;
}
ModInt& operator*=(ModInt that) {
x = (unsigned long long)x * that.x % MD;
return *this;
}
ModInt& operator-=(ModInt that) {
if ((x -= that.x) < 0) x += MD;
return *this;
}
ModInt& operator/=(ModInt that) {
x = (unsigned long long)x * that.inv().x % MD;
return *this;
}
ModInt operator-() const { return -x < 0 ? MD - x : -x; }
ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
bool operator==(ModInt that) const { return x == that.x; }
bool operator!=(ModInt that) const { return x != that.x; }
ModInt inv() const { return to_inv.count(this->x) ? to_inv[this->x] : (to_inv[this->x] = pow(*this, MD - 2).x); }
friend ostream& operator<<(ostream& s, ModInt<MD> a) {
s << a.x;
return s;
}
friend istream& operator>>(istream& s, ModInt<MD>& a) {
long long tmp;
s >> tmp;
a = ModInt<MD>(tmp);
return s;
}
friend string to_string(ModInt<MD> a) { return to_string(a.x); }
};
template <int MD> unordered_map<int, int> ModInt<MD>::to_inv;
using mint = ModInt<1000000007>;
vector<mint> fact, fact_inv;
void init_factorial(int n) {
fact = vector<mint>(n + 1, 1);
fact_inv = vector<mint>(n + 1);
for (int i = 0; i < n; i++) fact[i + 1] = fact[i] * (i + 1);
fact_inv[n] = mint(1) / fact[n];
for (int i = n - 1; i >= 0; i--) fact_inv[i] = fact_inv[i + 1] * (i + 1);
// for (int i = 0; i < n + 1; i++) assert(fact[i] * fact_inv[i] == 1);
}
mint comb(int n, int r) { return fact[n] * fact_inv[r] * fact_inv[n - r]; }
template <class T, class F> struct SegmentTree {
const F op;
const T e;
SegmentTree(F op_, T e_) : op(op_), e(e_) {}
int n;
vector<T> t;
void set_by_identity(int n_) {
n = n_;
t.clear(), t.resize(2 * n, e);
}
void set_by_vector(const vector<T>& a) {
n = a.size();
t.clear(), t.resize(2 * n, e);
for (int i = 0; i < n; i++) t[i + n] = a[i];
build();
}
void build() {
for (int i = n - 1; i; --i) t[i] = op(t[2 * i], t[2 * i + 1]);
}
T& operator[](int i) { return t[i + n]; }
// [l, r)
T query(int l, int r) const {
assert(0 <= l && l <= r && r <= n);
T resl = e, resr = e;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) resl = op(resl, t[l++]);
if (r & 1) resr = op(t[--r], resr);
}
return op(resl, resr);
}
// ia
void update(int i, const T& x) {
assert(0 <= i && i < n);
i += n;
t[i] = op(t[i], x);
while (i >>= 1) t[i] = op(t[2 * i], t[2 * i + 1]);
}
friend string to_string(const SegmentTree<T, F>& seg) {
return to_string(vector<T>(seg.t.begin() + seg.n, seg.t.end()));
}
};
template <class T, class F> auto make_segment_tree(F op, T e) { return SegmentTree<T, F>(op, e); }
template <class T> struct Compressor {
vector<T> sorted;
Compressor(const vector<T>& a) : sorted(a) {
sort(sorted.begin(), sorted.end());
sorted.erase(unique(sorted.begin(), sorted.end()), sorted.end());
}
int get(T x) {
auto it = lower_bound(sorted.begin(), sorted.end(), x);
assert(*it == x);
return it - sorted.begin();
}
};
signed main() {
Int n;
cin >> n;
vector<Int> a(n);
REP(i, n) cin >> a[i];
Compressor<Int> cp(a);
// {max, max_num}
auto seg = make_segment_tree<pair<Int, mint>>(
[](pair<Int, mint> a, pair<Int, mint> b) -> pair<Int, mint> {
mint num = 0;
if (a.first >= b.first) num += a.second;
if (a.first <= b.first) num += b.second;
return {max(a.first, b.first), num};
},
{-1e18, 0});
seg.set_by_vector(vector<pair<Int, mint>>(n + 1, {0, 0}));
Int ma = -1;
REP(i, n) {
Int x = cp.get(a[i]) + 1;
auto prv = seg.query(0, x);
prv.first++;
if (prv.second == 0) prv.second += 1;
seg.update(x, prv);
ma = max(ma, seg[x].first);
}
mint ans = 0;
REP(i, n + 1) {
if (seg[i].first == ma) ans += seg[i].second;
}
cout << ans << endl;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0