Submission #48412487
Source Code Expand
import os
import sys
from typing import List, Optional, Tuple
sys.setrecursionlimit(210000)
MOD = 998244353
if os.getenv('TEST'):
def eprint(*args, **kwargs):
print('[\033[31mEPRINT\033[0m]', *args, file=sys.stderr, **kwargs)
else:
def eprint(*args, **kwargs):
pass
def inv(x, m):
y, u, v = m, 1, 0
while y != 0:
t = x // y
x -= t * y
u -= t * v
x, y = y, x
u, v = v, u
return u % m
class SegNode(object):
@staticmethod
def make_tree(nodes: List['SegNode']) -> 'SegNode':
parents = nodes[:]
while len(parents) > 1:
new_parents = []
for i in range(0, len(parents), 2):
if i + 1 < len(parents):
new_parents.append(SegNode.make_parent(parents[i], parents[i + 1]))
else:
new_parents.append(parents[i])
parents = new_parents
return parents[0]
@staticmethod
def make_parent(left_node: 'SegNode', right_node: 'SegNode') -> 'SegNode':
new_node = SegNode(left_node.left_index, right_node.right_index)
new_node.left_node = left_node
new_node.right_node = right_node
left_node.parent_node = new_node
right_node.parent_node = new_node
return new_node
def __init__(self, left_index: int, right_index: int) -> None:
self.left_index = left_index
self.right_index = right_index
self.left_node: Optional['SegNode'] = None
self.right_node: Optional['SegNode'] = None
self.parent_node: Optional['SegNode'] = None
self.multi_value = 1
self.add_value = 0
def update(self, left_index: int, right_index: int, multi_value: int, add_value: int) -> None:
if self.right_index <= left_index or right_index <= self.left_index:
return
if left_index <= self.left_index and self.right_index <= right_index:
self.apply(multi_value, add_value)
return
if self.left_node is not None and self.right_node is not None:
self.left_node.update(
self.left_index,
self.right_index,
self.multi_value,
self.add_value)
self.left_node.update(left_index, right_index, multi_value, add_value)
self.right_node.update(
self.left_index,
self.right_index,
self.multi_value,
self.add_value)
self.right_node.update(left_index, right_index, multi_value, add_value)
self.multi_value = 1
self.add_value = 0
def apply(self, multi_value: int, add_value: int) -> None:
self.multi_value = (self.multi_value * multi_value) % MOD
self.add_value = (self.add_value * multi_value + add_value) % MOD
def get_value(self) -> Tuple[int, int]:
multi_value = self.multi_value
add_value = self.add_value
node = self.parent_node
while node is not None:
multi_value = (multi_value * node.multi_value) % MOD
add_value = (add_value * node.multi_value + node.add_value) % MOD
node = node.parent_node
return multi_value, add_value
def __str__(self) -> str:
return f'[{self.left_index},{self.right_index}): {self.multi_value}*x+{self.add_value}'
def eprint_tree(root: SegNode) -> None:
nodes = [root]
while len(nodes) != 0:
new_nodes = []
for node in nodes:
eprint(node, end='')
if node.left_node is not None:
new_nodes.append(node.left_node)
if node.right_node is not None:
new_nodes.append(node.right_node)
eprint()
nodes = new_nodes
def main() -> None:
_, M = map(int, input().split())
A = list(map(int, input().split()))
nodes = [SegNode(i, i + 1) for i in range(len(A))]
root = SegNode.make_tree(nodes)
for _ in range(M):
L, R, X = map(int, input().split())
L -= 1
length = R - L
multi_value = ((length - 1) * inv(length, MOD)) % MOD
add_value = (X * inv(length, MOD)) % MOD
root.update(L, R, multi_value, add_value)
results: List[int] = []
for node, a in zip(nodes, A):
multi_value, add_value = node.get_value()
results.append((a * multi_value + add_value) % MOD)
print(*results)
if __name__ == '__main__':
main()
Submission Info
| Submission Time |
|
| Task |
F - Random Update Query |
| User |
takedarts |
| Language |
Python (PyPy 3.10-v7.3.12) |
| Score |
550 |
| Code Size |
4479 Byte |
| Status |
AC |
| Exec Time |
2067 ms |
| Memory |
170752 KiB |
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
550 / 550 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
example0.txt, example1.txt, example2.txt |
| All |
000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, example0.txt, example1.txt, example2.txt |
| Case Name |
Status |
Exec Time |
Memory |
| 000.txt |
AC |
219 ms |
86456 KiB |
| 001.txt |
AC |
1040 ms |
166116 KiB |
| 002.txt |
AC |
1043 ms |
165692 KiB |
| 003.txt |
AC |
1016 ms |
167260 KiB |
| 004.txt |
AC |
1361 ms |
166252 KiB |
| 005.txt |
AC |
1337 ms |
166148 KiB |
| 006.txt |
AC |
1308 ms |
166160 KiB |
| 007.txt |
AC |
389 ms |
160288 KiB |
| 008.txt |
AC |
394 ms |
160308 KiB |
| 009.txt |
AC |
398 ms |
160364 KiB |
| 010.txt |
AC |
1503 ms |
169448 KiB |
| 011.txt |
AC |
1523 ms |
169768 KiB |
| 012.txt |
AC |
1537 ms |
169548 KiB |
| 013.txt |
AC |
1968 ms |
159764 KiB |
| 014.txt |
AC |
2067 ms |
170684 KiB |
| 015.txt |
AC |
1648 ms |
129212 KiB |
| 016.txt |
AC |
652 ms |
92508 KiB |
| 017.txt |
AC |
597 ms |
139916 KiB |
| 018.txt |
AC |
879 ms |
161320 KiB |
| 019.txt |
AC |
1178 ms |
105592 KiB |
| 020.txt |
AC |
921 ms |
124512 KiB |
| 021.txt |
AC |
673 ms |
95788 KiB |
| 022.txt |
AC |
527 ms |
118872 KiB |
| 023.txt |
AC |
836 ms |
116840 KiB |
| 024.txt |
AC |
666 ms |
102332 KiB |
| 025.txt |
AC |
1970 ms |
169472 KiB |
| 026.txt |
AC |
1706 ms |
169160 KiB |
| 027.txt |
AC |
1932 ms |
170752 KiB |
| 028.txt |
AC |
1900 ms |
169016 KiB |
| 029.txt |
AC |
1918 ms |
169476 KiB |
| 030.txt |
AC |
1918 ms |
168708 KiB |
| 031.txt |
AC |
1689 ms |
167148 KiB |
| 032.txt |
AC |
1753 ms |
170744 KiB |
| 033.txt |
AC |
1936 ms |
170412 KiB |
| 034.txt |
AC |
1916 ms |
169160 KiB |
| 035.txt |
AC |
1942 ms |
159568 KiB |
| example0.txt |
AC |
119 ms |
84736 KiB |
| example1.txt |
AC |
121 ms |
84760 KiB |
| example2.txt |
AC |
122 ms |
84608 KiB |