Menu Close

Find the final states of N lights…[Cprogm] FTC

Asraf has N lights, arranged in a line, with him.  A[i] denotes the initial state of ‘i’th light. 

He wants to toggle some lights, but he can only toggle the lights in ranges.  Toggling a light means changing the state of the light. That is, if the light was ON then after toggling it becomes OFF. 

He does this ‘range toggling’ Q times.  In the ‘i’th range toggling, he toggles the lights all the lights between Li and Ri lights (Li and Ri inclusive). 

You need to find the final states of all the N lights after these Q toggles.
A[i] = 1 means the light is ON and A[i] = 0 means the light is OFF

Input:
First line N and Q. 
Next line contains N integers showing the initial state of the bulbs. 
Next Q line contains the queries where 'i'th line contains Li and Ri.

Output:
Output N integers showing the final state of the bulbs.
#include <stdio.h>
int main()
{
  int lights[100001],n,q,i,a,b;
  scanf("%d %d",&n,&q);
  for(i=0;i<n;i++)
  {scanf("%d",&lights[i]);}
  while(q-->0)
  {scanf("%d %d",&a,&b);
  for(i=a-1;i<b;i++)
  lights[i]=!lights[i];
  }
  for(i=0;i<n;i++){
   printf("%d ",lights[i]);}

return 0;
}

INPUT_1:
7  2
1  1  0  1  0  0  0
5  7
1  3

OUTPUT:
0  0  1  1  1  1  1


INPUT_2:
10  4
0  1  0  0  0  1  1  0  0  1
3  6
2  5
7  10
4  8

OUTPUT:
0  0  0  1  1  1  1  0  1  0


INPUT_3:
5  2
0  0  1  1  1
1  3
3  5

OUTPUT:
1  1  1  0  0


ILLUSTRATION OUTPUT

Executed using python3