恶心死了
link
弄了好长时间 用双端队列维护的时候一定要注意下标
我们在 块间操作的时候 它下表是会变的
int tmp = q[bl[b]][id[b]+1];
一定要+1 我调了一个半小时
注意他是强制在线的
#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
#define fi first
#define se second
#define pb push_back
#define inf 1<<62
#define endl "\n"
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define de_bug(x) cerr << #x << "=" << x << endl
#define all(a) a.begin(),a.end()
#define IOS std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fer(i,a,b) for(int i=a;i<=b;i++)
#define der(i,a,b) for(int i=a;i>=b;i--)
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
int n, m ;
int len;
int id[N];
int ans;
deque<int>q[N];
int bl[N];
int c[360][N];
int l[N], r[N];
void work(int &x) {
x = (x + ans - 1) % n + 1;
}
void change(int a, int b) {
if(bl[a] == bl[b]) {
int x = q[bl[a]][id[b]];
q[bl[a]].erase(q[bl[a]].begin() + id[b]);
q[bl[a]].insert(q[bl[a]].begin() + id[a], x);
} else {
for(int i = bl[a] ; i <= bl[b] - 1 ;) {
int x = q[i].back();
q[i].pop_back();
c[i][x]--;
i++;
q[i].push_front(x);
c[i][x]++;
}
int tmp = q[bl[b]][id[b]+1];
q[bl[b]].erase(q[bl[b]].begin() + id[b]+1);
c[bl[b]][tmp]--;
q[bl[a]].insert(q[bl[a]].begin() + id[a], tmp);
c[bl[a]][tmp]++;
}
}
int query(int a, int b, int k) {
int cnt = 0;
if(bl[a] == bl[b]) {
for(int i = id[a]; i <= id[b]; i++) {
if(q[bl[a]][i] == k)cnt++;
}
} else {
for(int i = bl[a] + 1; i <= bl[b] - 1; i++) {
cnt += c[i][k];
}
for(int i = id[a]; i <= id[r[bl[a] ]]; i++) {
if(q[bl[a]][i] == k)cnt++;
}
for(int i = id[l[ bl[b]]]; i <= id[b]; i++) {
if(q[bl[b]][i] == k)cnt++;
}
}
return cnt;
}
int tot;
void check(){
for(int i=1;i<=tot;i++){
for(int j=id [l[i]] ;j<=id[r[i]];j++ ){
cout<<q[i][j]<<" ";
}
}
cout<<endl;
}
void solve() {
cin >> n;
len = (int)sqrt(n);
tot = (n + len - 1) / len;
fer(i, 1, n)id[i] = (i - 1) % len, bl[i] = (i - 1) / len + 1;
fer(i, 1, n) {
int x;
cin >> x;
q[bl[i]].push_back(x);
c[bl[i]][x]++;
}
fer(i, 1, tot) {
l[i] = r[i - 1] + 1;
r[i] = l[i] + len - 1 ;
}
r[tot] = n;
/*for(int i=1;i<=tot;i++){
for(int j=id [l[i]] ;j<=id[r[i]];j++ ){
cout<<q[i][j]<<" ";
}
}*/
int q;
cin >> q;
while(q--) {
int op, a, b;
cin >> op >> a >> b;
work(a);
work(b);
if(a > b)swap(a, b);
// a++,b++;
// cout<<a<<" "<<b<<endl;
if(op == 1) change(a, b) ;//,check();
else {
int k;
cin >> k;
// cout<<ans<<"****"<<endl;
work(k);
// cout<<k<<"///"<< endl;
ans = query(a, b, k);
cout << ans << endl;
}
}
}
int main() {
IOS;
int _ = 1;
//cin>>_;
while( _-- )
solve();
}