結果

問題 No.510 二次漸化式
ユーザー parukiparuki
提出日時 2017-04-29 09:59:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,317 ms / 3,000 ms
コード長 2,884 bytes
コンパイル時間 2,001 ms
コンパイル使用メモリ 180,876 KB
実行使用メモリ 96,528 KB
最終ジャッジ日時 2023-10-11 20:20:21
合計ジャッジ時間 51,180 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,796 KB
testcase_01 AC 3 ms
5,716 KB
testcase_02 AC 245 ms
6,476 KB
testcase_03 AC 246 ms
6,600 KB
testcase_04 AC 243 ms
6,688 KB
testcase_05 AC 242 ms
6,596 KB
testcase_06 AC 509 ms
16,264 KB
testcase_07 AC 509 ms
16,248 KB
testcase_08 AC 510 ms
16,452 KB
testcase_09 AC 508 ms
16,296 KB
testcase_10 AC 100 ms
5,764 KB
testcase_11 AC 99 ms
5,740 KB
testcase_12 AC 100 ms
5,740 KB
testcase_13 AC 100 ms
5,756 KB
testcase_14 AC 100 ms
5,784 KB
testcase_15 AC 100 ms
5,764 KB
testcase_16 AC 2,065 ms
96,348 KB
testcase_17 AC 2,029 ms
96,368 KB
testcase_18 AC 2,036 ms
96,424 KB
testcase_19 AC 2,049 ms
96,328 KB
testcase_20 AC 2,040 ms
96,368 KB
testcase_21 AC 2,030 ms
96,388 KB
testcase_22 AC 2,034 ms
96,432 KB
testcase_23 AC 2,274 ms
96,344 KB
testcase_24 AC 2,277 ms
96,332 KB
testcase_25 AC 2,276 ms
96,332 KB
testcase_26 AC 2,273 ms
96,528 KB
testcase_27 AC 2,279 ms
96,372 KB
testcase_28 AC 2,269 ms
96,372 KB
testcase_29 AC 2,274 ms
96,376 KB
testcase_30 AC 2,279 ms
96,524 KB
testcase_31 AC 2,317 ms
96,416 KB
testcase_32 AC 2,292 ms
96,436 KB
testcase_33 AC 2,284 ms
96,388 KB
testcase_34 AC 2,224 ms
96,372 KB
testcase_35 AC 2,106 ms
96,368 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(vector<vector<int> > A, 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;

template<class Value>
class SegmentTree {
    int n;
    Value init;
    vector<Value> dat;

    Value cal(Value a, Value b) {
        return mulMatMod(b, a);
    }

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

public:
    SegmentTree() {}

    SegmentTree(int n_, Value init_) :n(1), init(init_) {
        for (; n<n_; n <<= 1);
        dat = vector<Value>(n << 1, init);
    }

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

    Value query(int a, int b) {
        return query(a, b, 1, 0, n);
    }
};
typedef SegmentTree<mat> SegTree;

int n, q;
mat A[100001];

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

    cin >> n >> q;
    mat E(4, vi(4));
    rep(i, 4)E[i][i] = 1;
    SegTree st(n + 1, E);

    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