結果

問題 No.510 二次漸化式
ユーザー hiyokko2hiyokko2
提出日時 2017-09-08 10:14:39
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 444 ms / 3,000 ms
コード長 2,274 bytes
コンパイル時間 1,579 ms
コンパイル使用メモリ 168,176 KB
実行使用メモリ 87,168 KB
最終ジャッジ日時 2024-04-24 19:54:06
合計ジャッジ時間 12,635 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 142 ms
5,376 KB
testcase_03 AC 137 ms
5,376 KB
testcase_04 AC 139 ms
5,376 KB
testcase_05 AC 138 ms
5,376 KB
testcase_06 AC 212 ms
13,568 KB
testcase_07 AC 216 ms
13,696 KB
testcase_08 AC 222 ms
13,568 KB
testcase_09 AC 221 ms
13,696 KB
testcase_10 AC 63 ms
5,376 KB
testcase_11 AC 62 ms
5,376 KB
testcase_12 AC 65 ms
5,376 KB
testcase_13 AC 65 ms
5,376 KB
testcase_14 AC 64 ms
5,376 KB
testcase_15 AC 64 ms
5,376 KB
testcase_16 AC 252 ms
87,040 KB
testcase_17 AC 255 ms
87,040 KB
testcase_18 AC 261 ms
87,168 KB
testcase_19 AC 264 ms
87,040 KB
testcase_20 AC 262 ms
87,040 KB
testcase_21 AC 259 ms
87,040 KB
testcase_22 AC 257 ms
87,168 KB
testcase_23 AC 410 ms
87,040 KB
testcase_24 AC 421 ms
87,168 KB
testcase_25 AC 421 ms
87,168 KB
testcase_26 AC 419 ms
87,168 KB
testcase_27 AC 420 ms
87,168 KB
testcase_28 AC 408 ms
87,040 KB
testcase_29 AC 409 ms
87,040 KB
testcase_30 AC 417 ms
87,168 KB
testcase_31 AC 433 ms
87,040 KB
testcase_32 AC 444 ms
87,040 KB
testcase_33 AC 432 ms
87,168 KB
testcase_34 AC 323 ms
87,168 KB
testcase_35 AC 269 ms
87,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define FOR(i,bg,ed) for(ll i=(bg);i<(ed);i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) for(ll i=1;i<=(n);i++)
#define MOD 1000000007
#define int long long
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 1e9;

//行列Aと行列Bの積を返す
mat f(mat A, mat B)
{
    mat C(A.size(), vec(B[0].size()));
    REP(i,A.size()) REP(j,B[0].size()) {
        REP(k,A[0].size()) {
            C[i][j] += (A[i][k] * B[k][j]) % MOD;
            C[i][j] %= MOD;
        }
    }
    return C;
}

mat defvalue(4, vec(4)), defvalue2(4, vec(4));

class SegTree
{
private:
    int n;
    vector<mat> val;

public:
    SegTree(int size)
    {
        n = 1;
        while (n < size) n <<= 1;
        val = vector<mat>(2 * n, defvalue);
    }

    void update(int q, int i, int a)
    {
        i += n - 1;
        if (q == 0) {
            val[i][0][2] = a;
        } else {
            val[i][1][1] = a;
            val[i][2][1] = 2 * a;
            val[i][2][2] = a * a % MOD;
        }
        while (i > 0) {
            i = (i - 1) / 2;
            val[i] = f(val[i * 2 + 2],val[i * 2 + 1]);
        }
    }

    mat query(int a, int b, int k = 0, int l = 0, int r = - 1)
    {
        if (r == -1) r = n;
        if (r <= a || b <= l) return defvalue2;
        if (a <= l && r <= b) return val[k];
        else return f(query(a, b, k * 2 + 2, (l + r) / 2, r),
                      query(a, b, k * 2 + 1, l, (l + r) / 2));
    }
};

int n, q;

signed main()
{
    cin >> n >> q;

    defvalue[0][0] = defvalue[1][3] = defvalue[2][3] = defvalue[3][3] = 1;
    defvalue2[0][0] = defvalue2[1][1] = defvalue2[2][2] = defvalue2[3][3] = 1;

    SegTree st(n + 1);
    while (q--) {
        int a, b;
        char c;

        cin >> c;
        if (c == 'x') {
            cin >> a >> b;
            st.update(0, a, b);
        } else if (c == 'y') {
            cin >> a >> b;
            st.update(1, a, b);
        } else if (c == 'a') {
            cin >> a;
            mat t = st.query(0, a);
            cout << (t[0][0]+t[0][1]+t[0][2]+t[0][3]) % MOD << endl;
        }
    }
}
0