結果

問題 No.510 二次漸化式
ユーザー mamekinmamekin
提出日時 2017-04-28 23:31:24
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 103 ms / 3,000 ms
コード長 4,761 bytes
コンパイル時間 1,044 ms
コンパイル使用メモリ 106,808 KB
実行使用メモリ 8,896 KB
最終ジャッジ日時 2023-10-11 19:46:50
合計ジャッジ時間 4,129 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 71 ms
4,352 KB
testcase_03 AC 92 ms
4,352 KB
testcase_04 AC 103 ms
4,352 KB
testcase_05 AC 79 ms
4,356 KB
testcase_06 AC 38 ms
4,352 KB
testcase_07 AC 39 ms
4,348 KB
testcase_08 AC 39 ms
4,372 KB
testcase_09 AC 39 ms
4,348 KB
testcase_10 AC 27 ms
4,348 KB
testcase_11 AC 27 ms
4,352 KB
testcase_12 AC 28 ms
4,348 KB
testcase_13 AC 28 ms
4,352 KB
testcase_14 AC 28 ms
4,352 KB
testcase_15 AC 27 ms
4,352 KB
testcase_16 AC 28 ms
8,572 KB
testcase_17 AC 28 ms
8,568 KB
testcase_18 AC 28 ms
8,636 KB
testcase_19 AC 28 ms
8,524 KB
testcase_20 AC 28 ms
8,640 KB
testcase_21 AC 28 ms
8,536 KB
testcase_22 AC 28 ms
8,636 KB
testcase_23 AC 50 ms
8,520 KB
testcase_24 AC 50 ms
8,564 KB
testcase_25 AC 50 ms
8,720 KB
testcase_26 AC 50 ms
8,632 KB
testcase_27 AC 50 ms
8,568 KB
testcase_28 AC 49 ms
8,696 KB
testcase_29 AC 49 ms
8,536 KB
testcase_30 AC 49 ms
8,888 KB
testcase_31 AC 39 ms
8,528 KB
testcase_32 AC 39 ms
8,704 KB
testcase_33 AC 38 ms
8,584 KB
testcase_34 AC 58 ms
8,896 KB
testcase_35 AC 51 ms
8,892 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;

// セグメント木(遅延評価あり)
class SegmentTree
{
private:
    typedef long long T1;
    typedef long long T2;

    // データの初期値、以下の条件を満たすこと
    // uniteData(v, INIT_DATA) == v
    static const T1 INIT_DATA;
    // 遅延の初期値、以下の条件を満たすこと
    // updateData(prev, size, INIT_DELAY) == prev
    // updateDelay(x, INIT_DELAY) == x
    static const T2 INIT_DELAY;

    // 長さがsizeの区間における前回の計算結果prevに対して、
    // 区間の各要素にパラメータxを用いた更新処理を適用した後の計算結果を返す
    T1 updateData(T1 prev, int size, T2 x){
        return (prev + x * size) % MOD;
    }
    // パラメータx1,x2による2つの更新処理を順に適用した場合に対して、
    // 同じ結果になるような1つの更新処理のパラメータを返す
    T2 updateDelay(T2 x1, T2 x2){
        return (x1 + x2) % MOD;
    }
    // 2つの区間の計算結果v1,v2に対して、
    // その2つの区間を統合した区間における計算結果を返す
    T1 uniteData(T1 v1, T1 v2){
        return (v1 + v2) % MOD;
    }

    int n;
    vector<T1> data;
    vector<T2> delay;
    void updateTree(int a, int b, int k, int l, int r, T2 x){
        if(a <= l && r <= b){
            data[k] = updateData(data[k], r - l + 1, x);
            delay[k] = updateDelay(delay[k], x);
        }
        else if(a <= r && l <= b){
            for(int i=0; i<2; ++i){
                data[k*2+1+i] = updateData(data[k*2+1+i], (r - l + 1) / 2, delay[k]);
                delay[k*2+1+i] = updateDelay(delay[k*2+1+i], delay[k]);
            }
            delay[k] = INIT_DELAY;
            updateTree(a, b, k*2+1, l, (l+r)/2, x);
            updateTree(a, b, 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);
        delay.assign(2*n-1, INIT_DELAY);
    }
    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,b]の要素にパラメータxによる更新処理を適用
    void update(int a, int b, T2 x){
        updateTree(a, b, 0, 0, n-1, x);
    }
    // 区間[a,b]の計算結果を返す
    T1 get(int a, int b){
        updateTree(a, b, 0, 0, n-1, 0);
        return getValue(a, b, 0, 0, n-1);
    }
};
const long long SegmentTree::INIT_DATA  = 0;
const long long SegmentTree::INIT_DELAY = 0;

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

    SegmentTree a(n+1);
    vector<long long> b(n+2, 1);
    vector<int> x(n+1, 0);
    vector<int> y(n+1, 0);
    a.update(0, n, 1);

    while(--q >= 0){
        char c;
        int i;
        cin >> c >> i;
        if(c == 'a'){
            cout << a.get(i, i) << endl;
            continue;
        }

        int v;
        cin >> v;
        if(c == 'x'){
            long long a2 = x[i] * b[i] % MOD * b[i] % MOD;
            a2 = (MOD - a2) % MOD;
            x[i] = v;
            a2 += x[i] * b[i] % MOD * b[i] % MOD;
            a2 %= MOD;
            a.update(i+1, n, a2);
        }
        else{
            y[i] = v;
            for(int j=i; ; ++j){
                long long b2 = (y[j] * b[j] + 1) % MOD;
                if(b[j+1] == b2)
                    break;

                long long a2 = x[j+1] * b[j+1] % MOD * b[j+1] % MOD;
                a2 = (MOD - a2) % MOD;
                b[j+1] = b2;
                a2 += x[j+1] * b[j+1] % MOD * b[j+1] % MOD;
                a2 %= MOD;
                if(j+2 <= n)
                    a.update(j+2, n, a2);
            }
        }
    }

    return 0;
}
0