結果

問題 No.992 最長増加部分列の数え上げ
ユーザー Y17Y17
提出日時 2020-02-14 22:23:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 306 ms / 2,000 ms
コード長 2,244 bytes
コンパイル時間 1,911 ms
コンパイル使用メモリ 168,932 KB
実行使用メモリ 17,260 KB
最終ジャッジ日時 2024-04-16 02:34:27
合計ジャッジ時間 11,649 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 110 ms
10,988 KB
testcase_05 AC 78 ms
7,912 KB
testcase_06 AC 134 ms
10,984 KB
testcase_07 AC 95 ms
11,108 KB
testcase_08 AC 49 ms
8,036 KB
testcase_09 AC 99 ms
11,084 KB
testcase_10 AC 135 ms
11,056 KB
testcase_11 AC 169 ms
10,980 KB
testcase_12 AC 32 ms
6,400 KB
testcase_13 AC 93 ms
10,984 KB
testcase_14 AC 94 ms
10,988 KB
testcase_15 AC 33 ms
6,272 KB
testcase_16 AC 279 ms
17,252 KB
testcase_17 AC 49 ms
8,036 KB
testcase_18 AC 90 ms
11,112 KB
testcase_19 AC 171 ms
10,984 KB
testcase_20 AC 304 ms
17,128 KB
testcase_21 AC 304 ms
17,256 KB
testcase_22 AC 300 ms
17,240 KB
testcase_23 AC 304 ms
17,256 KB
testcase_24 AC 305 ms
17,256 KB
testcase_25 AC 302 ms
17,256 KB
testcase_26 AC 303 ms
17,128 KB
testcase_27 AC 303 ms
17,132 KB
testcase_28 AC 301 ms
17,128 KB
testcase_29 AC 306 ms
17,128 KB
testcase_30 AC 215 ms
17,128 KB
testcase_31 AC 218 ms
17,128 KB
testcase_32 AC 216 ms
17,260 KB
testcase_33 AC 218 ms
17,124 KB
testcase_34 AC 212 ms
17,256 KB
testcase_35 AC 213 ms
17,252 KB
testcase_36 AC 199 ms
17,252 KB
testcase_37 AC 212 ms
17,128 KB
testcase_38 AC 227 ms
17,132 KB
testcase_39 AC 202 ms
17,124 KB
testcase_40 AC 212 ms
17,132 KB
testcase_41 AC 214 ms
17,252 KB
testcase_42 AC 214 ms
17,128 KB
testcase_43 AC 215 ms
17,132 KB
testcase_44 AC 215 ms
17,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <class Monoid> struct SegmentTree{
    using Func = function<Monoid(Monoid, Monoid)>;

    int size;
    vector<Monoid> seg;
    const Func merge;
    const Monoid def;

    SegmentTree(int n, Monoid d, const Func f): merge(f), def(d){
        for(size = 1; size <= n; size <<= 1);
        seg.assign(size*2, def);
    }

    Monoid& operator[] (int i) { return seg[i+size]; };
    void build(){ for(int i = size-1;i >= 0;i--) seg[i] = merge(seg[i*2], seg[i*2+1]); } 

    void update(int i, Monoid val){
        seg[i += size] = val;
        while(i >>= 1) seg[i] = merge(seg[i*2], seg[i*2+1]);
    }

    void add(int i, Monoid val){ update(i, val+seg[i+size]); }

    Monoid get(int a, int b){
        Monoid l = def, r = def;
        for(a += size, b += size; a < b; a >>= 1, b >>= 1){
            if(a & 1) l = merge(l, seg[a++]);
            if(b & 1) r = merge(r, seg[--b]);
        }
        return merge(l, r);
    }
};


#define int long long
#define MOD 1000000007

typedef pair<int, int> P;

signed main(){
    int n;
    cin >> n;
    SegmentTree<P> seg(n, {0, 0}, [](P a, P b){
        if(a.first > b.first){
            return a;
        }else if(a.first < b.first){
            return b;
        }
        a.second += b.second;
        a.second %= MOD;
        return a;
    });

    int a[200010];
    vector<pair<int, int>> azip;
    for(int i = 0;i < n;i++){
        int b;
        cin >> b;
        a[i] = b;
        azip.push_back({b, i});
    }
    sort(azip.begin(), azip.end());

    int atmp = 0;
    int ans = 0;
    for(int i = n-1;i >= 0;i--){
        pair<int, int> pp = {a[i], i};
        pair<int, int> pp2 = {a[i]+1, INT_MIN};
        int idx = lower_bound(azip.begin(), azip.end(), pp) - azip.begin();
        int start = lower_bound(azip.begin(), azip.end(), pp2) - azip.begin();
        auto get = seg.get(start, n);
        get.first++;
        if(get.second == 0) get.second = 1;
        seg.update(idx, get);
        if(atmp == get.first){
            ans += get.second;
            ans %= MOD;
        }else if(atmp < get.first){
            atmp = get.first;
            ans = get.second;
        }
    }

    cout << ans << endl;

    return 0;
}
0