結果

問題 No.510 二次漸化式
ユーザー tsutajtsutaj
提出日時 2017-08-02 13:46:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 464 ms / 3,000 ms
コード長 2,758 bytes
コンパイル時間 1,075 ms
コンパイル使用メモリ 82,424 KB
実行使用メモリ 87,680 KB
最終ジャッジ日時 2024-04-19 13:27:53
合計ジャッジ時間 15,046 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
87,540 KB
testcase_01 AC 125 ms
87,396 KB
testcase_02 AC 354 ms
87,424 KB
testcase_03 AC 348 ms
87,508 KB
testcase_04 AC 345 ms
87,552 KB
testcase_05 AC 350 ms
87,552 KB
testcase_06 AC 363 ms
87,424 KB
testcase_07 AC 363 ms
87,424 KB
testcase_08 AC 367 ms
87,552 KB
testcase_09 AC 368 ms
87,424 KB
testcase_10 AC 344 ms
87,424 KB
testcase_11 AC 348 ms
87,680 KB
testcase_12 AC 358 ms
87,552 KB
testcase_13 AC 351 ms
87,648 KB
testcase_14 AC 356 ms
87,552 KB
testcase_15 AC 357 ms
87,552 KB
testcase_16 AC 273 ms
87,424 KB
testcase_17 AC 275 ms
87,604 KB
testcase_18 AC 294 ms
87,552 KB
testcase_19 AC 285 ms
87,552 KB
testcase_20 AC 283 ms
87,412 KB
testcase_21 AC 277 ms
87,552 KB
testcase_22 AC 280 ms
87,424 KB
testcase_23 AC 422 ms
87,424 KB
testcase_24 AC 430 ms
87,400 KB
testcase_25 AC 434 ms
87,616 KB
testcase_26 AC 464 ms
87,512 KB
testcase_27 AC 432 ms
87,552 KB
testcase_28 AC 433 ms
87,552 KB
testcase_29 AC 418 ms
87,424 KB
testcase_30 AC 414 ms
87,424 KB
testcase_31 AC 404 ms
87,408 KB
testcase_32 AC 399 ms
87,552 KB
testcase_33 AC 401 ms
87,492 KB
testcase_34 AC 391 ms
87,424 KB
testcase_35 AC 324 ms
87,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#define rep(i,a,n) for(int i=a; i<n; i++)
using namespace std;
typedef long long int ll;
#define int long long int
constexpr int MOD = 1000000007;


template <typename T>
using Matrix = vector< vector<T> >;

template <typename T>
void init_mat(Matrix<T> &A, int h, int w) {
    A.resize(h, vector<T>(w, 0));
}

template <typename T>
Matrix<T> calc_mat(Matrix<T> A, Matrix<T> B) {
    Matrix<T> C(A.size(), vector<T>(B[0].size()));
    for(int i=0; i<A.size(); i++) {
        for(int k=0; k<B.size(); k++) {
            for(int j=0; j<B[0].size(); j++) {
                // C[i][j] += A[i][k] * B[k][j]; // modなし
                C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD; // modあり
            }
        }
    }
    return C;
}

template <typename T>
Matrix<T> mat_pow(Matrix<T> A, ll n) {
    Matrix<T> B(A.size(), vector<T>(A.size()));
    for(int i=0; i<A.size(); i++) B[i][i] = 1;
    while(n > 0) {
        if(n & 1) B = calc_mat(B, A);
        A = calc_mat(A, A);
        n >>= 1;
    }
    return B;
}

void printmat(int p);

static const ll S = 1 << 17;
struct SegTree {
    Matrix<ll> node[2*S], I;
    SegTree() {
        I = { {1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1} };
        rep(i,0,2*S) {
            node[i] = { {1, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1} };
        }
    }
    void change(int x, Matrix<ll> M) {
        int p = x + (S - 1);
        node[p] = M;
        while(p > 0) {
            int c1 = p, c2 = p + (p % 2 ? 1 : -1);
            if(c1 > c2) swap(c1, c2);
            p = (p - 1) / 2;
            node[p] = calc_mat(node[c2], node[c1]);
        }
    }
    Matrix<ll> query(int a, int b, int l=0, int r=S, int k=0) {
        if(b <= l || r <= a) return I;
        if(a <= l && r <= b) return node[k];
        int mid = (l + r) / 2;
        Matrix<ll> vl = query(a, b, l, mid, 2*k+1);
        Matrix<ll> vr = query(a, b, mid, r, 2*k+2);
        return calc_mat(vr, vl);
    }
};

int N, Q;
SegTree seg;

signed main() {
    cin >> N >> Q;
    rep(x,0,Q) {
        char q; cin >> q;
        if(q == 'x') {
            int i, v; cin >> i >> v;
            Matrix<ll> nmat = seg.node[S+i-1];
            nmat[0][2] = v;
            seg.change(i, nmat);
        }
        if(q == 'y') {
            int i, v; cin >> i >> v;
            Matrix<ll> nmat = seg.node[S+i-1];
            nmat[1][1] = v;
            nmat[2][1] = 2 * v;
            nmat[2][2] = (v * v) % MOD;
            seg.change(i, nmat);
        }
        if(q == 'a') {
            int i; cin >> i;
            Matrix<ll> qmat = seg.query(0, i);
            int ans = 0;
            rep(i,0,4) (ans += qmat[0][i]) %= MOD;
            cout << ans << endl;
        }
    }
    return 0;
}
0