There are N coins kept on the table, numbered from 0 to N - 1. Initally, each coin is kept tails up. You have to perform two types of operations :
1) Flip all coins numbered between A and B. This is represented by the command "0 A B"
2) Answer how many coins numbered between A and B 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
#include<iostream>
int main()
{
unsigned int N,Q,temp,op,A,B,count;
scanf ("%u%u",&N,&Q);
bool *coin=new bool [N];
for(temp=0;temp<N;temp++)
coin[temp]=true;
for(int temp1=0;temp1<Q;temp1++)
{
count=0;
scanf ("%u%u%u",&op,&A,&B);
switch(op)
{
case 0:
for(temp=A;temp<=B;temp++)
{
if(coin[temp]==true)
coin[temp]=false;
else
coin[temp]=true;
}
break;
case 1:
for(temp=A;temp<=B;temp++)
{
if(coin[temp]==false)
count++;
}
printf ("\n%u\n",count);
break;
default:
break;
}
}
delete coin;
return 0;
}
The following code gives the correct output but it is very slow please help me suggesting a different algorithmic thank you in advance.