結果

問題 No.992 最長増加部分列の数え上げ
ユーザー betrue12betrue12
提出日時 2020-02-14 23:45:32
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 274 ms / 2,000 ms
コード長 2,832 bytes
コンパイル時間 2,592 ms
コンパイル使用メモリ 210,020 KB
実行使用メモリ 13,056 KB
最終ジャッジ日時 2024-04-16 03:01:20
合計ジャッジ時間 11,958 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 101 ms
7,936 KB
testcase_05 AC 71 ms
5,888 KB
testcase_06 AC 124 ms
8,192 KB
testcase_07 AC 89 ms
7,808 KB
testcase_08 AC 46 ms
5,504 KB
testcase_09 AC 93 ms
7,936 KB
testcase_10 AC 124 ms
8,192 KB
testcase_11 AC 155 ms
8,192 KB
testcase_12 AC 30 ms
5,376 KB
testcase_13 AC 86 ms
7,808 KB
testcase_14 AC 87 ms
7,808 KB
testcase_15 AC 30 ms
5,376 KB
testcase_16 AC 244 ms
12,800 KB
testcase_17 AC 46 ms
5,632 KB
testcase_18 AC 87 ms
7,808 KB
testcase_19 AC 153 ms
8,192 KB
testcase_20 AC 271 ms
12,928 KB
testcase_21 AC 271 ms
12,928 KB
testcase_22 AC 270 ms
12,928 KB
testcase_23 AC 271 ms
13,056 KB
testcase_24 AC 270 ms
12,928 KB
testcase_25 AC 271 ms
12,928 KB
testcase_26 AC 272 ms
12,928 KB
testcase_27 AC 271 ms
12,928 KB
testcase_28 AC 270 ms
13,056 KB
testcase_29 AC 274 ms
12,928 KB
testcase_30 AC 227 ms
12,928 KB
testcase_31 AC 228 ms
12,928 KB
testcase_32 AC 228 ms
12,928 KB
testcase_33 AC 227 ms
13,056 KB
testcase_34 AC 226 ms
12,928 KB
testcase_35 AC 232 ms
13,056 KB
testcase_36 AC 231 ms
12,928 KB
testcase_37 AC 233 ms
12,928 KB
testcase_38 AC 233 ms
13,056 KB
testcase_39 AC 232 ms
13,056 KB
testcase_40 AC 232 ms
13,056 KB
testcase_41 AC 230 ms
12,928 KB
testcase_42 AC 232 ms
12,928 KB
testcase_43 AC 232 ms
12,928 KB
testcase_44 AC 233 ms
12,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<typename T>
vector<T> compress(vector<T> A){
    sort(A.begin(), A.end());
    A.erase(unique(A.begin(), A.end()), A.end());
    return A;
}

template<typename T>
struct Segtree {
    int n, n_org;
    T e;
    vector<T> dat;
    typedef function<T(T a, T b)> Func;
    Func f;

    Segtree(){}
    Segtree(int n_input, Func f_input, T e_input){
        initialize(n_input, f_input, e_input);
    }
    void initialize(int n_input, Func f_input, T e_input){
        n_org = n_input;
        f = f_input;
        e = e_input;
        n = 1;
        while(n < n_input) n <<= 1;
        dat.resize(2*n-1, e);
    }

    void update(int k, T a){
        k += n - 1;
        dat[k] = f(dat[k], a);
        while(k > 0){
            k = (k - 1)/2;
            dat[k] = f(dat[2*k+1], dat[2*k+2]);
        }
    }

    T get(int k){
        return dat[k+n-1];
    }

    T between(int a, int b){
        return query(a, b+1, 0, 0, n);
    }

    T query(int a, int b, int k, int l, int r){
        if(r<=a || b<=l) return e;
        if(a<=l && r<=b) return dat[k];
        T vl = query(a, b, 2*k+1, l, (l+r)/2);
        T vr = query(a, b, 2*k+2, (l+r)/2, r);
        return f(vl, vr);
    }

    // [S, t] が条件checkを満たす最大のtを求める
    int bisect(int S, function<bool(T a)> check){
        T val = get(S);
        int k = S+n-1, l = S, r = S+1;
        while(true){
            while(k%2) k = (k-1)/2, r += r-l;
            T val2 = f(val, dat[k]);
            if(check(val2)){
                if(r == n) return n_org-1;
                val = val2;
                k++;
                int d = r-l;
                l += d, r += d;
            }else{
                break;
            }
        }
        while(k<n-1){
            T val2 = f(val, dat[2*k+1]);
            if(check(val2)){
                val = val2;
                k = 2*k+2;
                l = (l+r)/2;
            }else{
                k = 2*k+1;
                r = (l+r)/2;
            }
        }
        return min(l, n_org)-1;
    }
};

int main(){
    int N;
    cin >> N;
    vector<int> A(N);
    for(int i=0; i<N; i++) cin >> A[i];
    auto C = compress(A);
    int sz = C.size();
    for(int& a : A) a = lower_bound(C.begin(), C.end(), a) - C.begin();

    const int64_t MOD = 1e9+7;

    typedef pair<int, int64_t> P;
    Segtree<P> st(sz, [](P a, P b){
        if(a.first == b.first){
            return make_pair(a.first, (a.second + b.second) % MOD);
        }else{
            return max(a, b);
        }
    }, {0, 0});

    for(int a : A){
        st.update(a, {1, 1});
        auto res = st.between(0, a-1);
        if(res.first > 0){
            res.first++;
            st.update(a, res);
        }
    }
    cout << st.between(0, sz-1).second << endl;
    return 0;
}
0