Serega loves fun. However, everyone has fun in the unique manner. Serega has fun by solving query problems. One day Fedor came up with such a problem.
You are given an array a consisting of n positive integers and queries to it. The queries can be of two types:
Make a unit cyclic shift to the right on the segment from l to r (both borders inclusive). That is rearrange elements of the array in the following manner:
a[l], a[l + 1], …, a[r - 1], a[r] → a[r], a[l], a[l + 1], …, a[r - 1].
Count how many numbers equal to k are on the segment from l to r (both borders inclusive).
Fedor hurried to see Serega enjoy the problem and Serega solved it really quickly. Let’s see, can you solve it?
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of elements of the array. The second line contains n integers a[1], a[2], …, a[n] (1 ≤ a[i] ≤ n).
The third line contains a single integer q (1 ≤ q ≤ 105) — the number of queries. The next q lines contain the queries.
As you need to respond to the queries online, the queries will be encoded. A query of the first type will be given in format: 1 l’i r’i. A query of the second type will be given in format: 2 l’i r’i k’i. All the number in input are integer. They satisfy the constraints: 1 ≤ l’i, r’i, k’i ≤ n.
To decode the queries from the data given in input, you need to perform the following transformations:
li = ((l’i + lastans - 1) mod n) + 1; ri = ((r’i + lastans - 1) mod n) + 1; ki = ((k’i + lastans - 1) mod n) + 1.
Where lastans is the last reply to the query of the 2-nd type (initially, lastans = 0). If after transformation li is greater than ri, you must swap these values.
Output
For each query of the 2-nd type print the answer on a single line.
题意
有一组数据
现有两种操作:
1:将(l,r)内数据整体向右移动(最右边的移动到最左边)
2:查询(l,r)内为k的数有多少个
题解:将n分块为m个区间,即每次查询时遍历区间复杂度为sqrt(n)量级,使用双端队列将每次更新复杂度降低为常数,同时方便记录数字,结构体映射储存特定数字的个数。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <deque>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5, M = 350;
int n, Q, op, a[N];
int block, pos[N], m;
struct _Blo { int l, r, c[N]; }b[M];//分块,每一块l,r表示包含位置,c表示块内每个数的个数
deque<int> q[M];//双端队列保存每个块内的数,方便循环更改
deque<int>::iterator it;
void ini() {
//黄金比例分块,效率高
block = pow(n, 0.618);//n的0.618次方,m表示分块的个数
m = (n - 1) / block + 1;
for (int i = 1; i <= n; i++) pos[i] = (i - 1) / block + 1;//将n个数分配到m块内
//初始化每一块的信息
for (int i = 1; i <= m; i++) b[i].l = (i - 1) * block + 1, b[i].r = i * block;
b[m].r = n;
}
struct Block {
void Set(int x) {//更新包含x的块内的信息
for (int i = b[x].l; i <= b[x].r; i++) q[x].push_back(a[i]), b[x].c[a[i]]++;
}
void Cha(int l, int r) {//块内循环推移
int pl = pos[l], pr = pos[r];
int f = l - b[pl].l, g = r - b[pr].l, x = q[pr][g];
if (pl == pr) {
q[pr].erase(q[pr].begin() + g); q[pl].insert(q[pl].begin() + f, x);
}
else {
q[pl].insert(q[pl].begin() + f, x); b[pl].c[x]++;
q[pr].erase(q[pr].begin() + g); b[pr].c[x]--;
for (int i = pl; i < pr; i++) {
int x = q[i].back();
q[i].pop_back(); b[i].c[x]--;
q[i + 1].push_front(x); b[i + 1].c[x]++;
}
}
}
int Que(int l, int r, int k) {//查询,类似线段树,但是复杂度为sqrt(n)*logn块内遍历
int pl = pos[l], pr = pos[r];
int f = l - b[pl].l, g = r - b[pr].l;
int ans = 0;
if (pl == pr)
for (int i = f; i <= g; i++) ans += (q[pl][i] == k);
else {
for (int i = f; i < (int)q[pl].size(); i++) ans += q[pl][i] == k;
for (int i = 0; i <= g; i++) ans += q[pr][i] == k;
for (int i = pl + 1; i < pr; i++) ans += b[i].c[k];
}
return ans;
}
}B;
int main() {
//freopen("in","r",stdin);
cin>>n; ini(); //初始化分块
for (int i = 1; i <= n; i++) cin>>a[i];
for (int i = 1; i <= m; i++) B.Set(i);//初始化每块的值
cin>>Q; int last = 0, l, r;
while (Q--) {
cin >> op;
int col,cor;
cin >> col >> cor;
l = (col + last - 1) % n + 1, r = (cor + last - 1) % n + 1;
if (l > r) swap(l, r);
if (op == 1) B.Cha(l, r);
else {
int i;
cin >> i;
int k = (i + last - 1) % n + 1;//printf("k %d\n",k);
last = B.Que(l, r, k);
printf("%d\n", last);
}
}
}