結果

問題 No.1548 [Cherry 2nd Tune B] 貴方と私とサイクルとモーメント
ユーザー 👑 KazunKazun
提出日時 2021-04-11 02:38:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,790 ms / 4,500 ms
コード長 7,744 bytes
コンパイル時間 320 ms
コンパイル使用メモリ 87,192 KB
実行使用メモリ 197,808 KB
最終ジャッジ日時 2023-08-21 09:33:00
合計ジャッジ時間 53,044 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
71,256 KB
testcase_01 AC 79 ms
71,304 KB
testcase_02 AC 1,614 ms
123,072 KB
testcase_03 AC 741 ms
95,840 KB
testcase_04 AC 1,312 ms
184,240 KB
testcase_05 AC 503 ms
87,200 KB
testcase_06 AC 1,289 ms
116,812 KB
testcase_07 AC 1,060 ms
196,248 KB
testcase_08 AC 1,672 ms
123,336 KB
testcase_09 AC 1,308 ms
102,696 KB
testcase_10 AC 927 ms
180,056 KB
testcase_11 AC 1,114 ms
184,400 KB
testcase_12 AC 1,142 ms
96,332 KB
testcase_13 AC 678 ms
181,904 KB
testcase_14 AC 1,485 ms
106,604 KB
testcase_15 AC 562 ms
196,988 KB
testcase_16 AC 1,310 ms
136,540 KB
testcase_17 AC 1,480 ms
178,220 KB
testcase_18 AC 1,170 ms
191,076 KB
testcase_19 AC 920 ms
99,856 KB
testcase_20 AC 858 ms
187,892 KB
testcase_21 AC 1,235 ms
117,984 KB
testcase_22 AC 1,746 ms
197,148 KB
testcase_23 AC 1,451 ms
197,252 KB
testcase_24 AC 1,461 ms
196,916 KB
testcase_25 AC 1,767 ms
196,644 KB
testcase_26 AC 1,739 ms
197,192 KB
testcase_27 AC 1,790 ms
197,808 KB
testcase_28 AC 1,721 ms
197,372 KB
testcase_29 AC 1,706 ms
197,644 KB
testcase_30 AC 1,690 ms
197,316 KB
testcase_31 AC 1,716 ms
197,092 KB
testcase_32 AC 266 ms
196,492 KB
testcase_33 AC 1,228 ms
196,776 KB
testcase_34 AC 1,116 ms
197,216 KB
testcase_35 AC 1,159 ms
197,288 KB
testcase_36 AC 949 ms
197,000 KB
testcase_37 AC 1,406 ms
197,200 KB
testcase_38 AC 261 ms
197,308 KB
testcase_39 AC 821 ms
197,172 KB
testcase_40 AC 809 ms
196,996 KB
testcase_41 AC 999 ms
197,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Lazy_Evaluation_Tree():
    def __init__(self,L,calc,unit,op,comp,id,index):
        """calcを演算,opを作用とするリストLのSegment Treeを作成

        calc:演算
        unit:モノイドcalcの単位元 (xe=ex=xを満たすe)
        op:作用素
        comp:作用素の合成
        id:恒等写像

        [条件] M:Monoid,F={f:F x M→ M:作用素}に対して,以下が成立する.
        Fは恒等写像 id を含む.つまり,任意の x in M に対して id(x)=x
        Fは写像の合成に閉じている.つまり,任意の f,g in F に対して, comp(f,g) in F
        任意の f in F, x,y in M に対して,f(xy)=f(x)f(y)である.

        [注記]
        作用素は左から掛ける.更新も左から.
        """

        self.calc=calc
        self.unit=unit
        self.op=op
        self.comp=comp
        self.id=id
        self.index=index

        N=len(L)
        d=max(1,(N-1).bit_length())
        k=1<<d

        self.data=[unit]*k+L+[unit]*(k-len(L))
        self.lazy=[self.id]*(2*k)
        self.N=k
        self.depth=d

        for i in range(k-1,0,-1):
            self.data[i]=calc(self.data[i<<1],self.data[i<<1|1])

    def _eval_at(self,m):
        if self.lazy[m]==self.id:
            return self.data[m]
        return self.op(self.lazy[m],self.data[m])

    #配列の第m要素を下に伝搬
    def _propagate_at(self,m):
        self.data[m]=self._eval_at(m)

        if m<self.N and self.lazy[m]!=self.id:
            self.lazy[m<<1]=self.comp(
                self.lazy[m],
                self.lazy[m<<1]
                )

            self.lazy[m<<1|1]=self.comp(
                self.lazy[m],
                self.lazy[m<<1|1]
                )

        self.lazy[m]=self.id

    #配列の第m要素より上を全て伝搬
    def _propagate_above(self,m):
        H=m.bit_length()
        for h in range(H-1,0,-1):
            self._propagate_at(m>>h)

    #配列の第m要素より上を全て再計算
    def _recalc_above(self,m):
        while m>1:
            m>>=1
            self.data[m]=self.calc(
                self._eval_at(m<<1),
                self._eval_at(m<<1|1)
            )

    def get(self,k):
        index=self.index
        m=k-index+self.N
        self._propagate_above(m)
        self.data[m]=self._eval_at(m)
        self.lazy[m]=self.id
        return self.data[m]

    #作用
    def operate(self,From,To,alpha,left_closed=True,right_closed=True):
        index=self.index
        L=(From-index)+self.N+(not left_closed)
        R=(To-index)+self.N+(right_closed)

        L0=R0=-1
        X,Y=L,R-1
        while X<Y:
            if X&1:
                L0=max(L0,X)
                X+=1

            if Y&1==0:
                R0=max(R0,Y)
                Y-=1

            X>>=1
            Y>>=1

        L0=max(L0,X)
        R0=max(R0,Y)

        self._propagate_above(L0)
        self._propagate_above(R0)

        while L<R:
            if L&1:
                self.lazy[L]=self.comp(alpha,self.lazy[L])
                L+=1

            if R&1:
                R-=1
                self.lazy[R]=self.comp(alpha,self.lazy[R])

            L>>=1
            R>>=1

        self._recalc_above(L0)
        self._recalc_above(R0)

    def update(self,k,x):
        """ 第k要素をxに変更する.
        """
        index=self.index
        m=k-index+self.N
        self._propagate_above(m)
        self.data[m]=x
        self.lazy[m]=self.id
        self._recalc_above(m)

    def product(self,From,To,left_closed=True,right_closed=True):
        index=self.index
        L=(From-index)+self.N+(not left_closed)
        R=(To-index)+self.N+(right_closed)

        L0=R0=-1
        X,Y=L,R-1
        while X<Y:
            if X&1:
                L0=max(L0,X)
                X+=1

            if Y&1==0:
                R0=max(R0,Y)
                Y-=1

            X>>=1
            Y>>=1

        L0=max(L0,X)
        R0=max(R0,Y)

        self._propagate_above(L0)
        self._propagate_above(R0)

        vL=vR=self.unit

        while L<R:
            if L&1:
                vL=self.calc(vL,self._eval_at(L))
                L+=1

            if R&1:
                R-=1
                vR=self.calc(self._eval_at(R),vR)

            L>>=1
            R>>=1

        return self.calc(vL,vR)

    def all_product(self):
        return self.product(1,self.N,1)

    #リフレッシュ
    def refresh(self):
        for m in range(1,2*self.N):
            self.data[m]=self._eval_at(m)

            if m<self.N and self.lazy[m]!=self.id:
                self.lazy[m<<1]=self.comp(
                    self.lazy[m],
                    self.lazy[m<<1]
                    )

                self.lazy[m<<1|1]=self.comp(
                    self.lazy[m],
                    self.lazy[m<<1|1]
                    )

            self.lazy[m]=self.id

    def __getitem__(self,k):
        return self.get(k)

    def __setitem__(self,k,x):
        self.update(k,x)
