結果

問題 No.510 二次漸化式
ユーザー mamekinmamekin
提出日時 2017-04-29 12:30:26
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 630 ms / 3,000 ms
コード長 4,058 bytes
コンパイル時間 1,409 ms
コンパイル使用メモリ 120,152 KB
実行使用メモリ 88,848 KB
最終ジャッジ日時 2023-10-11 20:22:18
合計ジャッジ時間 15,451 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 209 ms
4,348 KB
testcase_03 AC 211 ms
4,348 KB
testcase_04 AC 209 ms
4,352 KB
testcase_05 AC 208 ms
4,348 KB
testcase_06 AC 344 ms
13,608 KB
testcase_07 AC 345 ms
13,716 KB
testcase_08 AC 345 ms
13,616 KB
testcase_09 AC 343 ms
13,736 KB
testcase_10 AC 100 ms
4,352 KB
testcase_11 AC 97 ms
4,348 KB
testcase_12 AC 98 ms
4,348 KB
testcase_13 AC 98 ms
4,352 KB
testcase_14 AC 98 ms
4,352 KB
testcase_15 AC 98 ms
4,348 KB
testcase_16 AC 334 ms
88,416 KB
testcase_17 AC 330 ms
88,456 KB
testcase_18 AC 332 ms
88,452 KB
testcase_19 AC 331 ms
88,416 KB
testcase_20 AC 336 ms
88,476 KB
testcase_21 AC 333 ms
88,824 KB
testcase_22 AC 334 ms
88,456 KB
testcase_23 AC 555 ms
88,472 KB
testcase_24 AC 563 ms
88,472 KB
testcase_25 AC 559 ms
88,760 KB
testcase_26 AC 566 ms
88,808 KB
testcase_27 AC 559 ms
88,756 KB
testcase_28 AC 548 ms
88,836 KB
testcase_29 AC 550 ms
88,420 KB
testcase_30 AC 550 ms
88,416 KB
testcase_31 AC 624 ms
88,760 KB
testcase_32 AC 619 ms
88,724 KB
testcase_33 AC 630 ms
88,472 KB
testcase_34 AC 451 ms
88,472 KB
testcase_35 AC 428 ms
88,848 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, 0, 0, 1 },
    { 0, 0, 0, 1 },
    { 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);

    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