1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
import numpy as np
class SegmentTreeInjectable:
def __init__( self , n, identity_factory, func):
n2 = 1 << (n - 1 ).bit_length()
self .offset = n2
self .tree = [identity_factory() for _ in range (n2 << 1 )]
self .func = func
self .idf = identity_factory
@classmethod
def from_array( cls , arr, identity_factory, func):
ins = cls ( len (arr), identity_factory, func)
ins.tree[ins.offset:ins.offset + len (arr)] = arr
for i in range (ins.offset - 1 , 0 , - 1 ):
l = i << 1
r = l + 1
ins.tree[i] = func(ins.tree[l], ins.tree[r])
return ins
def add( self , i, x):
i + = self .offset
self .tree[i] = self .func( self .tree[i], x)
self .__upstream(i)
def update( self , i, x):
i + = self .offset
self .tree[i] = x
self .__upstream(i)
def __upstream( self , i):
tree = self .tree
func = self .func
while i > 1 :
j = i >> 1
tree[j] = func(tree[j << 1 ], tree[(j << 1 ) | 1 ])
i >> = 1
def get_range( self , a, b):
tree = self .tree
func = self .func
result_l = self .idf()
result_r = self .idf()
l = a + self .offset
r = b + self .offset
while l < r:
if r & 1 :
result_r = func(tree[r - 1 ], result_r)
if l & 1 :
result_l = func(result_l, tree[l])
l + = 1
l >> = 1
r >> = 1
return func(result_l, result_r)
def get_all( self ):
return self .tree[ 1 ]
def get_point( self , i):
return self .tree[i + self .offset]
def debug_print( self ):
i = 1
while i < = self .offset:
print ( self .tree[i:i * 2 ])
i << = 1
mat_zero = np.array([
[ 1 , 0 , 1 , 0 ],
[ 1 , 0 , 0 , 1 ],
[ 0 , 0 , 1 , 0 ],
[ 0 , 0 , 0 , 1 ],
], np.int64)
mat_one = np.array([
[ 1 , 1 , 0 , 0 ],
[ 0 , 1 , 0 , 0 ],
[ 1 , 0 , 0 , 1 ],
[ 0 , 0 , 0 , 1 ],
], np.int64)
mat_ques = np.array([
[ 2 , 0 , 0 , 1 ],
[ 1 , 0 , 0 , 1 ],
[ 1 , 0 , 0 , 1 ],
[ 0 , 0 , 0 , 1 ],
], np.int64)
mat_identity = np.eye( 4 , dtype = np.int64)
MOD = 998244353
n, q = map ( int , input ().split())
s = input ()
init_array = []
for c in s:
if c = = '0' :
init_array.append(mat_zero)
elif c = = '1' :
init_array.append(mat_one)
else :
init_array.append(mat_ques)
sgt = SegmentTreeInjectable.from_array(init_array, lambda : mat_identity, lambda a, b: a @ b % MOD)
for _ in range (q):
x, c = input ().split()
x = int (x) - 1
if c = = '0' :
sgt.update(x, mat_zero)
elif c = = '1' :
sgt.update(x, mat_one)
else :
sgt.update(x, mat_ques)
res = sgt.get_all()
ans = (res[ 0 ]. sum () - 1 ) % MOD
print (ans)
|