結果

問題 No.992 最長増加部分列の数え上げ
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-10-02 03:33:04
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 4,780 bytes
コンパイル時間 2,031 ms
コンパイル使用メモリ 184,880 KB
実行使用メモリ 38,384 KB
最終ジャッジ日時 2023-09-22 00:00:53
合計ジャッジ時間 14,910 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 92 ms
16,680 KB
testcase_05 AC 66 ms
13,272 KB
testcase_06 AC 120 ms
19,728 KB
testcase_07 AC 81 ms
15,152 KB
testcase_08 AC 41 ms
9,352 KB
testcase_09 AC 83 ms
15,512 KB
testcase_10 AC 113 ms
19,868 KB
testcase_11 AC 149 ms
24,032 KB
testcase_12 AC 27 ms
7,404 KB
testcase_13 AC 78 ms
14,684 KB
testcase_14 AC 78 ms
14,948 KB
testcase_15 AC 28 ms
7,408 KB
testcase_16 AC 243 ms
35,004 KB
testcase_17 AC 41 ms
9,344 KB
testcase_18 AC 76 ms
14,824 KB
testcase_19 AC 145 ms
23,816 KB
testcase_20 AC 267 ms
38,384 KB
testcase_21 AC 271 ms
38,068 KB
testcase_22 AC 267 ms
38,052 KB
testcase_23 AC 266 ms
38,068 KB
testcase_24 AC 264 ms
38,008 KB
testcase_25 AC 264 ms
38,264 KB
testcase_26 AC 263 ms
38,380 KB
testcase_27 AC 262 ms
38,132 KB
testcase_28 AC 263 ms
38,200 KB
testcase_29 AC 264 ms
38,360 KB
testcase_30 AC 196 ms
38,068 KB
testcase_31 AC 197 ms
38,144 KB
testcase_32 AC 197 ms
38,200 KB
testcase_33 AC 196 ms
38,004 KB
testcase_34 AC 196 ms
38,144 KB
testcase_35 AC 198 ms
38,120 KB
testcase_36 AC 196 ms
38,068 KB
testcase_37 AC 196 ms
38,216 KB
testcase_38 AC 195 ms
38,264 KB
testcase_39 AC 196 ms
38,072 KB
testcase_40 AC 191 ms
38,148 KB
testcase_41 AC 194 ms
38,120 KB
testcase_42 AC 193 ms
38,200 KB
testcase_43 AC 193 ms
38,272 KB
testcase_44 AC 195 ms
38,068 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘std::ostream& operator<<(std::ostream&, const BinaryIndexedTree<T>&)’ 内:
main.cpp:56:5: 警告: 非 void を戻す関数内に return 文がありません [-Wreturn-type]
   56 |     }
      |     ^

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.10.02 03:32:58
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;



template< typename T >
struct BinaryIndexedTree {
    std::vector< T > data;
    BinaryIndexedTree() {}
    BinaryIndexedTree(int sz) {
        data.assign(++sz, 0);
    }
    
    inline T sum(int k) const {
        T ret = 0;
        for (k; k > 0; k -= k & -k) ret += data[k];
        return (ret);
    }
    
    inline T sum(int left, int right) const {
        return sum(right) - sum(left);
    }
    
    
    inline void add(int k, T x) {
        for (++k; k < data.size(); k += k & -k) data[k] += x;
    }
    
    int lower_bound(ll k) const {
        if (k <= 0) return 0;
        int res = 0;
        int N = 1; while (N < (int)data.size()) N *= 2;
        for (int i = N / 2; i > 0; i /= 2) {
            if (res + i < (int)data.size() && data[res + i] < k) {
                k -= data[res + i];
                res += i;
            }
        }
        return res;
    }
    
    friend std::ostream& operator<<(std::ostream &os, const BinaryIndexedTree &bit) {
        os << "[ ";
        for (int i = 0; i < bit.data.size() - 1; i++) {
            os << bit.sum(i, i + 1);
            if (i < bit.data.size() - 2) os << ", ";
        }
        os << " ]";
    }
};
template< int MOD >
struct mint {
public:
    long long x;
    mint(long long x = 0) :x((x%MOD+MOD)%MOD) {}
    mint(std::string &s) {
        long long z = 0;
        for (int i = 0; i < s.size(); i++) {
            z *= 10;
            z += s[i] - '0';
            z %= MOD;
        }
        this->x = z;
    }
    mint& operator+=(const mint &a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator-=(const mint &a) {
        if ((x += MOD - a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator*=(const mint &a) {
        (x *= a.x) %= MOD;
        return *this;
    }
    mint& operator/=(const mint &a) {
        long long n = MOD - 2;
        mint u = 1, b = a;
        while (n > 0) {
            if (n & 1) {
                u *= b;
            }
            b *= b;
            n >>= 1;
        }
        return *this *= u;
    }
    mint operator+(const mint &a) const {
        mint res(*this);
        return res += a;
    }
    mint operator-() const {return mint() -= *this; }
    mint operator-(const mint &a) const {
        mint res(*this);
        return res -= a;
    }
    mint operator*(const mint &a) const {
        mint res(*this);
        return res *= a;
    }
    mint operator/(const mint &a) const {
        mint res(*this);
        return res /= a;
    }
    friend std::ostream& operator<<(std::ostream &os, const mint &n) {
        return os << n.x;
    }
    friend std::istream &operator>>(std::istream &is, mint &n) {
        long long x;
        is >> x;
        n = mint(x);
        return is;
    }
    bool operator==(const mint &a) const {
        return this->x == a.x;
    }
    mint pow(long long k) const {
        mint ret = 1;
        mint p = this->x;
        while (k > 0) {
            if (k & 1) {
                ret *= p;
            }
            p *= p;
            k >>= 1;
        }
        return ret;
    }
};
constexpr int MOD = 1e9 + 7;
int main() {
    constexpr int INF = 1e9 + 6;
    int n;cin >> n;
    vector<pair<int,int>> a(n); 
    vector<pair<int,int>> lis(n); 
    int max_lis = 0;
    vector<int> count_lis(n+1,0);
    {
        vector<int> v(n,INF);
        for (int i = 0; i < n; i++) {
            cin >> a[i].first;
            a[i].second = i;
            auto lb = lower_bound(v.begin(), v.end(), a[i].first);
            *lb = a[i].first;
            lis[i].first = lb - v.begin() + 1;
            lis[i].second = count_lis[lis[i].first]++;
            max_lis = max(max_lis,lis[i].first);
        }
    }
    vector<BinaryIndexedTree<mint<MOD>>> bit(n+1);
    bit[0] = BinaryIndexedTree<mint<MOD>>(1);
    bit[0].add(0,1);
    for (int i = 0; i < n; i++) {
        bit[i+1] = BinaryIndexedTree<mint<MOD>>(count_lis[i+1]);
    }
    sort(a.begin(), a.end(), [](pair<int,int> a,pair<int,int> b){
        return a.first != b.first ? a.first < b.first : a.second > b.second;
    });
    
    
    
    mint<MOD> ans = 0;
    vector<set<pair<int,int>>> st(n+1); 
    st[0].insert({-1,0});
    for (int i = 0; i < n; i++) {
        int idx = a[i].second;
        
        auto k = st[lis[idx].first-1].lower_bound({idx,0});
        k--;
        
        mint<MOD> t = bit[lis[idx].first-1].sum(k->second+1);
        bit[lis[idx].first].add(lis[idx].second,t);
        st[lis[idx].first].insert({idx,lis[idx].second});
        
    }
    
    cout << bit[max_lis].sum(count_lis[max_lis]) << endl;
    return 0;
}
0