結果

問題 No.510 二次漸化式
ユーザー nebukuro09nebukuro09
提出日時 2018-05-29 13:19:51
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 28 ms / 3,000 ms
コード長 2,082 bytes
コンパイル時間 693 ms
コンパイル使用メモリ 104,688 KB
実行使用メモリ 10,592 KB
最終ジャッジ日時 2023-09-03 20:15:20
合計ジャッジ時間 3,089 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 22 ms
4,376 KB
testcase_03 AC 23 ms
4,380 KB
testcase_04 AC 25 ms
4,380 KB
testcase_05 AC 23 ms
4,380 KB
testcase_06 AC 21 ms
4,380 KB
testcase_07 AC 21 ms
4,380 KB
testcase_08 AC 21 ms
4,376 KB
testcase_09 AC 21 ms
4,376 KB
testcase_10 AC 18 ms
4,376 KB
testcase_11 AC 17 ms
4,376 KB
testcase_12 AC 17 ms
4,376 KB
testcase_13 AC 17 ms
4,376 KB
testcase_14 AC 17 ms
4,380 KB
testcase_15 AC 18 ms
4,376 KB
testcase_16 AC 15 ms
9,012 KB
testcase_17 AC 16 ms
10,288 KB
testcase_18 AC 16 ms
9,240 KB
testcase_19 AC 15 ms
9,560 KB
testcase_20 AC 15 ms
9,276 KB
testcase_21 AC 15 ms
9,044 KB
testcase_22 AC 15 ms
9,084 KB
testcase_23 AC 25 ms
9,016 KB
testcase_24 AC 25 ms
9,060 KB
testcase_25 AC 26 ms
9,744 KB
testcase_26 AC 25 ms
8,752 KB
testcase_27 AC 25 ms
9,284 KB
testcase_28 AC 25 ms
8,716 KB
testcase_29 AC 26 ms
10,088 KB
testcase_30 AC 26 ms
9,772 KB
testcase_31 AC 21 ms
9,296 KB
testcase_32 AC 22 ms
9,476 KB
testcase_33 AC 21 ms
8,984 KB
testcase_34 AC 28 ms
10,084 KB
testcase_35 AC 25 ms
10,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;

void main() {
    immutable long MOD = 10^^9 + 7;
    
    auto N = readln.chomp.to!int;
    auto Q = readln.chomp.to!int;
    auto B = new long[](N+10);
    auto X = new long[](N+10);
    auto Y = new long[](N+10);
    auto Z = new SegmentTree!(long, (a, b) => a + b, 0L)(N+10);
    fill(B, 1);

    while (Q--) {
        auto s = readln.split;
        int i = s[1].to!int;
        if (s[0] == "x") {
            long v = s[2].to!long;
            X[i] = v;
            Z.update(i, v * B[i] % MOD * B[i] % MOD);
        } else if (s[0] == "y") {
            long v = s[2].to!long;
            Y[i] = v;
            B[i+1] = (B[i] * Y[i] + 1) % MOD;
            Z.update(i+1, X[i+1] * B[i+1] % MOD * B[i+1] % MOD);
            i += 1;
            while (i < N) {
                B[i+1] = (B[i] * Y[i] + 1) % MOD;
                Z.update(i+1, X[i+1] * B[i+1] % MOD * B[i+1] % MOD);
                if (Y[i] == 0) break;
                i += 1;
            }
        } else {
            writeln((Z.query(0, i-1) + 1) % MOD);
        }
    }
}

class SegmentTree(T, alias op, T e) {
    T[] table;
    int size;
    int offset;

    this(int n) {
        size = 1;
        while (size <= n) size <<= 1;
        size <<= 1;
        table = new T[](size);
        fill(table, e);
        offset = size / 2;
    }

    void update(int pos, T val) {
        pos += offset;
        table[pos] = val;
        while (pos > 1) {
            pos /= 2;
            table[pos] = op(table[pos*2], table[pos*2+1]);
        }
    }

    T query(int l, int r) {
        return query(l, r, 1, 0, offset-1);
    }

    T query(int l, int r, int i, int a, int b) {
        if (b < l || r < a) {
            return e;
        } else if (l <= a && b <= r) {
            return table[i];
        } else {
            return op(query(l, r, i*2, a, (a+b)/2), query(l, r, i*2+1, (a+b)/2+1, b));
        }
    }
}
0