There are 'n' coins kept on the table, numbered from 0 to 'n-1'. Initially, each coin is…
- User
There are 'n' coins kept on the table, numbered from 0 to 'n-1'. Initially, each coin is kept tails up. You have to perform two types of operations :
- Flip all coins numbered between A and B inclusive. This is represented by the command: 0 A B
- Answer how many coins numbered between A and B inclusive are heads up. This is represented by the command 1 A B Input: The first two lines contain two integers, N and Q.The first line after inputting N and Q has to be of form (1).Each of the next Q lines are of the form (2) as mentioned above. Output: Output 1 line for each of the queries of the form 2 containing the required answer for the corresponding query just after the query. 7 3 0 3 5 1 0 6 3 <-- this is the first output 1 3 4 2 <-- this is output
SAMPLE INPUT 7 3 0 3 5 1 0 6 1 3 4
SAMPLE OUTPUT 3 2
Assistant
class Node: def __init__(self, start, end): self.start = start self.end = end self.left = None self.right = None self.count = 0 self.flip = False def main(): import sys input = sys.stdin.read().split() ptr = 0 n = int(input[ptr]) ptr += 1 q = int(input[ptr]) ptr += 1 root = Node(0, n-1) for _ in range(q): op = int(input[ptr]) ptr +=1 a = int(input[ptr]) ptr +=1 b = int(input[ptr]) ptr +=1 if op == 0: flip_range(root, a, b) else: res = query_range(root, a, b) print(res) def flip_range(node, a, b): if node.end < a or node.start > b: return if a <= node.start and node.end <= b: node.count = (node.end - node.start + 1) - node.count if node.start < node.end: node.flip = not node.flip return if node.flip and node.start < node.end: if not node.left: mid = (node.start + node.end) // 2 node.left = Node(node.start, mid) node.right = Node(mid + 1, node.end) for child in [node.left, node.right]: child.count = (child.end - child.start + 1) - child.count child.flip = not child.flip node.flip = False if node.start < node.end: if not node.left: mid = (node.start + node.end) // 2 node.left = Node(node.start, mid) node.right = Node(mid + 1, node.end) flip_range(node.left, a, b) flip_range(node.right, a, b) node.count = node.left.count + node.right.count def query_range(node, a, b): if node.end < a or node.start > b: return 0 if a <= node.start and node.end <= b: return node.count if node.flip and node.start < node.end: if not node.left: mid = (node.start + node.end) // 2 node.left = Node(node.start, mid) node.right = Node(mid + 1, node.end) for child in [node.left, node.right]: child.count = (child.end - child.start + 1) - child.count child.flip = not child.flip node.flip = False res = 0 if node.start < node.end: if not node.left: mid = (node.start + node.end) // 2 node.left = Node(node.start, mid) node.right = Node(mid + 1, node.end) res = query_range(node.left, a, b) + query_range(node.right, a, b) return res if __name__ == "__main__": main()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.