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

#SRD0000048. Serega và các truy vấn thú vị (Serega and Fun)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Serega và các truy vấn thú vị (Serega and Fun)

Serega and Fun

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given an array a1,…,ana_1,\ldots,a_n with 1≤ai≤n1\le a_i\le n, process the following operations in their original order:

  • 1 l' r': cyclically rotate the inclusive interval [l,r][l,r] one position to the right.
  • 2 l' r' k': count the elements equal to kk in the inclusive interval [l,r][l,r].

Every supplied parameter l′,r′,k′l',r',k' is encoded. Let lastanslastans be the most recent answer to a type-2 operation, initially 00. Decode each supplied parameter z′z' as

z=((z′+lastans−1) mod n)+1.z=((z'+lastans-1)\bmod n)+1.

Decode only the parameters present in the operation; swap l,rl,r if l>rl>r after decoding. Update lastanslastans after every count operation. Queries must be handled online and in order.

Input

The first line contains nn, the second line contains the nn array values, and the third line contains qq. The next qq lines contain encoded 1 l' r' or 2 l' r' k' operations. All encoded parameters belong to [1,n][1,n].

Output

For every type-2 operation, print its count on a separate line.

Subtasks

  • Subtask 1 (10 points): 1≤n≤601\le n\le60; 1≤q≤601\le q\le60. Every query parameter is encoded using the latest answer.
  • Subtask 2 (20 points): 1≤n≤20001\le n\le2000; 1≤q≤30001\le q\le3000. Every query parameter is encoded using the latest answer.
  • Subtask 3 (25 points): 1≤n≤200001\le n\le20000; 1≤q≤250001\le q\le25000. Every query parameter is encoded using the latest answer.
  • Subtask 4 (45 points): 1≤n≤1000001\le n\le100000; 1≤q≤1000001\le q\le100000. Every query parameter is encoded using the latest answer.

Examples

Input

7
6 6 2 7 4 2 5
7
1 3 6
2 2 4 2
2 2 4 7
2 2 2 5
1 2 6
1 1 4
2 1 7 3

Output

2
1
0
0

Explanation

Initially lastans=0lastans=0. The second operation counts the value 22 in the rotated array and returns 22, so the following operation must decode its parameters using lastans=2lastans=2. The four count queries return 2, 1, 0, and 0 respectively; a rotation never changes lastans.