( ext{Description})
( ext{Solution})
注意到题目有个很特殊的条件:(a,b) 都是排列。也就是说 (a,b) 里相同的数是一一对应的。
我们可以搞一个奇怪的映射:(c_i) 表示 (b) 数组的第 (i) 个数出现在 (a) 数组的哪个位置,用主席树维护,在第 (i) 位多插入一个 (c_i)(相当于 (rt) 的下标是 (b) 数组第 (i) 个,内部是 (a) 数组的下标)。
答案就是 ( ext{Query}(rt[r_2],l_1,r_1)- ext{Query}(rt[l_2-1],l_1,r_1))。
其实不是排列也是可做的,只是需要换一种问法:在 (a) 的 ([l_1,r_1]) 和 (b) 的 ([l_2,r_2]) 同时出现的数有多少个?(可以重复)
( ext{Code})
#include <cstdio>
#define rep(i,_l,_r) for(register signed i=(_l),_end=(_r);i<=_end;++i)
#define fep(i,_l,_r) for(register signed i=(_l),_end=(_r);i>=_end;--i)
#define erep(i,u) for(signed i=head[u],v=to[i];i;i=nxt[i],v=to[i])
#define efep(i,u) for(signed i=Head[u],v=to[i];i;i=nxt[i],v=to[i])
#define print(x,y) write(x),putchar(y)
template <class T> inline T read(const T sample) {
T x=0; int f=1; char s;
while((s=getchar())>'9'||s<'0') if(s=='-') f=-1;
while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+(s^48),s=getchar();
return x*f;
}
template <class T> inline void write(const T x) {
if(x<0) return (void) (putchar('-'),write(-x));
if(x>9) write(x/10);
putchar(x%10^48);
}
template <class T> inline T Max(const T x,const T y) {if(x>y) return x; return y;}
template <class T> inline T Min(const T x,const T y) {if(x<y) return x; return y;}
template <class T> inline T fab(const T x) {return x>0?x:-x;}
template <class T> inline T gcd(const T x,const T y) {return y?gcd(y,x%y):x;}
template <class T> inline T lcm(const T x,const T y) {return x/gcd(x,y)*y;}
#include <iostream>
using namespace std;
const int maxn=1e6+5;
int n,ans,m,cnt,pos[maxn],b[maxn],rt[maxn];
struct Tree {
int son[2],siz;
} t[maxn*25];
int f(int x) {return (x-1+ans)%n+1;}
void Insert(int pre,int &o,int l,int r,int k) {
o=++cnt;
t[o]=t[pre]; ++t[o].siz;
if(l==r) return;
int mid=l+r>>1;
if(k<=mid) Insert(t[pre].son[0],t[o].son[0],l,mid,k);
else Insert(t[pre].son[1],t[o].son[1],mid+1,r,k);
}
int Query(int o,int l,int r,int L,int R) {
if(l>R||r<L) return 0;
if(l>=L&&r<=R) return t[o].siz;
int mid=l+r>>1;
return Query(t[o].son[0],l,mid,L,R)+Query(t[o].son[1],mid+1,r,L,R);
}
int main() {
n=read(9);
rep(i,1,n) pos[read(9)]=i;
rep(i,1,n) b[i]=pos[read(9)],Insert(rt[i-1],rt[i],1,n,b[i]);
m=read(9);
int a,b,c,d;
while(m--) {
a=f(read(9)),b=f(read(9)),c=f(read(9)),d=f(read(9));
if(a>b) swap(a,b);
if(c>d) swap(c,d);
print(ans=Query(rt[d],1,n,a,b)-Query(rt[c-1],1,n,a,b),'
');
++ans;
}
return 0;
}