結果

問題 No.510 二次漸化式
ユーザー PachicobuePachicobue
提出日時 2017-07-15 02:14:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 216 ms / 3,000 ms
コード長 8,303 bytes
コンパイル時間 1,894 ms
コンパイル使用メモリ 171,548 KB
実行使用メモリ 50,816 KB
最終ジャッジ日時 2024-04-16 20:56:43
合計ジャッジ時間 7,711 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 77 ms
5,376 KB
testcase_03 AC 76 ms
5,376 KB
testcase_04 AC 76 ms
5,376 KB
testcase_05 AC 76 ms
5,376 KB
testcase_06 AC 111 ms
8,832 KB
testcase_07 AC 106 ms
8,832 KB
testcase_08 AC 111 ms
8,832 KB
testcase_09 AC 110 ms
8,832 KB
testcase_10 AC 43 ms
5,376 KB
testcase_11 AC 43 ms
5,376 KB
testcase_12 AC 43 ms
5,376 KB
testcase_13 AC 43 ms
5,376 KB
testcase_14 AC 42 ms
5,376 KB
testcase_15 AC 43 ms
5,376 KB
testcase_16 AC 129 ms
50,688 KB
testcase_17 AC 129 ms
50,560 KB
testcase_18 AC 131 ms
50,688 KB
testcase_19 AC 129 ms
50,560 KB
testcase_20 AC 131 ms
50,560 KB
testcase_21 AC 129 ms
50,688 KB
testcase_22 AC 130 ms
50,432 KB
testcase_23 AC 195 ms
50,688 KB
testcase_24 AC 197 ms
50,816 KB
testcase_25 AC 198 ms
50,560 KB
testcase_26 AC 196 ms
50,688 KB
testcase_27 AC 193 ms
50,560 KB
testcase_28 AC 193 ms
50,560 KB
testcase_29 AC 194 ms
50,560 KB
testcase_30 AC 195 ms
50,560 KB
testcase_31 AC 214 ms
50,560 KB
testcase_32 AC 214 ms
50,560 KB
testcase_33 AC 216 ms
50,432 KB
testcase_34 AC 163 ms
50,560 KB
testcase_35 AC 143 ms
50,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define show(x) cout << #x << " = " << x << endl

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz=" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = 1e9 + 7;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;

struct Matrix {
    Matrix& operator=(const Matrix& m)
    {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                table[i][j] = m.table[i][j];
            }
        }
        return (*this);
    }
    Matrix operator*(const Matrix& m) const
    {
        Matrix result;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                ll v = 0;
                for (int k = 0; k < 4; k++) {
                    v = (v + table[i][k] * m.table[k][j]) % MOD;
                }
                result.table[i][j] = v;
            }
        }
        return result;
    }
    static Matrix unit()
    {
        Matrix result;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (i == j) {
                    result.table[i][i] = 1;
                } else {
                    result.table[i][j] = 0;
                }
            }
        }
        return result;
    }
    static Matrix initial()
    {
        Matrix init;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                init.table[i][j] = 0;
            }
        }
        init.table[0][0] = 1;
        init.table[1][3] = 1;
        init.table[2][3] = 1;
        init.table[3][3] = 1;
        return init;
    }

    array<array<ll, 4>, 4> table;
};

ostream& operator<<(ostream& os, const Matrix& m)
{
    os << "[" << endl;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            cout << m.table[i][j] << " ";
        }
        cout << endl;
    }
    cout << "]\n";
    return os;
}

template <typename Base>
struct SegmentTree {
public:
    using BaseAlgebra = Base;
    using AccMonoid = typename BaseAlgebra::AccMonoid;
    using OpMonoid = typename BaseAlgebra::OpMonoid;
    using T = typename BaseAlgebra::T;
    using F = typename BaseAlgebra::OpMonoid::T;

    SegmentTree(const int n) : data_num(n), height(__lg(2 * data_num - 1)), size(1 << (1 + height)), half(size >> 1), value(size, AccMonoid::identity()), action(size, OpMonoid::identity()) { assert(n > 0); }
    SegmentTree(const std::vector<T>& val) : data_num(val.size()), height(__lg(2 * data_num - 1)), size(1 << (1 + height)), half(size >> 1), value(size), action(size, OpMonoid::identity())
    {
        for (int data = 0; data < half; data++) {
            if (data < data_num) {
                value[data + half] = val[data];
            } else {
                value[data + half] = AccMonoid::identity();
            }
        }
        for (int node = half - 1; node >= 1; node--) {
            value[node] = acc(value[2 * node], value[2 * node + 1]);
        }
    }

    T get(const int a) const
    {
        assert(0 <= a and a < data_num);
        return accumulate(a, a + 1);
    }

