#include<bits/stdc++.h>
#define N 500005
using namespace std;
int pos=1,now,pd,n,m,ans[N],tree[N<<1];
struct node1
{
int a,b,id;
}c[N];
stack<node1>st;
int read()
{
int x=0;char ch=getchar();
while(ch>='0'&&ch<='9') {x=x*10+ch-48;ch=getchar();}
return x;
}
struct node
{
int val,id;
}a[N];
struct ask
{
int l,r,id;
}q[N];
int cmp1(node x,node y){return x.val<y.val;};
int cmp2(ask x,ask y){return x.l<y.l;}
int lowbit(int x){return x&(-x);}
void Add(int x)
{
for(int i=x;i<=n;i=i+lowbit(i)) tree[i]++;
}
int query(int x)
{
int s=0;
for(int i=x;i>=1;i=i-lowbit(i)) s+=tree[i];
return s;
}
int main()
{
n=read();m=read();
for(int i=1;i<=n;i++) c[i].a=read();
for(int i=1;i<=n;i++)
{
c[i].b=read();
c[i].id=i;
}
for(int i=1;i<=n;i++)
{
a[i].id=i;
if(st.empty()) st.push(c[i]);
else
{
while(!st.empty()&&(st.top().a==c[i].a||st.top().b<=c[i].b)) st.pop();
if(st.empty()) a[i].val=0;
else a[i].val=st.top().id;
st.push(c[i]);
}
}
for(int i=1;i<=m;i++)
{
q[i].l=read();q[i].r=read();q[i].id=i;
}
sort(a+1,a+n+1,cmp1);
sort(q+1,q+m+1,cmp2);
for(int i=1;i<=m;i++)
{
while(pos<=n&&a[pos].val<q[i].l)
{
Add(a[pos].id);
pos++;
}
ans[q[i].id]=query(q[i].r)-query(q[i].l-1);
}
for(int i=1;i<=m;i++) printf("%d\n",ans[i]);
return 0;
}