#================================================
"""
上32bit: データ
下32bit: 担っている要素数
"""

def calc(x,y):
    a,p=x>>32,x&msk
    b,q=y>>32,y&msk
    c,r=(a+b)%Mod,(p+q)
    return (c<<32)+r

def op1(t,x):
    a,p=x>>32,x&msk
    t=(p*t)%Mod
    return (t<<32)+p

def op2(t,x):
    a,p=x>>32,x&msk
    t=(p*pow(t,2,Mod))%Mod
    return (t<<32)+p

def op3(t,x):
    a,p=x>>32,x&msk
    t=(p*pow(t,3,Mod))%Mod
    return (t<<32)+p

def op4(t,x):
    a,p=x>>32,x&msk
    t=(p*pow(t,4,Mod))%Mod
    return (t<<32)+p

def comp(a,b):
    return a
#================================================
def product_mod(*A):
    x=1
    for a in A:
        x*=a
        x%=Mod
    return x
#================================================
import sys
from operator import add
input=sys.stdin.readline
write=sys.stdout.write

N=int(input())

A=list(map(int,input().split()))

Mod=998244353
msk=(1<<32)-1

L_inv=[0]*(N+1)
L_inv[1]=1
for k in range(2,N+1):
    q,r=divmod(Mod,k)
    L_inv[k]=(-q*L_inv[r])%Mod

