結果

問題 No.510 二次漸化式
ユーザー parukiparuki
提出日時 2017-05-02 16:24:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,087 ms / 3,000 ms
コード長 3,018 bytes
コンパイル時間 2,015 ms
コンパイル使用メモリ 180,464 KB
実行使用メモリ 96,436 KB
最終ジャッジ日時 2023-10-12 05:12:16
合計ジャッジ時間 26,869 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,892 KB
testcase_01 AC 4 ms
5,788 KB
testcase_02 AC 122 ms
6,652 KB
testcase_03 AC 121 ms
6,484 KB
testcase_04 AC 121 ms
6,484 KB
testcase_05 AC 121 ms
6,536 KB
testcase_06 AC 256 ms
16,220 KB
testcase_07 AC 256 ms
16,192 KB
testcase_08 AC 259 ms
16,256 KB
testcase_09 AC 266 ms
16,252 KB
testcase_10 AC 57 ms
5,776 KB
testcase_11 AC 57 ms
5,732 KB
testcase_12 AC 57 ms
5,732 KB
testcase_13 AC 57 ms
5,792 KB
testcase_14 AC 58 ms
5,784 KB
testcase_15 AC 57 ms
5,728 KB
testcase_16 AC 931 ms
96,304 KB
testcase_17 AC 930 ms
96,316 KB
testcase_18 AC 934 ms
96,344 KB
testcase_19 AC 930 ms
96,420 KB
testcase_20 AC 932 ms
96,336 KB
testcase_21 AC 929 ms
96,416 KB
testcase_22 AC 930 ms
96,348 KB
testcase_23 AC 1,087 ms
96,320 KB
testcase_24 AC 1,069 ms
96,328 KB
testcase_25 AC 1,068 ms
96,412 KB
testcase_26 AC 1,064 ms
96,356 KB
testcase_27 AC 1,072 ms
96,372 KB
testcase_28 AC 1,065 ms
96,412 KB
testcase_29 AC 1,062 ms
96,408 KB
testcase_30 AC 1,067 ms
96,404 KB
testcase_31 AC 1,045 ms
96,436 KB
testcase_32 AC 1,051 ms
96,348 KB
testcase_33 AC 1,047 ms
96,416 KB
testcase_34 AC 1,025 ms
96,368 KB
testcase_35 AC 960 ms
96,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define mt make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

const int MOD = (int)1e9 + 7;

vector<vector<int> > mulMatMod(const vector<vector<int> > &A, const vector<vector<int> > &B) {
    auto n = A.size(), m = A[0].size(), p = B[0].size();
    vector<vector<int> > res(n, vector<int>(p));
    for (int i = 0; i < n; ++i)for (int j = 0; j < p; ++j) for (int k = 0; k < m; ++k)
        res[i][j] = (int)((res[i][j] + (long long)A[i][k] * B[k][j]) % MOD);
    return res;
}

typedef vector<vi> mat;

/*
行列の部分積
A[b-1]A[b-2]...A[a]
を求める。
*/
class PartialMatMul {
    int n;
    vector<vector<int> > E;
    vector<vector<vector<int> >> dat;

    inline vector<vector<int> > query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return E;
        if (a <= l&&r <= b) return dat[k];
        else {
            vector<vector<int> > vl, vr;
            vl = query(a, b, k << 1, l, (l + r) >> 1);
            vr = query(a, b, (k << 1) | 1, (l + r) >> 1, r);
            return mulMatMod(vr, vl);
        }
    }

public:
    PartialMatMul() {}

    PartialMatMul(int segSize, int matSize) :n(1) {
        for (; n<segSize; n <<= 1);
        E = vector<vector<int> >(matSize, vector<int>(matSize));
        for (int i = 0; i < matSize; ++i)E[i][i] = 1;
        dat = vector<vector<vector<int> >>(n << 1, E);
    }

    void update(int k, vector<vector<int> > a) {
        k += n;
        dat[k] = a;
        while (k>1) {
            k >>= 1;
            dat[k] = mulMatMod(dat[(k << 1) | 1], dat[k << 1]);
        }
    }

    vector<vector<int> > query(int a, int b) {
        return query(a, b, 1, 0, n);
    }
};

int n, q;
mat A[100001];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> q;
    PartialMatMul st(n + 1, 4);

    rep(i, n) {
        A[i] = mat(4, vi(4));
        auto &B = A[i];
        B[0][0] = B[1][3] = B[2][3] = B[3][3] = 1;
        st.update(i, B);
    }
    while (q--) {
        string c; cin >> c;
        int k;
        cin >> k;
        auto &B = A[k];
        if (c == "x") {
            int v; cin >> v;
            B[0][2] = v;
            st.update(k, B);
        }
        else if (c == "y") {
            int v; cin >> v;
            B[1][1] = v;
            B[2][1] = v * 2;
            B[2][2] = (int)((ll)v*v % MOD);
            st.update(k, B);
        }
        else {
            auto C = st.query(0, k);
            ll ans = 0;
            rep(i, 4)ans += C[0][i];
            cout << ans%MOD << endl;
        }
    }
}
0