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

#SRD0000111. AquaMoon và những củ khoai tây (AquaMoon and Potatoes)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

AquaMoon và những củ khoai tây (AquaMoon and Potatoes)

AquaMoon and Potatoes

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

AquaMoon has three integer arrays aa, bb, and cc, each of length nn. All three arrays are indexed from 11 to nn, and every element is an integer between 11 and nn, inclusive.

To organize her potato farm, AquaMoon performs mm operations. Only array aa may change; arrays bb and cc remain fixed. There are two types of operations:

  • Type 1 k x: Assign ak=xa_k=x, replacing its previous value. This is an assignment, not an increment.

  • Type 2 r: Consider all triples of integer indices (i,j,k)(i,j,k) satisfying

    1≤i<j<k≤r1\le i<j<k\le r

    and

    bai=aj=cak.b_{a_i}=a_j=c_{a_k}.

    Every such triple contributes exactly one potato. Count all qualifying triples using the current state of array aa.

In the expression baib_{a_i}, the value aia_i is used as an index into array bb; similarly, cakc_{a_k} denotes the entry of cc at index aka_k. For example, if ai=3a_i=3, then bai=b3b_{a_i}=b_3.

Operations must be processed in their given order. After an assignment, subsequent queries use the updated value until that position is assigned again.

Input

  • The first line contains two integers n,mn,m: the length of each array and the number of operations, respectively.
  • The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n, describing the initial array aa.
  • The third line contains nn integers b1,b2,…,bnb_1,b_2,\ldots,b_n, describing array bb.
  • The fourth line contains nn integers c1,c2,…,cnc_1,c_2,\ldots,c_n, describing array cc.
  • Each of the next mm lines specifies one operation in exactly one of these forms:
    • 1 k x: assign ak=xa_k=x;
    • 2 r: count qualifying triples among positions 11 through rr.

Output

For each operation of type 2, print one integer on its own line: the number of qualifying triples at the time of that operation. Print the answers in the same order as the type 2 operations in the input.

Subtasks

  • Subtask 1 (10 points): 1≤n≤251\le n\le25; 1≤m≤401\le m\le40; 1≤ai,bi,ci≤n1\le a_i,b_i,c_i\le n for every 1≤i≤n1\le i\le n; each operation is either 1 k x (1≤k,x≤n1\le k,x\le n) or 2 r (1≤r≤n1\le r\le n); at least one operation is of type 2.
  • Subtask 2 (20 points): 1≤n≤3 0001\le n\le3\,000; 1≤m≤3 0001\le m\le3\,000; 1≤ai,bi,ci≤n1\le a_i,b_i,c_i\le n for every 1≤i≤n1\le i\le n; each operation is either 1 k x (1≤k,x≤n1\le k,x\le n) or 2 r (1≤r≤n1\le r\le n); at least one operation is of type 2.
  • Subtask 3 (30 points): 1≤n≤40 0001\le n\le40\,000; 1≤m≤12 0001\le m\le12\,000; 1≤ai,bi,ci≤n1\le a_i,b_i,c_i\le n for every 1≤i≤n1\le i\le n; each operation is either 1 k x (1≤k,x≤n1\le k,x\le n) or 2 r (1≤r≤n1\le r\le n); at least one operation is of type 2.
  • Subtask 4 (40 points): 1≤n≤200 0001\le n\le200\,000; 1≤m≤50 0001\le m\le50\,000; 1≤ai,bi,ci≤n1\le a_i,b_i,c_i\le n for every 1≤i≤n1\le i\le n; each operation is either 1 k x (1≤k,x≤n1\le k,x\le n) or 2 r (1≤r≤n1\le r\le n); at least one operation is of type 2.

Examples

Example 1

Input

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

Output

3
0
2

Explanation

Initially, a=[1,2,3,4,5]a=[1,2,3,4,5], b=[2,3,4,5,1]b=[2,3,4,5,1], and c=[5,1,2,3,4]c=[5,1,2,3,4].

  • Operation 1, 2 5: Exactly three triples satisfy the condition: (1,2,3)(1,2,3), (2,3,4)(2,3,4), and (3,4,5)(3,4,5). For instance, for (1,2,3)(1,2,3), ba1=b1=2b_{a_1}=b_1=2, a2=2a_2=2, and ca3=c3=2c_{a_3}=c_3=2. For the other two triples, the three values are all equal to 33 and 44, respectively. The answer is 33.
  • Operation 2, 1 2 3: Array aa becomes [1,3,3,4,5][1,3,3,4,5]. Assignments produce no output.
  • Operation 3, 2 4: None of the triples within the first four positions of the updated array satisfy the equality, so the answer is 00.
  • Operation 4, 2 5: The valid triples are (2,4,5)(2,4,5) and (3,4,5)(3,4,5). Both have bai=b3=4b_{a_i}=b_3=4, a4=4a_4=4, and ca5=c5=4c_{a_5}=c_5=4. The answer is 22.

Example 2

Input

4 6
1 1 1 1
1 1 1 1
1 1 1 1
2 4
1 2 2
2 4
1 2 1
2 3
2 4

Output

4
2
1
4

Explanation

Both bb and cc contain only the value 11, so bai=cak=1b_{a_i}=c_{a_k}=1 at every position. Thus, the remaining condition is aj=1a_j=1.

  • Initially, all four entries of aa are 11. Every selection of three increasing indices from {1,2,3,4}\{1,2,3,4\} is valid, giving (43)=4\binom{4}{3}=4 triples.
  • After assigning a2=2a_2=2, only positions 1,3,41,3,4 contain 11. For the query on the first four positions, the middle index must be j=3j=3. The valid triples are (1,3,4)(1,3,4) and (2,3,4)(2,3,4), so the answer is 22.
  • After restoring a2=1a_2=1, the query 2 3 has exactly one valid triple, (1,2,3)(1,2,3), so it outputs 11.
  • The final query 2 4 again has all four valid triples.