X=[]
Z1=Lazy_Evaluation_Tree([(a<<32)+1 for a in A],calc,0,op1,comp,-1,1)
Z2=Lazy_Evaluation_Tree([(pow(a,2,Mod)<<32)+1 for a in A],calc,0,op2,comp,-1,1)
Z3=Lazy_Evaluation_Tree([(pow(a,3,Mod)<<32)+1 for a in A],calc,0,op3,comp,-1,1)
Z4=Lazy_Evaluation_Tree([(pow(a,4,Mod)<<32)+1 for a in A],calc,0,op4,comp,-1,1)

Q=int(input())

for _ in range(Q):
    T,*Y=map(int,input().split())
    if T==0:
        U,V,W,B=Y
        if U>V:
            U,V=V,U

        if U<W<V:
            Z1.operate(U,V,B)
            Z2.operate(U,V,B)
            Z3.operate(U,V,B)
            Z4.operate(U,V,B)
        else:
            Z1.operate(1,U,B);Z1.operate(V,N,B)
            Z2.operate(1,U,B);Z2.operate(V,N,B)
            Z3.operate(1,U,B);Z3.operate(V,N,B)
            Z4.operate(1,U,B);Z4.operate(V,N,B)
        continue

    if T==1:
        #1次中心化モーメントは0確定
        X.append(0)
        continue

    U,V,W=Y
    if U>V:
        U,V=V,U

    if U<W<V:
        L=V-U+1
        T1=Z1.product(U,V)>>32
        T2=Z2.product(U,V)>>32
        T3=Z3.product(U,V)>>32
        T4=Z4.product(U,V)>>32
    else:
        L=U+(N-V+1)
        T1=calc(Z1.product(1,U),Z1.product(V,N))>>32
        T2=calc(Z2.product(1,U),Z2.product(V,N))>>32
        T3=calc(Z3.product(1,U),Z3.product(V,N))>>32
        T4=calc(Z4.product(1,U),Z4.product(V,N))>>32

    l_inv=L_inv[L]

    if T==2:
        E=T2-product_mod(T1,T1,l_inv)
    elif T==3:
        E=T3-3*product_mod(T2,T1,l_inv)+2*product_mod(T1,T1,T1,l_inv,l_inv)
    else:
        E=T4-4*product_mod(T3,T1,l_inv)+6*product_mod(T2,T1,T1,l_inv,l_inv)-3*product_mod(pow(T1,4,Mod),pow(l_inv,3,Mod))

    E%=Mod
    E=(E*l_inv)%Mod
    X.append(E)

write("\n".join(map(str,X)))
0