Under construction

Still being built. Things may change

Magic Suitcase

Time limit1.5sMemory limit1024 MB

Summary
Given n values, apply m swaps of equal-length segments and answer how many values in a segment lie between a and b.
Level

Hard8 of 10

Topics
Array, Sorting, Binary search, Divide and conquer
Solved
No attempts yet

Statement

Newt Scamander's suitcase holds nn magical animals and the same number of compartments. The compartments are numbered from 1 to nn. Each compartment holds exactly one animal. Every creature has a danger level tt. The animals are very active. They dislike staying in one place, so they keep swapping positions inside the suitcase.

A group of consecutive animals in compartments ll through rr is called a segment.

During mm minutes, two types of events happened:

  1. Two non-overlapping segments of equal length trade places. Segment l1l_1 to r1r_1 is swapped with segment l2l_2 to r2r_2. Formally, for each ii from l1l_1 to r1r_1, the animal in compartment ii swaps with the animal in compartment i−l1+l2i - l_1 + l_2.
  2. A zoologist asks how many animals in segment ll to rr have a danger level tt with a≤t≤ba \le t \le b.

Input

The first line contains nn and mm, the number of compartments and the number of queries (1≤n≤1061 \le n \le 10^6; 1≤m≤2⋅1031 \le m \le 2 \cdot 10^3).

The second line contains the sequence tit_i of length nn, the danger levels of the animals (1≤ti≤1091 \le t_i \le 10^9).

Each of the next mm lines starts with typetype, the query type (1≤type≤21 \le type \le 2).

If type=1type = 1, four numbers l1l_1, r1r_1, l2l_2, r2r_2 follow, describing a swap. The segments do not overlap and have equal length (1≤l1≤r1≤n1 \le l_1 \le r_1 \le n, 1≤l2≤r2≤n1 \le l_2 \le r_2 \le n).

If type=2type = 2, four numbers ll, rr, aa, bb follow, describing a zoologist's query (1≤l≤r≤n1 \le l \le r \le n, 1≤a≤b≤1091 \le a \le b \le 10^9).

Output

For each query of type 2, print the number of animals that match.

Examples2

  1. Example 1

    Input
    3 2
    1 2 3
    1 1 1 3 3
    2 2 3 1 2
    
    Expected output
    2
    
  2. Example 2

    Input
    6 5
    1 2 3 4 5 6
    1 1 3 4 6
    1 2 3 5 6
    2 3 6 2 5
    1 1 1 5 5
    2 2 5 2 6
    
    Expected output
    2
    3