結果
| 問題 |
No.1548 [Cherry 2nd Tune B] 貴方と私とサイクルとモーメント
|
| コンテスト | |
| ユーザー |
👑 Kazun
|
| 提出日時 | 2021-04-02 02:48:55 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 7,378 bytes |
| コンパイル時間 | 289 ms |
| コンパイル使用メモリ | 82,436 KB |
| 実行使用メモリ | 193,028 KB |
| 最終ジャッジ日時 | 2024-12-14 19:33:30 |
| 合計ジャッジ時間 | 43,556 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 3 WA * 39 |
ソースコード
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)
#================================================
class S:
def __init__(self,x,y,z,w,m):
self.x=x
self.y=y
self.z=z
self.w=w
self.m=m
def __str__(self):
return "[{}, {}, {}, {}] ({})".format(self.x,self.y,self.z,self.w,self.m)
def __repr__(self):
return str(self)
def __neg__(self):
return S(Mod-self.x,Mod-self.y,Mod-self.z,Mod-self.w,-m)
def __add__(self,other):
return S(
(self.x+other.x)%Mod,
(self.y+other.y)%Mod,
(self.z+other.z)%Mod,
(self.w+other.w)%Mod,
self.m+other.m)
def __iter__(self):
yield from (self.x,self.y,self.z,self.w,self.m)
#================================================
def op(a,p):
m=p.m
x=(m*a)%Mod
y=(m*a*a)%Mod
z=(m*a*a*a)%Mod
w=(m*a*a*a*a)%Mod
return S(x,y,z,w,m)
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=10**9+7
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=[]
A=[S(a,(a*a)%Mod,pow(a,3,Mod),pow(a,4,Mod),1) for a in A]
Z=Lazy_Evaluation_Tree(A,add,S(0,0,0,0,0),op,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:
Z.operate(U,V,B)
else:
Z.operate(1,U,B)
Z.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:
T1,T2,T3,T4,L=Z.product(U,V)
else:
T1,T2,T3,T4,L=Z.product(1,U)+Z.product(V,N)
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)))
Kazun