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:
The first line contains two integers, N and Q. Each of the next Q lines are either of the form “0 A B” or “1 A B” as mentioned above.
Output 1 line for each of the queries of the form “1 A B” containing the required answer for the corresponding query.
Sample Input :
4 7
1 0 3
0 1 2
1 0 1
1 0 0
0 0 3
1 0 3
1 3 3
Sample Output :
0
1
0
2
1
1 <= N <= 100000
1 <= Q <= 100000
0 <= A <= B <= N - 1
It is a Simple problem related to fast input/ output processing. Key to this problem is using sys.stdin module instead of the default input() function.
import numpy as np import sys # Using sys.stdin.readline() since it is fast N,M = map(int,sys.stdin.readline().split()) # Initializing the Numpy Array L = np.zeros(N, dtype=bool) for _ in range(M): p,x,y = map(int, sys.stdin.readline().split()) if p == 0: L[x:y+1] = ~L[x:y+1] else: print(L[x:y+1].sum())
This code uses bitwise operators for speedy calculations. Bitwise operators can make you competitive life easy. Take our Quick Guide on bitwise operators to know more.
In Python, the print() function is a fundamental tool for displaying output. While printing simple…
Python is a versatile programming language known for its simplicity and flexibility. When working on…
PDF (Portable Document Format) files are commonly used for sharing documents due to their consistent…
PDF (Portable Document Format) files are widely used for document exchange due to their consistent…
Python is a high-level programming language known for its simplicity and ease of use. However,…
Object-Oriented Programming (OOP), iterators, generators, and closures are powerful concepts in Python that can be…
This website uses cookies.