atcoder#ABC265G. [ABC265G] 012 Inversion

[ABC265G] 012 Inversion

Score : 600600 points

Problem Statement

You are given a sequence A=(A1,,AN)A=(A_1,\ldots,A_N) of length NN. Each element is 00, 11, or 22. Process QQ queries in order. Each query is of one of the following kinds:

  • 1 L R: print the inversion number of the sequence (AL,,AR)(A_L,\ldots,A_R).
  • 2 L R S T U: for each ii such that LiRL\leq i \leq R, if AiA_i is 00, replace it with SS; if AiA_i is 11, replace it with TT; if AiA_i is 22, replace it with UU.
What is the inversion number? The inversion number of a sequence B = (B_1, \ldots, B_M) is the number of pairs of integers (i, j) (1 \leq i < j \leq M) such that B_i > B_j.

Constraints

  • 1N1051 \leq N \leq 10^5
  • 0Ai20 \leq A_i \leq 2
  • 1Q1051\leq Q\leq 10^5
  • In each query, 1LRN1\leq L \leq R \leq N.
  • In each query of the second kind, 0S,T,U20\leq S,T,U \leq 2.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN QQ

A1A_1 A2A_2 \ldots ANA_N

Query1\rm Query_1

Query2\rm Query_2

\vdots

QueryQ\rm Query_Q

Queryi\rm Query_i denotes the ii-th query, which is in one of the following formats:

11 LL RR

22 LL RR SS TT UU

Output

Print the responses to the queries of the first kind in the given order, separated by newlines.

5 3
2 0 2 1 0
1 2 5
2 2 4 2 1 0
1 2 5
3
4

Initially, A=(2,0,2,1,0)A=(2,0,2,1,0).

  • In the 11-st query, print the inversion number 33 of (A2,A3,A4,A5)=(0,2,1,0)(A_2,A_3,A_4,A_5)=(0,2,1,0).
  • The 22-nd query makes A=(2,2,0,1,0)A=(2,2,0,1,0).
  • In the 33-rd query, print the inversion number 44 of (A2,A3,A4,A5)=(2,0,1,0)(A_2,A_3,A_4,A_5)=(2,0,1,0).
3 3
0 1 2
1 1 1
2 1 3 0 0 0
1 1 3
0
0