Sum of Numbers - XOR and OR

 Coding Problem Keys 

 Sum of Numbers - XOR and OR 

 Problem Statement 

In the faculty of Informatics Engineering, the Teacher of Mathematics gave a lecture on bits and explained XOR and OR.

Then he decided to test the competence of his students by giving them this question. He will give the students an array A of N numbers and then give them Q queries.

Each query will consist of three numbers Typeᵢ, Lᵢ and Rᵢ. Each of the queries can be one of the following types.

    - If Typeᵢ is 1, then the student has to replace each Aᵢ with (Aᵢ | (Aᵢ - 1)) for every i between Lᵢ and Rᵢ.
    - If Typeᵢ is 2, this means that the student must replace each Aᵢ with (Aᵢ ^ (Aᵢ - 1)) for each i between the Lᵢ and Rᵢ.

After each query, the professor asks his students to calculate the sum of the numbers from array A. You need to find the sum of the answers to all the queries modulo 10^9+7.

Note

  • The sign | represents the logical OR.
  • The sign ^ represents the logical XOR.

 Function Description 

Complete the Solve function in the editor. It has the following Parameter(s):
Parameters
    - N (INTEGER) - The length of the array A.
    - Q (INTEGER) - Number of queries.
Return
    - The function must return an INTEGER denoting the sum of the numbers from array A.

 Constraints 

1 ≤ N ≤ 10⁵
1 ≤ Q ≤ 10⁵
1 ≤ A[i] ≤ 10⁵
1 ≤ Type[i] ≤ 2
1 ≤ L[i] ≤ N
1 ≤ R[i] ≤ N

 Input Format For Custom Testing 

The first line contains an integer, N, denoting the number of elements in A.
The next line contains an integer, Q, denoting the number of the query (the number of the elements in array Type, L and R).
Each line i of the N subsequent lines (where 0 ≤ i ≤ N) contains an integer describing A[i].
Each line i of the Q subsequent lines (where 0 ≤ i ≤ Q) contains an integer describing Type[i].
Each line i of the Q subsequent lines (where 0 ≤ i ≤ Q) contains an integer describing L[i].
Each line i of the Q subsequent lines (where 0 ≤ i ≤ Q) contains an integer describing R[i].

 Sample Input/Output 1 

 Input 

4
1
1
2
3
4
1
2
3

 Output 

11

 Explanation 

The array will become [1, 3, 3, 4].

 Sample Input/Output 2 

 Input 

4
1
1
2
3
4
2
2
3

 Output 

9

 Explanation 

The array will become [1, 3, 1, 4].

 Sample Input/Output 3 

 Input 

4
2
5
4
8
6
1
2
1
2
3
2

 Output 

60

 Explanation 

After the first query, the array will become [5, 7, 15, 6], after the second query, the array will become [5, 1, 15, 6].

 Solution 

 Programming Language: Python 3 Language 

n=int(input())
q=int(input())
a=[];l=[];r=[];typ=[]
for i in range(n):
    a.append(int(input()))
for i in range(q):
    typ.append(int(input()))
for i in range(q):
    l.append(int(input()))
for i in range(q):
    r.append(int(input()))
b=[]
b.extend(a)
for i in range(q):
    if(typ[i]==1):
        for j in range(l[i]-1,r[i]):
            # Store OR of a[j] and a[j-1] in a[j]
        if(i!=q-1):
            b=[]
            b.extend(a)
    if(typ[i]==2):
        for k in range(l[i]-1,r[i]):
            # Store XOR of b[k] and b[k]-1 in b[k]
        if(i!=q-1):
            a=[]
            a.extend(b)
if 1 in typ and 2 in typ:
    # PRINT the sum of sum of a and sum of b
elif(1 not in typ):
    # PRINT the sum of b
elif(2 not in typ):
    # PRINT the sum of a

# Published By PKJCODERS

 (Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.) 

Comments

Popular Posts

Count Number of Occurrences of a Character

Print Prime Numbers from 2 to N

Cities and Population

K Palindrome Integers

Most Vowels in Row

Sum - Odd Number of Digits