PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000024. Tổng theo phép toán bit (Enjoy Sum with Operations)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng theo phép toán bit (Enjoy Sum with Operations)

Enjoy Sum with Operations

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an array a1,a2,…,ana_1,a_2,\ldots,a_n of nonnegative integers, indexed from 11 through nn. A bitwise operator OpOp combines two integers into another integer.

Process qq commands. Command 1 x i assigns ai=xa_i=x, entirely replacing its former value. Command 2 Op l r considers every pair of distinct indices (i,j)(i,j) satisfying l≤i<j≤rl\le i<j\le r, computes the bitwise operation ai Op aja_i\ Op\ a_j, and sums all such results. Op is one of the uppercase keywords XOR (exclusive OR), OR, and AND.

Every unordered pair is counted exactly once; (i,j)(i,j) and (j,i)(j,i) do not count as different pairs. If l=rl=r, no such pair exists, and the answer must be 00. Queries use the values resulting from previous assignments.

Input

  • Line 1: n q gives array length and command count.
  • Line 2: a_1 a_2 ... a_n gives the initial values in index order.
  • Next qq lines: each line contains one of the following commands, in execution order:
    • 1 x i: set ai=xa_i=x.
    • 2 Op l r: sum Op over all index pairs i<ji<j in [l,r][l,r].

Output

For each 2 Op l r command, print the sum of Op over all position pairs l≤i<j≤rl\le i<j\le r. If l=rl=r, print 00. Print each answer on its own line.

Subtasks

Subtask 1 (20 points):

  • 1≤n≤241\le n\le 24.
  • 1≤q≤301\le q\le 30.
  • 1≤ai<1081\le a_i<10^8 for every initial element.
  • 0≤x≤1080\le x\le 10^8 for assignment values.
  • 1≤i≤n1\le i\le n.
  • 1≤l≤r≤n1\le l\le r\le n.
  • Op is one of XOR, OR, or AND.

Subtask 2 (30 points):

  • 1≤n≤12001\le n\le 1200.
  • 1≤q≤16001\le q\le 1600.
  • 1≤ai<1081\le a_i<10^8 for every initial element.
  • 0≤x≤1080\le x\le 10^8 for assignment values.
  • 1≤i≤n1\le i\le n.
  • 1≤l≤r≤n1\le l\le r\le n.
  • Op is one of XOR, OR, or AND.

Subtask 3 (50 points):

  • 1≤n≤1000001\le n\le 100000.
  • 1≤q≤1000001\le q\le 100000.
  • 1≤ai<1081\le a_i<10^8 for every initial element.
  • 0≤x≤1080\le x\le 10^8 for assignment values.
  • 1≤i≤n1\le i\le n.
  • 1≤l≤r≤n1\le l\le r\le n.
  • Op is one of XOR, OR, or AND.

Examples

Input

4 3
10 20 30 40
2 OR 1 3
1 0 1
2 OR 1 3

Output

90
80

Explanation

The initial array is [10, 20, 30, 40]. The two queries only consider their specified intervals.

  1. 2 OR 1 3: the values in [1,3][1,3] are 10,20,3010,20,30. The three pairs are (1,2)(1,2), (1,3)(1,3), and (2,3)(2,3). Their bitwise OR values are 10 OR 20=3010\,\mathrm{OR}\,20=30, 10 OR 30=3010\,\mathrm{OR}\,30=30, and 20 OR 30=3020\,\mathrm{OR}\,30=30. Thus 30+30+30=9030+30+30=90, so print 90.

  2. 1 0 1: assign 00 to position 11. The array becomes [0, 20, 30, 40]. The last element 4040 is outside the queried interval.

  3. 2 OR 1 3: the three pairwise values now are 0 OR 20=200\,\mathrm{OR}\,20=20, 0 OR 30=300\,\mathrm{OR}\,30=30, and 20 OR 30=3020\,\mathrm{OR}\,30=30. Their sum is 20+30+30=8020+30+30=80, so print 80.

Each pair is counted only once, producing outputs 90 and 80.