이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
class SegmentTree:
def __init__(self, n):
self.n = n
self.tree = [0] * (4 * n)
def update(self, node, start, end, idx):
if start == end:
self.tree[node] ^= 1 # Toggle the lamp state
else:
mid = (start + end) // 2
if start <= idx <= mid:
self.update(2 * node + 1, start, mid, idx)
else:
self.update(2 * node + 2, mid + 1, end, idx)
self.tree[node] = self.tree[2 * node + 1] + self.tree[2 * node + 2]
def query(self, node, start, end, left, right):
if left > end or right < start:
return 0
if left <= start and right >= end:
return self.tree[node]
mid = (start + end) // 2
left_sum = self.query(2 * node + 1, start, mid, left, right)
right_sum = self.query(2 * node + 2, mid + 1, end, left, right)
return left_sum + right_sum
n, q = map(int, input().split())
lamp_states = list(map(int, input().strip()))
# Initialize the segment tree
segment_tree = SegmentTree(n)
# Process events
for _ in range(q):
event = input().split()
if event[0] == "toggle":
i = int(event[1]) - 1
segment_tree.update(0, 0, n - 1, i)
else: # Event is a query
a, b = map(int, event[1:])
time = segment_tree.query(0, 0, n - 1, a - 1, b - 2)
print(time)
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |