結果

問題 No.510 二次漸化式
ユーザー vjudge1
提出日時 2025-02-23 16:49:58
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 163 ms / 3,000 ms
コード長 3,072 bytes
コンパイル時間 2,091 ms
コンパイル使用メモリ 176,620 KB
実行使用メモリ 56,796 KB
最終ジャッジ日時 2025-02-23 16:50:06
合計ジャッジ時間 7,800 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
int n, q;
struct Matrix
{
    ll a[4][4];
    Matrix(bool flag = 0)
    {
        memset(a, 0, sizeof(a));
        if (flag)
            for (ll i = 0; i < 4; i++)
                a[i][i] = 1;
    }
    inline void init(ll x, ll y)
    {
        a[0][0] = 1, a[0][1] = 0, a[0][2] = 0, a[0][3] = 0;
        a[1][0] = 0, a[1][1] = y, a[1][2] = 2 * y % mod, a[1][3] = 0;
        a[2][0] = x, a[2][1] = 0, a[2][2] = y * y % mod, a[2][3] = 0;
        a[3][0] = 0, a[3][1] = 1, a[3][2] = 1, a[3][3] = 1;
    }
    inline Matrix operator*(const Matrix &b)
    {
        Matrix ans;
        for (ll i = 0; i < 4; i++)
            for (ll j = 0; j < 4; j++)
                for (ll k = 0; k < 4; k++)
                    (ans.a[i][j] += a[i][k] * b.a[k][j] % mod) %= mod;
        return ans;
    }
} I(1);
struct Segment_Tree
{
    struct Node
    {
        int l, r;
        Matrix sum;
    } tr[N << 2];
    inline void build(int x, int l, int r)
    {
        tr[x] = {l, r};
        if (l == r)
        {
            tr[x].sum.init(0, 0);
            return;
        }
        int ls = x << 1, rs = x << 1 | 1, mid = (l + r) >> 1;
        build(ls, l, mid), build(rs, mid + 1, r);
        tr[x].sum = tr[ls].sum * tr[rs].sum;
    }
    inline void update(int x, int pos, ll k, bool flag)
    {
        if (tr[x].l == tr[x].r)
        {
            if (flag)
                tr[x].sum.a[1][1] = k, tr[x].sum.a[1][2] = 2 * k % mod, tr[x].sum.a[2][2] = k * k % mod;
            else
                tr[x].sum.a[2][0] = k;
            return;
        }
        int ls = x << 1, rs = x << 1 | 1;
        if (pos <= tr[ls].r)
            update(ls, pos, k, flag);
        else
            update(rs, pos, k, flag);
        tr[x].sum = tr[ls].sum * tr[rs].sum;
    }
    inline Matrix query(int x, int l, int r)
    {
        if (tr[x].r < l || r < tr[x].l)
            return I;
        if (l <= tr[x].l && tr[x].r <= r)
            return tr[x].sum;
        int ls = x << 1, rs = x << 1 | 1;
        return query(ls, l, r) * query(rs, l, r);
    }
} t;
int main(int argc, char const *argv[])
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    cin >> n >> q;
    t.build(1, 1, n);
    for (int i = 1; i <= q; i++)
    {
        char opt;
        ll pos, k;
        cin >> opt;
        if (opt == 'x')
        {
            cin >> pos >> k;
            pos++;
            t.update(1, pos, k, 0);
        }
        else if (opt == 'y')
        {
            cin >> pos >> k;
            pos++;
            t.update(1, pos, k, 1);
        }
        else
        {
            cin >> pos;
            if (pos == 0)
                cout << 1 << endl;
            else
            {
                Matrix ans;
                ans.a[0][0] = ans.a[0][1] = ans.a[0][2] = ans.a[0][3] = 1;
                ans = ans * t.query(1, 1, pos);
                cout << ans.a[0][0] << endl;
            }
        }
    }
    return 0;
}
0