Magic Suitcase
Time limit1.5sMemory limit1024 MB
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 magical animals and the same number of compartments. The compartments are numbered from 1 to . Each compartment holds exactly one animal. Every creature has a danger level . 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 through is called a segment.
During minutes, two types of events happened:
- Two non-overlapping segments of equal length trade places. Segment to is swapped with segment to . Formally, for each from to , the animal in compartment swaps with the animal in compartment .
- A zoologist asks how many animals in segment to have a danger level with .
Input
The first line contains and , the number of compartments and the number of queries (; ).
The second line contains the sequence of length , the danger levels of the animals ().
Each of the next lines starts with , the query type ().
If , four numbers , , , follow, describing a swap. The segments do not overlap and have equal length (, ).
If , four numbers , , , follow, describing a zoologist's query (, ).
Output
For each query of type 2, print the number of animals that match.