Bit operations

2.4

12 votes
Advanced Data Structures, Data Structures, Segment Trees
Problem

You are given an array of size n. Initially, all the elements are zero. You are provided with q queries and each query is one of the following types:

  • 1 L R X: For each element in the range [L,R] such as y, set y=y|X.
  • 2 L R X: For each element in the range [L,R] like y, set y=y&X.
  • 3 L R X: For each element in the range [L,R] like y, set y=yX.
  • 4 L R: Print the sum of the elements in range [L,R].
  • 5 L R: Print the XOR of the elements in range [L,R].

Input format

  • First line: n and q
  • Each of the next n lines: A query

Output format

For each query of type 4 or 5, print the answer.

Constraints

 1n, q, X2×1051LRn

Sample Input
10 20
5 1 5
2 1 2 5
3 3 6 9
4 3 4
4 6 8
4 6 9
3 3 7 8
5 2 7
2 3 7 5
4 6 7
4 2 6
5 10 10
3 9 9 7
3 1 7 4
4 2 6
3 2 9 1
3 6 9 8
4 9 10
3 1 7 6
5 7 10
Sample Output
0
18
9
9
8
1
4
0
24
14
12
Time Limit: 10
Memory Limit: 256
Source Limit:
Explanation

Here is the array after each query:

0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 9 9 9 9 0 0 0 0 
0 0 9 9 9 9 0 0 0 0 
0 0 9 9 9 9 0 0 0 0 
0 0 9 9 9 9 0 0 0 0 
0 0 1 1 1 1 8 0 0 0 
0 0 1 1 1 1 8 0 0 0 
0 0 1 1 1 1 0 0 0 0 
0 0 1 1 1 1 0 0 0 0 
0 0 1 1 1 1 0 0 0 0 
0 0 1 1 1 1 0 0 0 0 
0 0 1 1 1 1 0 0 7 0 
4 4 5 5 5 5 4 0 7 0 
4 4 5 5 5 5 4 0 7 0 
4 5 4 4 4 4 5 1 6 0 
4 5 4 4 4 12 13 9 14 0 
4 5 4 4 4 12 13 9 14 0 
2 3 2 2 2 10 11 9 14 0 
2 3 2 2 2 10 11 9 14 0 
 

Editor Image

?