結果

問題 No.510 二次漸化式
ユーザー mamekinmamekin
提出日時 2017-04-29 12:09:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,385 ms / 3,000 ms
コード長 4,261 bytes
コンパイル時間 1,447 ms
コンパイル使用メモリ 122,156 KB
実行使用メモリ 88,852 KB
最終ジャッジ日時 2023-10-11 20:25:19
合計ジャッジ時間 50,366 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 221 ms
4,348 KB
testcase_03 AC 219 ms
4,348 KB
testcase_04 AC 220 ms
4,348 KB
testcase_05 AC 220 ms
4,352 KB
testcase_06 AC 485 ms
13,880 KB
testcase_07 AC 485 ms
13,680 KB
testcase_08 AC 486 ms
13,624 KB
testcase_09 AC 494 ms
13,744 KB
testcase_10 AC 99 ms
4,348 KB
testcase_11 AC 100 ms
4,352 KB
testcase_12 AC 98 ms
4,348 KB
testcase_13 AC 99 ms
4,352 KB
testcase_14 AC 99 ms
4,348 KB
testcase_15 AC 98 ms
4,352 KB
testcase_16 AC 2,029 ms
88,832 KB
testcase_17 AC 2,035 ms
88,460 KB
testcase_18 AC 2,041 ms
88,844 KB
testcase_19 AC 2,037 ms
88,792 KB
testcase_20 AC 2,032 ms
88,400 KB
testcase_21 AC 2,028 ms
88,456 KB
testcase_22 AC 2,036 ms
88,792 KB
testcase_23 AC 2,265 ms
88,792 KB
testcase_24 AC 2,268 ms
88,460 KB
testcase_25 AC 2,259 ms
88,408 KB
testcase_26 AC 2,271 ms
88,460 KB
testcase_27 AC 2,272 ms
88,408 KB
testcase_28 AC 2,273 ms
88,844 KB
testcase_29 AC 2,268 ms
88,848 KB
testcase_30 AC 2,272 ms
88,784 KB
testcase_31 AC 2,385 ms
88,852 KB
testcase_32 AC 2,357 ms
88,408 KB
testcase_33 AC 2,363 ms
88,408 KB
testcase_34 AC 2,145 ms
88,464 KB
testcase_35 AC 2,122 ms
88,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

const int MOD = 1000000007;

// 行列の積
template <class T>
vector<vector<T> > matrixProduct(const vector<vector<T> >& x, const vector<vector<T> >& y)
{
    int a = x.size();
    int b = x[0].size();
    int c = y[0].size();
    vector<vector<T> > z(a, vector<T>(c, 0));
    for(int i=0; i<a; ++i){
        for(int j=0; j<c; ++j){
            for(int k=0; k<b; ++k){
                z[i][j] += x[i][k] * y[k][j];
                z[i][j] %= MOD;
            }
        }
    }
    return z;
}

// セグメント木
class SegmentTree
{
private:
    typedef vector<vector<long long> > T1;
    typedef vector<vector<long long> > T2;

    // データの初期値、以下の条件を満たすこと
    // uniteData(v, INIT_DATA) == v
    static const T1 INIT_DATA;

    // 前回の値がprevである要素に対して、
    // パラメータxを用いた更新処理を適用した後の計算結果を返す
    T1 updateData(T1 prev, T2 x){
        return x;
    }
    // 2つの区間の計算結果v1,v2に対して、
    // その2つの区間を統合した区間における計算結果を返す
    T1 uniteData(T1 v1, T1 v2){
        return matrixProduct(v2, v1);
    }

    int n;
    vector<T1> data;
    void updateTree(int a, int k, int l, int r, T2 x){
        if(a == l && a == r){
            data[k] = updateData(data[k], x);
        }
        else if(l <= a && a <= r){
            updateTree(a, k*2+1, l, (l+r)/2, x);
            updateTree(a, k*2+2, (l+r+1)/2, r, x);
            data[k] = uniteData(data[k*2+1], data[k*2+2]);
        }
    }
    T1 getValue(int a, int b, int k, int l, int r){
        if(a <= l && r <= b){
            return data[k];
        }
        else if(a <= r && l <= b){
            T1 v1 = getValue(a, b, k*2+1, l, (l+r)/2);
            T1 v2 = getValue(a, b, k*2+2, (l+r+1)/2, r);
            return uniteData(v1, v2);
        }
        else{
            return INIT_DATA;
        }
    }
public:
    SegmentTree(int n0){
        n = 1;
        while(n < n0)
            n *= 2;
        data.assign(2*n-1, INIT_DATA);
    }
    SegmentTree(const vector<T1>& v) : SegmentTree((int)v.size()){
        for(unsigned i=0; i<v.size(); ++i)
            data[n-1+i] = v[i];
        for(int k=n-2; k>=0; --k)
            data[k] = uniteData(data[k*2+1], data[k*2+2]);
    }
    // a番目の要素にパラメータxによる更新処理を適用
    void update(int a, T2 x){
        updateTree(a, 0, 0, n-1, x);
    }
    // 区間[a,b]の計算結果を返す
    T1 get(int a, int b){
        return getValue(a, b, 0, 0, n-1);
    }
};
const vector<vector<long long> > SegmentTree::INIT_DATA =
{
    { 1, 0, 0, 0 },
    { 0, 1, 0, 0 },
    { 0, 0, 1, 0 },
    { 0, 0, 0, 1 },
};

int main()
{
    int n, q;
    cin >> n >> q;

    vector<long long> x(n, 0);
    vector<long long> y(n, 0);
    SegmentTree st(n+1);

    vector<vector<long long> > init =
    {
        { 1, 0, 0, 0 },
        { 0, 0, 0, 1 },
        { 0, 0, 0, 1 },
        { 0, 0, 0, 1 },
    };
    for(int i=0; i<n; ++i)
        st.update(i, init);

    while(--q >= 0){
        char c;
        int i;
        cin >> c >> i;
        if(c == 'a'){
            vector<vector<long long> > mat = st.get(0, i);
            long long ans = accumulate(mat[0].begin(), mat[0].end(), 0LL);
            ans %= MOD;
            cout << ans << endl;
            continue;
        }

        int v;
        cin >> v;
        if(c == 'x')
            x[i] = v;
        else
            y[i] = v;

        vector<vector<long long> > mat =
        {
            { 1,          0,          x[i], 0 },
            { 0,       y[i],             0, 1 },
            { 0, 2*y[i]%MOD, y[i]*y[i]%MOD, 1 },
            { 0,          0,             0, 1 },
        };
        st.update(i+1, mat);
    }

    return 0;
}
0