結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 143 ms
87,552 KB
testcase_01 AC 145 ms
87,552 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 317 ms
87,424 KB
testcase_17 AC 316 ms
87,424 KB
testcase_18 AC 320 ms
87,424 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 318 ms
87,424 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 488 ms
87,424 KB
testcase_32 AC 495 ms
87,424 KB
testcase_33 WA -
testcase_34 AC 449 ms
87,552 KB
testcase_35 AC 365 ms
87,424 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 int S = 1 << 17;
struct SegTree {
    Matrix<int> 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<int> 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<int> 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<int> vl = query(a, b, l, mid, 2*k+1);
        Matrix<int> 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<int> nmat = seg.node[S+i-1];
            nmat[0][2] = v;
            seg.change(i, nmat);
        }
        if(q == 'y') {
            int i, v; cin >> i >> v;
            Matrix<int> nmat = seg.node[S+i-1];
            nmat[1][1] = v;
            nmat[2][1] = 2 * v;
            nmat[2][2] = v * v;
            seg.change(i, nmat);
        }
        if(q == 'a') {
            int i; cin >> i;
            Matrix<int> qmat = seg.query(0, i);
            int ans = 0;
            rep(i,0,4) (ans += qmat[0][i]);
            cout << ans << endl;
        }
    }
    return 0;
}
0