結果

問題 No.1378 Flattening
ユーザー IKyoproIKyopro
提出日時 2020-10-31 15:04:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,970 ms / 2,000 ms
コード長 3,616 bytes
コンパイル時間 2,213 ms
コンパイル使用メモリ 204,244 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-29 10:26:33
合計ジャッジ時間 33,465 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1,663 ms
4,380 KB
testcase_32 AC 1,636 ms
4,380 KB
testcase_33 AC 1,661 ms
4,380 KB
testcase_34 AC 1,609 ms
4,376 KB
testcase_35 AC 1,664 ms
4,380 KB
testcase_36 AC 1,666 ms
4,376 KB
testcase_37 AC 1,877 ms
4,376 KB
testcase_38 AC 1,881 ms
4,376 KB
testcase_39 AC 1,879 ms
4,376 KB
testcase_40 AC 1,881 ms
4,380 KB
testcase_41 AC 1,878 ms
4,376 KB
testcase_42 AC 1,802 ms
4,376 KB
testcase_43 AC 1,970 ms
4,380 KB
testcase_44 AC 1,801 ms
4,376 KB
testcase_45 AC 125 ms
4,376 KB
testcase_46 AC 215 ms
4,376 KB
testcase_47 AC 22 ms
4,376 KB
testcase_48 AC 1,426 ms
4,376 KB
testcase_49 AC 1,628 ms
4,376 KB
testcase_50 AC 101 ms
4,376 KB
testcase_51 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> using vec = vector<T>;
template<class T> using vvec = vector<vec<T>>;
template<class T> bool chmin(T& a,T b){if(a>b) {a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b){if(a<b) {a = b; return true;} return false;}
#define all(x) (x).begin(),(x).end()
#define debug(x)  cerr << #x << " = " << (x) << endl;

constexpr ll mod = 998244353;
struct mint {
    ll x;
    mint(ll x=0):x((x%mod+mod)%mod){}
    
    friend ostream &operator<<(ostream& os,const mint& a){
        return os << a.x;
    }

    friend istream &operator>>(istream& is,mint& a){
        ll t;
        is >> t;
        a = mint(t);
        return (is);
    }

    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) 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;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res/=a;
    }
    bool operator==(const mint a)const{
        return x==a.x;
    }
};

template<typename Monoid,typename F>
class SegmentTree{
private:
    int sz;
    vector<Monoid> seg;
    const F op;
    const Monoid e;
public:
    SegmentTree(int n,const F op,const Monoid &e):op(op),e(e){
        sz = 1;
        while(sz<=n) sz <<= 1;
        seg.assign(2*sz,e);
    }
    void set(int k, const Monoid &x){
        seg[k+sz] = x;
    }
    void build(){
        for(int i=sz-1;i>0;i--){
            seg[i] = op(seg[2*i],seg[2*i+1]);
        }
    }
    void update(int k,const Monoid &x){
        k += sz;
        seg[k] = x;
        while(k>>=1){
            seg[k] = op(seg[2*k],seg[2*k+1]);
        }
    }
    Monoid query(int l,int r){
        Monoid L = e,R = e;
        for(l+=sz,r+=sz;l<r;l>>=1,r>>=1){
            if(l&1) L = op(L,seg[l++]);
            if(r&1) R = op(seg[--r],R);
        }
        return op(L,R);
    }
    Monoid operator[](const int &k)const{
        return seg[k+sz];
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vec<int> A(N);
    SegmentTree seg(N,[](int a,int b){return min(a,b);},N);
    vec<int> pos(N);
    for(int i=0;i<N;i++){
        cin >> A[i];
        A[i]--;
        pos[A[i]] = i;
        seg.set(i,A[i]);
    }
    seg.build();
    vec<mint> dp(N,0);
    vec<mint> R(N+1,1),L(N+1);
    mint sum = 1;
    for(int i=0;i<N;i++){
        vec<mint> ndp(N);
        for(int j=0;j<N;j++){
            if(j<=A[i]) ndp[j] += dp[j];
            if(i<=pos[j] && seg.query(i,pos[j])>j) ndp[j] += L[j]+R[j+1];
            if(pos[j]<i && seg.query(pos[j],i+1)==j) ndp[j] += L[j];
        }
        dp.swap(ndp);
        R[N] = 0;
        for(int j=N-1;j>=0;j--) R[j] = R[j+1]+dp[j];
        for(int j=0;j<N;j++) L[j+1] = L[j]+(pos[j]<=i? dp[j]:0);
    }
    cout << accumulate(all(dp),(mint) 0) << "\n";
}
0