IQUERY - Interesting queries

no tags 

Students of Computer Science department at NIT Durgapur were becoming extremely bored and frustrated due to too many classes ( weird but we had many classes ). As semesters were approaching, students were preparing hard to get good grades. Everyone was nervous about the Algorithms exam which is going to be held 2 days later. To add to their discomfort, one question came in the exam which took many one by surprise. The question was simple but everyone was told to optimize code as much as possible.

Now its your turn, and the question states that there is initially an array of N non-negative numbers (1-based indexing) and based on that array we have to run Q queries. Now we can do three things in each query. We have to sometimes calculate sum of product of all subsets of a number within a range, sometimes to calculate sum of bitwise AND of all subsets of a number, sometimes to update an element of an array. All the calculations are to be performed modulus (109+7). If we have to do sum of products then the query is given 'M', if bitwise AND then 'A' else if update then 'U'. For more details, see Explanation section .

Note: bitwise AND operator is defined as & in C, C++ and in most of the languages. For other programming languages see your language documentation.

Input

First line contains N, which denotes the number of elements in an array. Next line contains N integers denoting the number of integers in the array. Then Q denoting the number of queries and after that each line consists of 3 integers. If it is 'M' then we have to do product, if 'A' then bitwise AND and if 'U' then update. If it is 'M' or 'A' then it is followed by i and j which is the range of that array. If 'U' then we have the position and the value to be updated at that position.

Output

Corresponding to each query we have an output only to query of 'M' and 'A'. For update you don't have to print anything.

Constraints

1 ≤ N ≤ 105
1 ≤ A[i] ≤ 105
1 ≤ Q ≤ 105
1 ≤ i ≤ j ≤ 105
The element to be updated is of maximum value of 105.

Example

Input:
3
1 2 3
5
M 1 3
A 1 3
U 2 5
M 1 3
A 1 3

Output:
23
9
47
13

Explanation

For 1st query, we do 1 + 2 + 3 + 1*2 + 1*3 + 2*3 + 1*2*3 = 23

For 2nd query, it is 1 + 2 + 3 + 1&2 + 1&3 + 2&3 + 1&2&3 = 9

For 3rd query, it is update so array becomes 1 5 3

For 4th query, it is 1 + 5 + 3 + 1*5 + 1*3 + 3*5 + 1*3*5 = 47

For 5th query, it is 1 + 5 + 3 + 1&5 + 1&3 + 3&5 + 1&3&5 = 13


hide comments
mahilewets: 2017-08-19 11:30:37

After some solving efforts
WA#9 for a long time
Looks like first 8 tests are only for 'M' and 'U'
And ninth test is the first test with 'A'

suhash: 2014-08-31 13:27:19

Time limit is too strict. My solution passes on hackerrank in less than 0.5s. You should consider increasing the time limit.

Re : My solution is taking 0.5 seconds for per test file without using fast I/O . You have done unecessary calculations. Try avoiding them and as it is a cube cluster Time limit is well relaxed .

Last edit: 2014-08-31 14:20:29

Added by:Aayush Agarwal
Date:2014-08-30
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Own problem used for codecracker