結果

問題 No.749 クエリ全部盛り
ユーザー toyuzukotoyuzuko
提出日時 2020-05-14 21:45:32
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 4,049 bytes
コンパイル時間 374 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 66,944 KB
最終ジャッジ日時 2024-09-16 00:32:40
合計ジャッジ時間 3,562 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegmentTreeLazy():
    def __init__(self, arr, ti, ei, func, op, merge):
        self.h = (len(arr) - 1).bit_length()
        self.n = 2**self.h
        self.func = func
        self.op = op
        self.merge = merge
        self.ti = ti
        self.ei = ei
        self.val = [ti for _ in range(2 * self.n)]
        for i in range(len(arr)):
            self.val[self.n + i] = arr[i]
        for i in range(1, self.n)[::-1]:
            self.val[i] = self.func(self.val[2 * i], self.val[2 * i + 1])
        self.laz = [ei for _ in range(2 * self.n)]

    def reflect(self, k):
        if self.laz[k] == self.ei:
            return self.val[k]
        return self.op(self.val[k], self.laz[k])

    def propagate(self, k):
        if self.laz[k] == self.ei: return
        self.laz[2 * k] = self.merge(self.laz[2 * k], self.laz[k])
        self.laz[2 * k + 1] = self.merge(self.laz[2 * k + 1], self.laz[k])
        self.val[k] = self.reflect(k)
        self.laz[k] = self.ei

    def thrust(self, k):
        for i in range(1, self.h + 1)[::-1]:
            self.propagate(k >> i)

    def recalc(self, k):
        while k:
            k >>= 1
            self.val[k] = self.func(self.reflect(2 * k), self.reflect(2 * k + 1))

    def update(self, lt, rt, x):
        lt += self.n
        rt += self.n
        vl = lt
        vr = rt
        self.thrust(lt)
        self.thrust(rt - 1)
        while rt - lt > 0:
            if lt & 1:
                self.laz[lt] = self.merge(self.laz[lt], x)
                lt += 1
            if rt & 1:
                rt -= 1
                self.laz[rt] = self.merge(self.laz[rt], x)
            lt >>= 1
            rt >>= 1
        self.recalc(vl)
        self.recalc(vr - 1)

    def query(self, lt, rt):
        lt += self.n
        rt += self.n
        self.thrust(lt)
        self.thrust(rt - 1)
        vl = vr = self.ti
        while rt - lt > 0:
            if lt & 1:
                vl = self.func(vl, self.reflect(lt))
                lt += 1
            if rt & 1:
                rt -= 1
                vr = self.func(self.reflect(rt), vr)
            lt >>= 1
            rt >>= 1
        return self.func(vl, vr)

def fibonacci(n, mod=1000000007):
    res = [0 for _ in range(n + 1)]
    res[0] = 0
    res[1] = 1
    for i in range(2, n + 1):
        res[i] = res[i - 1] + res[i - 2]
        res[i] %= mod
    return res

import sys
input = sys.stdin.buffer.eadline

MOD = 1000000007
MODsq = MOD**2

N, Q = map(int, input().split())

F = fibonacci(N)

arr = [MOD + F[i] for i in range(N)]
ti = 0
ei = MODsq

def func(a, b):
    a3 = a % MOD
    a2 = (a - a3) // MOD % MOD
    a1 = (a - a3 - a2 * MOD) // MODsq
    b3 = b % MOD
    b2 = (b - b3) // MOD % MOD
    b1 = (b - b3 - b2 * MOD) // MODsq
    c1 = (a1 + b1) % MOD
    c2 = (a2 + b2) % MOD
    c3 = (a3 + b3) % MOD
    return c1 * MODsq + c2 * MOD + c3

def op(a, x):
    a3 = a % MOD
    a2 = (a - a3) // MOD % MOD
    a1 = (a - a3 - a2 * MOD) // MODsq
    x3 = x % MOD
    x2 = (x - x3) // MOD % MOD
    x1 = (x - x3 - x2 * MOD) // MODsq
    c1 = (a1 * x1 + a2 * x2 + a3 * x3) % MOD
    c2 = a2 % MOD
    c3 = a3 % MOD
    return c1 * MODsq + c2 * MOD + c3

def merge(x, y):
    x3 = x % MOD
    x2 = (x - x3) // MOD % MOD
    x1 = (x - x3 - x2 * MOD) // MODsq
    y3 = y % MOD
    y2 = (y - y3) // MOD % MOD
    y1 = (y - y3 - y2 * MOD) // MODsq
    z1 = (x1 * y1) % MOD
    z2 = (x2 * y1 + y2) % MOD
    z3 = (x3 * y1 + y3) % MOD
    return z1 * MODsq + z2 * MOD + z3

st = SegmentTreeLazy(arr, ti, ei, func, op, merge)

res = []

for _ in range(Q):
    q, l, r, k = map(int, input().split())
    if q == 0:
        x = st.query(l, r + 1)
        x3 = x % MOD
        x2 = (x - x3) // MOD % MOD
        x1 = (x - x3 - x2 * MOD) // MODsq
        res.append(k * x1 % MOD)
    elif q == 1:
        st.update(l, r + 1, k * MOD)
    elif q == 2:
        st.update(l, r + 1, MODsq + k * MOD)
    elif q == 3:
        st.update(l, r + 1, k * MODsq)
    else:
        st.update(l, r + 1, MODsq + k)

print('\n'.join(map(str, res)))
0