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
n = int(input()) q = int(input()) tree = [0] * (4 * n) lazy = [False] * (4 * n) def flip(l, r): _flip(0, n - 1, l, r, 0) def _flip(node_l, node_r, l, r, node_index): if lazy[node_index]: if node_l != node_r: mid = (node_l + node_r) // 2 left = 2 * node_index + 1 right = 2 * node_index + 2 tree[left] = (mid - node_l + 1) - tree[left] lazy[left] = not lazy[left] tree[right] = (node_r - mid) - tree[right] lazy[right] = not lazy[right] lazy[node_index] = False if node_r < l or node_l > r: return if l <= node_l and node_r <= r: tree[node_index] = (node_r - node_l + 1) - tree[node_index] if node_l != node_r: lazy[node_index] = not lazy[node_index] return mid = (node_l + node_r) // 2 _flip(node_l, mid, l, r, 2 * node_index + 1) _flip(mid + 1, node_r, l, r, 2 * node_index + 2) tree[node_index] = tree[2 * node_index + 1] + tree[2 * node_index + 2] def query(l, r): return _query(0, n - 1, l, r, 0) def _query(node_l, node_r, l, r, node_index): if lazy[node_index]: if node_l != node_r: mid = (node_l + node_r) // 2 left = 2 * node_index + 1 right = 2 * node_index + 2 tree[left] = (mid - node_l + 1) - tree[left] lazy[left] = not lazy[left] tree[right] = (node_r - mid) - tree[right] lazy[right] = not lazy[right] lazy[node_index] = False if node_r < l or node_l > r: return 0 if l <= node_l and node_r <= r: return tree[node_index] mid = (node_l + node_r) // 2 return _query(node_l, mid, l, r, 2 * node_index + 1) + _query(mid + 1, node_r, l, r, 2 * node_index + 2) for _ in range(q): cmd = input().strip() while cmd == '': cmd = input().strip() A = int(input()) B = int(input()) if cmd == '0': flip(A, B) else: print(query(A, B))
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.