# Flipping Coins

### Problem Statement :

```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:

1) Flip all coins numbered between A and B inclusive. This is represented by the command "0 A B"

2) 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 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 :

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

Constraints :

1 <= N <= 100000
1 <= Q <= 100000
0 <= A <= B <= N - 1```

### Solution :

```                            ```Solution in C :

#include <stdio.h>
#include <stdlib.h>

int t[20000002] = { 0 }, lz[20000002] = { 0 };

void update(int node, int s, int e, int l, int r) {
if (lz[node]) {
t[node] = (e - s + 1) - t[node];
if (s != e) {
lz[node << 1] ^= 1;
lz[(node << 1) | 1] ^= 1;
}
lz[node] = 0;
}
if (r < s || e < l || e < s)
return;
else if (l <= s && e <= r) {
t[node] = (e - s + 1) - t[node];
if (s != e) {
lz[node << 1] ^= 1;
lz[(node << 1) | 1] ^= 1;
}
return;
}
int m = s + (e - s) / 2;
update(node << 1, s, m, l, r);
update((node << 1) | 1, m + 1, e, l, r);
t[node] = t[node << 1] + t[(node << 1) | 1];
}

int query(int node, int s, int e, int l, int r) {
if (lz[node]) {
t[node] = (e - s + 1) - t[node];
if (s != e) {
lz[node << 1] ^= 1;
lz[(node << 1) | 1] ^= 1;
}	lz[node] = 0;
}
if (r < s || e < l || e < s)
return 0;
else if (l <= s && e <= r)
return t[node];
int p, q, m = s + (e - s) / 2;
p = query(node << 1, s, m, l, r);
q = query((node << 1) | 1, m + 1, e, l, r);
return p + q;
}
int main() {
int n, q, t, a, b;
scanf("%d%d", &n, &q);
while (q--) {
scanf("%d%d%d", &t, &a, &b);
a++;
b++;
if (t) {
printf("%d\n", query(1, 1, n, a, b));
}
else {
update(1, 1, n, a, b);
}
}
return 0;
}```
```

```                        ```Solution in C++ :

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
ll seg[400010];
ll lazy[400010];
void up(int in,int l,int r,int sl,int sr)
{
if(lazy[in]!=0)
{
seg[in]=(sr-sl+1)-seg[in];
if(sl!=sr){
lazy[2*in]=!lazy[2*in];
lazy[2*in+1]=!lazy[2*in+1];
}
lazy[in]=0;
}
if(r<sl || l>sr || l>r)return;
if(sl>=l && sr<=r)
{
seg[in]=(sr-sl+1)-seg[in];
if(sl!=sr){
lazy[2*in]=!lazy[2*in];
lazy[2*in+1]=!lazy[2*in+1];
}
return;
}
int mid=sl+(sr-sl)/2;
up(in*2,l,r,sl,mid);
up(in*2+1,l,r,mid+1,sr);
seg[in]=seg[in*2]+seg[in*2+1];
}
ll sum(int in,int l,int r,int sl,int sr)
{
if(lazy[in]!=0)
{
seg[in]=(sr-sl+1)-seg[in];
if(sl!=sr){
lazy[2*in]=!lazy[2*in];
lazy[2*in+1]=!lazy[2*in+1];
}
lazy[in]=0;
}
if(r<sl || l>sr || l>r)return 0;
if(sl>=l && sr<=r)
return seg[in];
int mid=sl+(sr-sl)/2;
return sum(in*2,l,r,sl,mid) + sum(in*2+1,l,r,mid+1,sr);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,q;
cin>>n>>q;
while(q--)
{
int x,l,r;
cin>>x>>l>>r;
if(x==0)
up(1,l,r,0,n-1);
else{
cout<<sum(1,l,r,0,n-1)<<"\n";
}
}
}```
```

```                        ```Solution in Java :

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
InputStream inputStream=System.in;
PrintWriter out=new PrintWriter(System.out);
int n=sc.nextInt();
int q=sc.nextInt();
BitSet bs=new BitSet(n);
StringBuffer sb=new StringBuffer();
for(int i=0;i<q;i++)
{
int x=sc.nextInt();
int a=sc.nextInt();
int b=sc.nextInt();
if(x==1)
{
sb.append(bs.get(a,b+1).cardinality()+"\n");
}
else
{
bs.flip(a,b+1);
}
}
System.out.println(sb.toString());
out.flush();
}
public StringTokenizer tokenizer;

tokenizer = null;
}

public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

public int nextInt() {
return Integer.parseInt(next());
}
}
}```
```

```                        ```Solution in Python :

import numpy as np
n,q=map(int,input().split())

dp=np.zeros(n,dtype=bool)
while q>0:
zero,a,b=map(int,input().split())
if zero:print(np.count_nonzero(dp[a:b+1]))
else:dp[a:b+1]=~dp[a:b+1]
q-=1```
```

## View More Similar Problems

The median M of numbers is defined as the middle number after sorting them in order if M is odd. Or it is the average of the middle two numbers if M is even. You start with an empty number list. Then, you can add numbers to the list, or remove existing numbers from it. After each add or remove operation, output the median. Input: The first line is an integer, N , that indicates the number o

## Maximum Element

You have an empty sequence, and you will be given N queries. Each query is one of these three types: 1 x -Push the element x into the stack. 2 -Delete the element present at the top of the stack. 3 -Print the maximum element in the stack. Input Format The first line of input contains an integer, N . The next N lines each contain an above mentioned query. (It is guaranteed that each

## Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

## Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

## Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

## Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle