結果

問題 No.991 N×Mマス計算(構築)
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2020-02-14 23:19:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 7,103 bytes
コンパイル時間 2,676 ms
コンパイル使用メモリ 212,836 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-04-16 02:54:40
合計ジャッジ時間 7,213 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
int mo = 1000000007;
#define def 0
using V = int;
V comp(V& l, V& r) { return (l + r) % mo; };
struct SegTree { //[l,r)
    int NV;
    vector<V> val;
    void init(int n) {
        NV = 1;
        while (NV < n) NV *= 2;
        val = vector<V>(NV * 2, def);
    }
    V get(int x, int y, int l, int r, int k) {
        if (r <= x || y <= l) return def; if (x <= l&&r <= y)return val[k];
        auto a = get(x, y, l, (l + r) / 2, k * 2); auto b = get(x, y, (l + r) / 2, r, k * 2 + 1); return comp(a, b);
    }
    V get(int x, int y) { return get(x, y, 0, NV, 1); }
    void update(int i, V v) { i += NV; val[i] = v; while (i>1)i >>= 1, val[i] = comp(val[i * 2], val[i * 2 + 1]); }
    void add(int i, V v) { update(i, val[i + NV] + v); }
    V operator[](int x) { assert(0 <= x and x < NV); return val[x + NV]; }
};

struct Healthy2DSegTreeVer2 {
    int NV;
    vector<SegTree> st;
    vector<vector<int>> index;
    
    void init(vector<vector<int>> &v) {
        int n = v.size();
        NV = 1; while (NV < n) NV *= 2;
        index.resize(2 * NV);
        rep(i, 0, n) fore(j, v[i]) index[i + NV].push_back(j);
        rrep(i, NV * 2 - 1, 1) {
            sort(index[i].begin(), index[i].end());
            index[i].erase(unique(index[i].begin(), index[i].end()), index[i].end());
            fore(j, index[i]) index[i / 2].push_back(j);
        }
        st.resize(2 * NV);
        rep(i, 1, NV * 2) st[i].init(index[i].size());
    }
    void restupdate(int x, int y) {
        if (x == 0) return;
        assert(1 <= x);

        while (x) {
            int yy = lower_bound(index[x].begin(), index[x].end(), y) - index[x].begin();
            assert(yy != index[x].size());
            assert(y == index[x][yy]);

            int x1 = x * 2;
            int yy1 = -1;
            int y1 = lower_bound(index[x1].begin(), index[x1].end(), y) - index[x1].begin();
            if (y1 == index[x1].size()) y1 = 0;
            else yy1 = index[x1][y1];


            int x2 = x * 2 + 1;
            int yy2 = -1;
            int y2 = lower_bound(index[x2].begin(), index[x2].end(), y) - index[x2].begin();
            if (y2 == index[x2].size()) y2 = 0;
            else yy2 = index[x2][y2];

            if (yy1 == y and yy2 == y) {
                V a = st[x1][y1];
                V b = st[x2][y2];
                st[x].update(yy, comp(a, b));
            }
            else if (yy1 == y) {
                V a = st[x1][y1];
                st[x].update(yy, a);
            }
            else if (yy2 == y) {
                V a = st[x2][y2];
                st[x].update(yy, a);
            }
            else assert(0);

            x /= 2;
        }
    }
    void update(int x, int y, V v) {
        assert(x < NV);
        x += NV;
        int yy = lower_bound(index[x].begin(), index[x].end(), y) - index[x].begin();
        assert(yy != index[x].size());
        assert(y == index[x][yy]);
        st[x].update(yy, v);
        restupdate(x / 2, y);
    }
    void add(int x, int y, V v) {
        assert(x < NV);
        x += NV;
        int yy = lower_bound(index[x].begin(), index[x].end(), y) - index[x].begin();
        assert(yy != index[x].size());
        assert(y == index[x][yy]);
        st[x].add(yy, v);
        restupdate(x / 2, y);
    }
    V get(int sx, int tx, int sy, int ty, int k, int l, int r) {
        assert(k < NV * 2);
        assert(l < r);
        if (r <= sx or tx <= l) return def;
        if (sx <= l and r <= tx) {
            int syy = lower_bound(index[k].begin(), index[k].end(), sy) - index[k].begin();
            int tyy = lower_bound(index[k].begin(), index[k].end(), ty) - index[k].begin();
            return st[k].get(syy, tyy);
        }
        int md = (l + r) / 2;
        V le = get(sx, tx, sy, ty, k * 2, l, md);
        V ri = get(sx, tx, sy, ty, k * 2 + 1, md, r);
        return comp(le, ri);
    }
    V get(int sx, int tx, int sy, int ty) {
        return get(sx, tx, sy, ty, 1, 0, NV);
    }
};
vector<int> compress1(int* v, int n) {
    vector<int> dic;
    rep(i, 0, n) dic.push_back(v[i]);
    sort(all(dic));
    dic.erase(unique(all(dic)), dic.end());
    rep(i, 0, n) v[i] = lower_bound(all(dic), v[i]) - dic.begin();
    return dic;
}
#define def 0
template<class V, int NV> struct SegTreeNormal { //[l,r)
    V comp(V& l, V& r) { return max(l, r); };

    vector<V> val; SegTreeNormal() { val = vector<V>(NV * 2, def); }
    V get(int x, int y, int l = 0, int r = NV, int k = 1) {
        if (r <= x || y <= l)return def; if (x <= l && r <= y)return val[k];
        auto a = get(x, y, l, (l + r) / 2, k * 2);
        auto b = get(x, y, (l + r) / 2, r, k * 2 + 1);
        return comp(a, b);
    }
    void update(int i, V v) {
        i += NV; val[i] = v;
        while (i > 1) i >>= 1, val[i] = comp(val[i * 2], val[i * 2 + 1]);
    }
    void add(int i, V v) { update(i, val[i + NV] + v); }
    V operator[](int x) { return get(x, x + 1); }
};
/*---------------------------------------------------------------------------------------------------
            ∧_∧
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     @hamayanhamayan0
    /   \     | |
    /   / ̄ ̄ ̄ ̄/  |
  __(__ニつ/     _/ .| .|____
     \/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/














int N, A[201010];
SegTreeNormal<int, 1 << 18> st;
int B[201010];
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N;
    rep(i, 0, N) cin >> A[i];
    compress1(A, N);

    rep(i, 0, N) {
        B[i] = st.get(0, A[i]) + 1;
        st.update(A[i], max(st[A[i]], B[i] + 1));
    }

    vector<vector<int>> dic(N);
    rep(i, 0, N) dic[i].push_back(B[i]);
    
    Healthy2DSegTreeVer2 st2;
    st2.init(dic);

    rep(i, 0, N) {
        if (B[i] == 1) st2.add(i, B[i], 1);
        else st2.add(i, B[i], st2.get(0, B[i] - 1, i, B[i]));
    }

    int ma = st.get(0, N);
    int ans = st2.get(0, N, ma, ma + 1);
    cout << ans << endl;

}





0