結果

問題 No.510 二次漸化式
ユーザー tottoripapertottoripaper
提出日時 2017-05-05 22:01:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 563 ms / 3,000 ms
コード長 3,997 bytes
コンパイル時間 3,964 ms
コンパイル使用メモリ 181,996 KB
実行使用メモリ 92,288 KB
最終ジャッジ日時 2023-10-12 09:54:04
合計ジャッジ時間 20,795 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 142 ms
90,616 KB
testcase_01 AC 142 ms
90,672 KB
testcase_02 AC 372 ms
90,684 KB
testcase_03 AC 369 ms
90,692 KB
testcase_04 AC 375 ms
90,624 KB
testcase_05 AC 374 ms
90,636 KB
testcase_06 AC 422 ms
90,832 KB
testcase_07 AC 416 ms
90,840 KB
testcase_08 AC 415 ms
90,824 KB
testcase_09 AC 418 ms
90,772 KB
testcase_10 AC 358 ms
90,672 KB
testcase_11 AC 359 ms
90,788 KB
testcase_12 AC 362 ms
90,612 KB
testcase_13 AC 364 ms
90,672 KB
testcase_14 AC 363 ms
90,916 KB
testcase_15 AC 361 ms
90,668 KB
testcase_16 AC 321 ms
92,172 KB
testcase_17 AC 317 ms
92,220 KB
testcase_18 AC 321 ms
92,172 KB
testcase_19 AC 316 ms
92,176 KB
testcase_20 AC 322 ms
92,176 KB
testcase_21 AC 321 ms
92,164 KB
testcase_22 AC 321 ms
92,168 KB
testcase_23 AC 491 ms
92,240 KB
testcase_24 AC 497 ms
92,288 KB
testcase_25 AC 501 ms
92,164 KB
testcase_26 AC 486 ms
92,168 KB
testcase_27 AC 488 ms
92,160 KB
testcase_28 AC 492 ms
92,160 KB
testcase_29 AC 493 ms
92,216 KB
testcase_30 AC 493 ms
92,224 KB
testcase_31 AC 556 ms
91,520 KB
testcase_32 AC 559 ms
91,388 KB
testcase_33 AC 563 ms
92,160 KB
testcase_34 AC 377 ms
90,672 KB
testcase_35 AC 310 ms
90,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)

using ll = long long;
using P = std::tuple<int,int>;

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};

const ll MOD = 1e9 + 7;

template <typename T>
using Matrix = std::vector<std::vector<T>>;

namespace tottori{
    template <typename T>
    Matrix<T> mult(const Matrix<T>& m2, const Matrix<T>& m1){
        assert(m1.size() > 0);
        assert(m2.size() > 0);
        assert(m1[0].size() == m2.size());
        int n = m1.size(), m = m1[0].size(), l = m2[0].size();
        
        Matrix<T> res(n);
        for(int i=0;i<n;++i){
            res[i].resize(l);

            for(int j=0;j<l;++j){
                for(int k=0;k<m;++k){
                    res[i][j] = (res[i][j] + m1[i][k] * m2[k][j]) % MOD;
                }
            }
        }

        return res;
    }
};

// I want to delete this stupid solution ;(
#define sandwich(x) [](auto a, auto b){return (x)(a, b);}

template <typename T>
using F2 = std::function<T(T,T)>;

template <typename T, int N, typename F = std::plus<T>>
    struct SegmentTree{
        SegmentTree(T u, F f = F()){
            size = 1;
            while(size < N){
                size <<= 1;
            }

            unit = u;
            func = f;

            init();
        }
        void init(){
            for(int i=1;i<=2*size-1;++i){
                data[i] = {{1, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}};
            }
        }
        void update(int k, T v){
            k += size;
            data[k] = v;

            while(k > 1){
                k >>= 1;
                data[k] = func(data[k*2], data[k*2+1]);
            }
        }
        inline T query(int l, int r){
            return _query(l, r, 1, 0, size);
        }
        T _query(int a, int b, int k, int l, int r){
            if(b <= l || r <= a){return unit;}
            if(a <= l && r <= b){return data[k];}
            int mid = (l + r) >> 1;
            return func(_query(a, b,   2*k,   l, mid),
                        _query(a, b, 2*k+1, mid, r  ));
        }

        T unit, data[N*4];
        int size;
        F func;
    };

SegmentTree<Matrix<ll>, 100100, F2<Matrix<ll>>> segtree({{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}, sandwich(tottori::mult));
ll xs[100100], ys[100100];

void update(int i){
    Matrix<ll> mat = {{1, 0, xs[i], 0}, {0, ys[i], 0, 1}, {0, ys[i] * 2 % MOD, ys[i] * ys[i] % MOD, 1}, {0, 0, 0, 1}};
    segtree.update(i, mat);
}

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

    int N;
    std::cin >> N;
    
    int Q;
    std::cin >> Q;

    for(int i=0;i<Q;++i){
        char c;
        std::cin >> c;

        if(c == 'x'){
            int i, v;
            std::cin >> i >> v;

            xs[i] = v;
            update(i);
        }else if(c == 'y'){
            int i, v;
            std::cin >> i >> v;

            ys[i] = v;
            update(i);
        }else{
            int i;
            std::cin >> i;

            int ai = 0;
            if(i == 0){
                ai = 1;
            }else{
                Matrix<ll> coefficientMatrix = segtree.query(0, i);

                // auto f = [](const Matrix<ll>& mat){
                //         int n = mat.size(), m = mat[0].size();

                //         for(int i=0;i<n;++i){
                //             for(int j=0;j<m;++j){
                //                 std::cout << mat[i][j] << " \n"[j+1==m];
                //             }
                //         }
                //         std::cout << "------------------------------------------------------------" << std::endl;
                //     };
                
                for(int i=0;i<4;++i){
                    ai = (ai + coefficientMatrix[0][i]) % MOD;
                }
            }

            std::cout << ai << std::endl;
        }
    }
}
0