結果

問題 No.2568 列辞書順列列
ユーザー rlangevin
提出日時 2023-12-15 12:23:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 394 ms / 3,000 ms
コード長 1,295 bytes
コンパイル時間 154 ms
コンパイル使用メモリ 82,196 KB
実行使用メモリ 111,392 KB
最終ジャッジ日時 2024-09-27 06:14:55
合計ジャッジ時間 10,351 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class SegmentTree:
    def __init__(self, size, f=min, default=10**18):
        self.size = 2**(size-1).bit_length() 
        self.default = default
        self.dat = [default]*(self.size*2) 
        self.f = f

    def update(self, i, x):
        i += self.size
        self.dat[i] = x
        while i > 0:
            i >>= 1
            self.dat[i] = self.f(self.dat[i*2], self.dat[i*2+1])

    def query(self, l, r):
        l += self.size
        r += self.size
        lres, rres = self.default, self.default
        while l < r:
            if l & 1:
                lres = self.f(lres, self.dat[l])
                l += 1

            if r & 1:
                r -= 1
                rres = self.f(self.dat[r], rres) 
            l >>= 1
            r >>= 1
        res = self.f(lres, rres)
        return res
    

N, M, Q = map(int, input().split())
A = list(map(int, input().split()))
mod = 998244353
p = [1] * (N + 1)
for i in range(N):
    p[i + 1] = p[i] * M % mod

def op(x, y):
    a = (x[0] * p[y[1]] + y[0]) % mod
    b = x[1] + y[1]
    return (a, b)

T = SegmentTree(N, op, (0, 0))
for i in range(N):
    T.update(i, (A[i]-1, 1))

for _ in range(Q):
    l, r = map(int, input().split())
    l -= 1
    print((T.query(l, r)[0] + 1)%mod)
0