    void set(const int a, const T& val)
    {
        assert(0 <= a and a < data_num);
        const int node = a + half;
        value[node] = val;
        for (int i = node / 2; i > 0; i /= 2) {
            value[i] = acc(value[2 * i], value[2 * i + 1]);
        }
    }

    T accumulate(const int a, const int b) const  // Accumulate (a,b]
    {
        assert(0 <= a and a < b and b <= data_num);
        return accumulateRec(1, 0, half, a, b);
    }

    void modify(const int a, const int b, const F& f)  // Apply f on (a,b]
    {
        assert(0 <= a and a < b and b <= data_num);
        if (f == OpMonoid::identity()) {
            return;
        }
        modifyRec(1, 0, half, a, b, f);
    }

private:
    void modifyRec(const int int_index, const int int_left, const int int_right, const int mod_left, const int mod_right, const F& f)
    {
        if (mod_left <= int_left and int_right <= mod_right) {
            value[int_index] = act(f, value[int_index]);
            action[int_index] = compose(f, action[int_index]);
        } else if (int_right <= mod_left or mod_right <= int_left) {
            // Do nothing
        } else {
            modifyRec(2 * int_index, int_left, (int_left + int_right) / 2, 0, half, action[int_index]);
            modifyRec(2 * int_index, int_left, (int_left + int_right) / 2, mod_left, mod_right, f);
            modifyRec(2 * int_index + 1, (int_left + int_right) / 2, int_right, 0, half, action[int_index]);
            modifyRec(2 * int_index + 1, (int_left + int_right) / 2, int_right, mod_left, mod_right, f);
            value[int_index] = acc(value[2 * int_index], value[2 * int_index + 1]);
            action[int_index] = OpMonoid::identity();
        }
    }

    T accumulateRec(const int int_index, const int int_left, const int int_right, const int mod_left, const int mod_right) const
    {
        if (mod_left <= int_left and int_right <= mod_right) {
            return value[int_index];
        } else if (int_right <= mod_left or mod_right <= int_left) {
            return AccMonoid::identity();
        } else {
            return act(action[int_index], acc(accumulateRec(2 * int_index, int_left, (int_left + int_right) / 2, mod_left, mod_right),
                                              accumulateRec(2 * int_index + 1, (int_left + int_right) / 2, int_right, mod_left, mod_right)));
        }
    }

    const int data_num;  // Num of valid data on leaves.
    const int height;
    const int size;
    const int half;
    vector<T> value;   // Tree for value(length: size)
    vector<F> action;  // Tree for action(length: half)
    bool has_lazy;

    const AccMonoid acc{};
    const OpMonoid compose{};
    const BaseAlgebra act{};
};

struct MatrixMonoid {
    using T = Matrix;

    struct AccMonoid {
        T operator()(const T& a, const T& b) const { return b * a; }
        static T identity() { return Matrix::unit(); }
    };

    struct OpMonoid {
        using T = ll;
        T operator()(const T& /*f1*/, const T& /*f2*/) const { return 0; }
        static constexpr T identity() { return 0; }
    };

    T operator()(const OpMonoid::T& /*f*/, const T& x) const { return x; }
};

inline Matrix x_renewal(const Matrix& m, ll newval)
{
    Matrix result;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            result.table[i][j] = m.table[i][j];
        }
    }
    result.table[0][1] = newval;
    return result;
}

inline Matrix y_renewal(const Matrix& m, ll newval)
{
    Matrix result;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            result.table[i][j] = m.table[i][j];
        }
    }
    result.table[1][1] = (newval * newval) % MOD;
    result.table[1][2] = (2 * newval) % MOD;
    result.table[2][2] = newval;
    return result;
}

int main()
{
    int n;
    cin >> n;

    vector<Matrix> initial_value(n, Matrix::initial());

    SegmentTree<MatrixMonoid> seg(initial_value);

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        char c;
        cin >> c;
        if (c == 'x') {
            int ind;
            ll x;
            cin >> ind >> x;
            const auto m = seg.get(ind);
            const auto val = x_renewal(m, x);
            seg.set(ind, val);
        } else if (c == 'y') {
            int ind;
            ll y;
            cin >> ind >> y;
            const auto m = seg.get(ind);
            const auto val = y_renewal(m, y);
            seg.set(ind, val);
        } else {
            int ind;
            cin >> ind;
            if (ind == 0) {
                cout << 1 << endl;
            } else {
                const Matrix acc = seg.accumulate(0, ind);
                cout << (acc.table[0][0] + acc.table[0][1] + acc.table[0][2] + acc.table[0][3]) % MOD << endl;
            }
        }
    }
    return 0;
}
0