結果

問題 No.3227 Matrix Query
ユーザー 👑 p-adic
提出日時 2025-07-29 19:57:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 6,642 ms / 8,000 ms
コード長 7,411 bytes
コンパイル時間 5,275 ms
コンパイル使用メモリ 82,464 KB
実行使用メモリ 267,424 KB
最終ジャッジ日時 2025-07-30 16:26:46
合計ジャッジ時間 92,063 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

class ModB:
	B = 998244353
	length_bound = 10**6 #User definition
	length_max = min( length_bound , B )
	inverse = [None,1]
	factorial = [1]
	factorial_inverse = [1]
	def SetModulo(B):
		ModB.B = int(B)
		assert(ModB.B > 0)
		ModB.length_max = min( ModB.length_bound , ModB.B )
		ModB.inverse = [None,1] if ModB.B>1 else [0]
		ModB.factorial = [1 if ModB.B>1 else 0]
		ModB.factorial_inverse = [1 if ModB.B>1 else 0]

	def __init__(self,val,valid = False):
		self.val = int(val)
		if not valid and not(0 <= self.val < ModB.B):self.val %= ModB.B
	def get(n):
		return n.val if n.__class__ == __class__ else n
	def copy(self):
		return ModB(self.val,True)

	def __str__(self):
		return str(self.val)

	def __eq__(self,x):
		if x.__class__ != __class__:x=ModB(x)
		return x.val==self.val
	def __ne__(self,other):
		return not( self == other )

	def __iadd__(self,x):
		self.val += ModB.ref(x).val
		if self.val >= ModB.B:self.val -= ModB.B
		return self
	def __add__(self,x):
		a = self.copy()
		a += x
		return a
	def __radd__(self,x):
		return ModB(x + self.val)

	def __neg__(self):
		return ModB(ModB.B - self.val if self.val else 0,True)
	def __isub__(self,x):
		self.val -= ModB.ref(x).val
		if self.val < 0:self.val += ModB.B
		return self
	def __sub__(self,x):
		a = self.copy()
		a -= x
		return a
	def __rsub__(self,x):
		return ModB(x - self.val)

	def __mul__(self,x):
		return ModB.get(x) * self
	def __rmul__(self,x):
		return ModB(self.val * x)

	def __pow__(self,n): #Supported only if n>=0.
		assert 0<=n
		answer = ModB(1)
		power = self.copy()
		while n > 0:
			if n&1:answer *= power.val
			power *= power.val
			n >>= 1
		return answer
	def __xor__(self,n): #Supported only if B is a prime and val!=0, or n>=0.
		return self ** ( ( n * (2 - ModB.B) )if n < 0 else n )

	def Inverse(n): #Supported only if B is a prime.
		if n.__class__ == __class__:n=n.val
		if n >= ModB.B:n %= ModB.B
		assert n > 0 or ModB.B == 1
		if n < ModB.length_max:
			while len(ModB.inverse) <= n:ModB.inverse+=[ModB.B - ModB.inverse[ModB.B % len(ModB.inverse)] * ( ModB.B // len(ModB.inverse) ) % ModB.B]
			return ModB(ModB.inverse[n],True)
		return ModB(n) ** ( ModB.B - 2 )
	def __truediv__(self,x):
		return ModB.Inverse(x) * self
	def __rtruediv__(self,x):
		return x * ModB.Inverse(self.val)

	def Factorial(n):
		while len(ModB.factorial) <= n:ModB.factorial+=[ModB.factorial[-1] * len(ModB.factorial) % ModB.B]
		return ModB(ModB.factorial[n],True)
	def FactorialInverse(n): #Supported only if B is a prime.
		while len(ModB.factorial_inverse) <= n:ModB.factorial_inverse+=[ModB.factorial_inverse[-1] * ModB.Inverse( len(ModB.factorial_inverse) ).val % ModB.B]
		return ModB(ModB.factorial_inverse[n],True)
	def Combination(n,m): #Supported only if B is a prime.
		return ModB.Factorial(n) * (ModB.FactorialInverse(m).val * ModB.FactorialInverse(n-m).val)if 0<=m<=n else ModB(0,True)

	#private:
	def ref(n):
		return n if n.__class__ == __class__ else ModB(n)

def copy(n):return n.copy()if hasattr(n,"copy")else n

class TwoByTwoMatrix:
	zero=None
	one=None

	def __init__(self,M00,M01,M10,M11):
		self.M00 = copy(M00)
		self.M01 = copy(M01)
		self.M10 = copy(M10)
		self.M11 = copy(M11)
	def copy(self):
		return self.__class__(self.M00,self.M01,self.M10,self.M11)

	def __eq__(self,other):
		return self.M00 == other.M00 and self.M01 == other.M01 and self.M10 == other.M10 and self.M11 == other.M11
	def __ne__(self,other):
		return not( self == other )

	def __iadd__(self,other):
		self.M00 += other.M00
		self.M01 += other.M01
		self.M10 += other.M10
		self.M11 += other.M11
		return self
	def __add__(self,other):
		M = self.copy()
		M += other
		return M

	def __isub__(self,other):
		self.M00 -= other.M00
		self.M01 -= other.M01
		self.M10 -= other.M10
		self.M11 -= other.M11
		return self
	def __sub__(self,other):
		M = self.copy()
		M -= other
		return M
	def __neg__(self):
		return self.__class__(-self.M00,-self.M01,-self.M10,-self.M11)

	def __mul__(self,other):
		return self.__class__(self.M00 * other.M00 + self.M01 * other.M10,self.M00 * other.M01 + self.M01 * other.M11,self.M10 * other.M00 + self.M11 * other.M10,self.M10 * other.M01 + self.M11 * other.M11)
	def __imul__(self,other):
		self.M00 , self.M01 , self.M10 , self.M11 = self.M00 * other.M00 + self.M01 * other.M10 , self.M00 * other.M01 + self.M01 * other.M11 , self.M10 * other.M00 + self.M11 * other.M10 , self.M10 * other.M01 + self.M11 * other.M11
		return self
	def ScalarMultiply(self,x):
		self.M00 *= x
		self.M01 *= x
		self.M10 *= x
		self.M11 *= x
		return self

	def det(self):
		return self.M00 * self.M11 - self.M01 * self.M10
	def tr(self):
		return self.M00 + self.M11

	def Adjugate(self):
		return self.__class__( self.M11 , - self.M01 , - self.M10 , self.M00 )
	def Inverse(self):
		return self.Adjugate().ScalarMultiply( 1 / self.det() )
		#d = self.det()
		#assert( d in [1,-1] ) #For the case of integer coefficients
		#return self.Adjugate().ScalarMultiply( d )
	def __truediv__(self,other):
		return self * other.Inverse()
	def __itruediv__(self,other):
		self *= other.Inverse()
		return self

	def __pow__(self,n): #Supported only when n>=0
		answer = self.__class__.one.copy()
		power = self.copy()
		while n > 0:
			if n&1:answer *= power
			power.Square()
			n >>= 1
		return answer
	def __xor__(self,n):
		return self.Inverse()**(-n)if n < 0 else self ** n

	#private:
	def Square(self):
		self.M00 , self.M01 , self.M10 , self.M11 = self.M00 ** 2 + self.M01 * self.M10 , ( self.M00 + self.M11 ) * self.M01 , self.M10 * ( self.M00 + self.M11 ) , self.M10 * self.M01 + self.M11 ** 2
TwoByTwoMatrix.zero = TwoByTwoMatrix(ModB(0,1),ModB(0,1),ModB(0,1),ModB(0,1)) #User's definition
TwoByTwoMatrix.one = TwoByTwoMatrix(ModB(1,1),ModB(0,1),ModB(0,1),ModB(1,1)) #User's definition

def copy(n):return n.copy()if hasattr(n,"copy")else n
def rec_str(a):return"".join(["[",", ".join(rec_str(x)for x in a),"]"])if isinstance(a,list)else str(a)
class SegmentTree:
	e=TwoByTwoMatrix.one #User's definition
	op=lambda x,y:x*y #User's definition

	def __init__(self,x):
		b=isinstance(x,int)
		if b:self.N=x
		else:self.N=len(x)
		self.p=1
		while self.N>self.p:self.p<<=1
		if b:self.T=[copy(__class__.e)for i in R(self.p<<1)]
		else:
			self.T=[__class__.e]*(self.p<<1)
			for i in R(0,self.N):self.T[self.p|i]=copy(x[i])
			for j in R(self.p-1,0,-1):self.T[j]=__class__.op(self.T[j<<1],self.T[(j<<1)|1])

	def copy(self):
		a=__class__([])
		a.N=self.N
		a.p=self.p
		a.T=copy(self.T)
		return a

	def Set(self,i,u):
		assert 0<=i<self.N
		j=self.p|i
		self.T[j],j=u,j>>1
		while j:self.T[j],j=__class__.op(self.T[j<<1],self.T[(j<<1)|1]),j>>1

	def Get(self,i):
		assert 0<=i<self.N
		return selgf.IntervalProduct(i,i)

	def IntervalProduct(self,l,r): #l,rは始端,終端
		l,r=max(0,l),min(r,self.N-1)
		assert l-1<=r
		l|=self.p
		r+=self.p+1
		a,b=copy(__class__.e),copy(__class__.e)
		while l<r:
			if l&1:a,l=__class__.op(a,self.T[l]),l+1
			if r&1:b,r=__class__.op(self.T[r-1],b),r-1
			l,r=l>>1,r>>1
		return __class__.op(a,b)

R=range
O=print
J=lambda:list(map(int,input().split()))
ModB.B,N=J()
A=[]
for i in R(N):
	a=J()
	b=J()
	A+=[TwoByTwoMatrix(ModB(a[0]),ModB(a[1]),ModB(b[0]),ModB(b[1]))]
X=SegmentTree(A)
for q in R(sum(J())):
	i,l,r=J()
	a=J()
	b=J()
	X.Set(i-1,TwoByTwoMatrix(ModB(a[0]),ModB(a[1]),ModB(b[0]),ModB(b[1])))
	B=X.IntervalProduct(l-1,r-1)
	O(B.M00,B.M01)
	O(B.M10,B.M11)
0