結果

問題 No.510 二次漸化式
ユーザー milanis48663220milanis48663220
提出日時 2020-06-21 14:45:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 522 ms / 3,000 ms
コード長 3,423 bytes
コンパイル時間 880 ms
コンパイル使用メモリ 90,188 KB
実行使用メモリ 36,884 KB
最終ジャッジ日時 2023-09-16 18:22:39
合計ジャッジ時間 13,638 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 67 ms
4,376 KB
testcase_03 AC 66 ms
4,376 KB
testcase_04 AC 66 ms
4,376 KB
testcase_05 AC 65 ms
4,376 KB
testcase_06 AC 118 ms
7,200 KB
testcase_07 AC 120 ms
7,260 KB
testcase_08 AC 124 ms
7,360 KB
testcase_09 AC 120 ms
7,236 KB
testcase_10 AC 33 ms
4,376 KB
testcase_11 AC 34 ms
4,380 KB
testcase_12 AC 33 ms
4,376 KB
testcase_13 AC 34 ms
4,380 KB
testcase_14 AC 34 ms
4,380 KB
testcase_15 AC 33 ms
4,376 KB
testcase_16 AC 467 ms
36,524 KB
testcase_17 AC 452 ms
36,852 KB
testcase_18 AC 460 ms
36,540 KB
testcase_19 AC 466 ms
36,804 KB
testcase_20 AC 465 ms
36,884 KB
testcase_21 AC 467 ms
36,804 KB
testcase_22 AC 466 ms
36,740 KB
testcase_23 AC 522 ms
36,812 KB
testcase_24 AC 511 ms
36,548 KB
testcase_25 AC 505 ms
36,760 KB
testcase_26 AC 502 ms
36,884 KB
testcase_27 AC 509 ms
36,880 KB
testcase_28 AC 512 ms
36,872 KB
testcase_29 AC 513 ms
36,860 KB
testcase_30 AC 518 ms
36,808 KB
testcase_31 AC 509 ms
36,212 KB
testcase_32 AC 509 ms
36,344 KB
testcase_33 AC 511 ms
36,516 KB
testcase_34 AC 520 ms
35,828 KB
testcase_35 AC 500 ms
35,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;

const ll MOD = 1000000007;

struct node{
    ll cnt[4][4];
};

void print(node m){
    for(int j = 0; j < 4; j++){
        for(int k = 0; k < 4; k++){
            cout << m.cnt[j][k] << ' ';
        }
        cout << endl;
    }
}

node calc_node(node a, node b){
    node ans;
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 4; j++){
            ans.cnt[i][j] = 0;
            for(int k = 0; k < 4; k++){
                ans.cnt[i][j] += (b.cnt[i][k]*a.cnt[k][j])%MOD;
                ans.cnt[i][j] %=MOD;
            }
        }
    }
    return ans;
}

template <typename T>
struct segtree{
    int n;
    T UNIT;
    vector<T> dat;
    segtree(int n_, T unit){
        UNIT = unit;
        n = 1;
        while(n < n_) n *= 2;
        dat = vector<T>(2*n);
        for(int i = 0; i < 2*n; i++) dat[i] = UNIT;
    }

    T calc(T a, T b){
        T ans;
        ans = calc_node(a, b);
        return ans;
    }
    void insert(int k, T a){
        dat[k+n-1] = a;
    }
    void update_all(){
        for(int i = n-2; i >= 0; i--){
            dat[i] = calc(dat[i*2+1], dat[i*2+2]);
        }
    }
    //k番目の値(0-indexed)をaに変更
    void update(int k, T a){
        k += n-1;
        dat[k] = a;
        while(k > 0){
            k = (k-1)/2;
            dat[k] = calc(dat[k*2+1], dat[k*2+2]);
        }
    }

    //[a, b)
    //区間[a, b]へのクエリに対してはquery(a, b+1)と呼ぶ
    T query(int a, int b, int k=0, int l=0, int r=-1){
        if(r < 0) r = n;
        if(r <= a || b <= l) return UNIT;
        if(a <= l && r <= b) return dat[k];
        else{
            T vl = query(a, b, k*2+1, l, (l+r)/2);
            T vr = query(a, b, k*2+2, (l+r)/2, r);
            return calc(vl, vr);
        }
    }
};
int n, q;
int x[200000], y[200000];

node create_node(ll x, ll y){
    node mat;
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 4; j++){
            mat.cnt[i][j] = 0;
        }
    }
    mat.cnt[0][0] = 1; mat.cnt[0][1] = x;
    mat.cnt[1][1] = (y*y)%MOD; mat.cnt[1][2] = 2*y; mat.cnt[1][3] = 1;
    mat.cnt[2][2] = y; mat.cnt[2][3] = 1;
    mat.cnt[3][3] = 1;
    return mat;
}


node create_unit(){
    node mat;
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 4; j++){
            mat.cnt[i][j] = 0;
        }
    }
    mat.cnt[0][0] = 1; mat.cnt[1][1] = 1;
    mat.cnt[2][2] = 1; mat.cnt[3][3] = 1;
    return mat;
}



int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> n >> q;
    node unit_node = create_unit();    
    segtree<node> sgt(n, unit_node); 
    node node_ = create_node(0, 0);
    for(int i = 0; i < n; i++){
        sgt.update(i, node_);
    }

    for(int t = 0; t < q; t++){
        char c; cin >> c;
        if(c == 'x'){
            int i, v; cin >> i >> v;
            x[i] = v;
            sgt.update(i, create_node(x[i], y[i]));
        }
        if(c == 'y'){
            int i, v; cin >> i >> v;
            y[i] = v;
            sgt.update(i, create_node(x[i], y[i]));
        }
        if(c == 'a'){
            int i;
            cin >> i; 
            auto m = sgt.query(0, i);
            cout << (m.cnt[0][0]+m.cnt[0][1]+m.cnt[0][2]+m.cnt[0][3])%MOD << endl;
        }
    }
}
0