Algorithm


problem link- https://www.spoj.com/problems/LITE/

LITE - Light Switching

 


Farmer John tries to keep the cows sharp by letting them play with intellectual toys. One of the larger toys is the lights in the barn.  Each of the N (2 <= N <= 100,000) cow stalls conveniently numbered 1..N has a colorful light above it.

At the beginning of the evening, all the lights are off. The cows control the lights with a set of N pushbutton switches that toggle the lights; pushing switch i changes the state of light i from off to on or from on to off.

The cows read and execute a list of M (1 <= M <= 100,000) operations expressed as one of two integers (0 <= operation <= 1).

The first kind of operation (denoted by a 0 command) includes two subsequent integers S_i and E_i (1 <= S_i <= E_i <= N) that indicate a starting switch and ending switch. They execute the operation by pushing each pushbutton from S_i through E_i inclusive exactly once.

The second kind of operation (denoted by a 1 command) asks the cows to count how many lights are on in the range given by two integers S_i and E_i (1 <= S_i <= E_i <= N) which specify the inclusive range in which the cows should count the number of lights that are on.

Help FJ ensure the cows are getting the correct answer by processing the list and producing the proper counts.

Input


Line 1: Two space-separated integers: N and M
Lines 2..M+1: Each line represents an operation with three space-separated integers: operation, S_i, and E_i

Output

Lines 1..number of queries: For each output query, print the count as an integer by itself on a single line.

Example

Input:
4 5
0 1 2
0 2 4
1 2 3
0 2 4
1 1 4

Output:
1
2

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
#define MOD                 1000000007LL
#define EPS                 1e-9
#define io                  ios_base::sync_with_stdio(false);cin.tie(NULL);

const int MAXN = 1e5+5;
int arr[MAXN], tree[4*MAXN], lazy[4*MAXN];
int n, q;

void update(int node, int start, int end, int l, int r){
	int left = node << 1, right = left + 1;
	if(lazy[node] != 0){
		tree[node] = (end-start+1) - tree[node];
		if(start != end){
			lazy[left] = 1-lazy[left];
			lazy[right] = 1-lazy[right];
		}
		lazy[node] = 0;
	}
	if(start > end || start > r || end < l)
		return;
	if(start >= l && end <= r){
		tree[node] = (end-start+1) - tree[node];
		if(start != end){
			lazy[left] = 1-lazy[left];
			lazy[right] = 1-lazy[right];
		}
		return;
	}
	int mid = (start + end) >> 1;
	update(left, start, mid, l, r);
	update(right, mid + 1, end, l, r);
	tree[node] = tree[left] + tree[right];
}

int query(int node, int start, int end, int l, int r){
	int left = node << 1, right = left + 1;
	if(lazy[node] != 0){
		tree[node] = (end-start+1) - tree[node];
		if(start != end){
			lazy[left] = 1-lazy[left];
			lazy[right] = 1-lazy[right];
		}
		lazy[node] = 0;
	}
	if(start > end || start > r || end < l)
		return 0;
	if(start >= l && end <= r){
		return tree[node];
	}
	int mid = (start + end) >> 1;
	return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r);
}

int main(){
	scanf("%d%d", &n, &q);
	while(q--){
		int type, l, r;
		scanf("%d%d%d", &type, &l, &r);
		l--;r--;
		if(type == 0){
			update(1, 0, n-1, l, r);
		}else{
			int ans = query(1, 0, n-1, l, r);
			printf("%d\n", ans);
		}
		// for(int i = 1;i <= 15; i++)
		// 	printf("Node: %d Value: %d\n", i, tree[i]);
		// printf("\n");
	}
	return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
4 5
0 1 2
0 2 4
1 2 3
0 2 4
1 1 4

Output

x
+
cmd
1
2
Advertisements

Demonstration


SPOJ Solution-Light Switching-Solution in C, C++, Java, Python

Previous
SPOJ Solution - Test Life, the Universe, and Everything - Solution in C, C++, Java, Python