只会打暴力
#include<iostream>
using namespace std;
const int N=500001;
int n,q,a[N],b[N],l[N],r[N],stack[N],stack2[N],top,cnt;
bool bol;
void push(int x,int y){
if(top<500001){
top++;
stack[top]=x;
stack2[top]=y;
return;
}
}
void pop(){
if(top>0){
top--;
return;
}
}
void empty(){
for(int i=1;i<=n;i++){
stack[i]=0,stack2[i]=0;
}
cnt=0,top=0,bol=0;
}
int main(){
freopen("stack.in","r",stdin);
freopen("stack.out","w",stdout);
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++){
cin>>b[i];
}
for(int i=1;i<=q;i++){
empty();
cin>>l[i]>>r[i];
for(int j=l[i];j<=r[i];j++){
if(top==0){
push(a[j],b[j]);
cnt++;
}else if(top!=0){
while(top!=0){
if(stack[top]!=a[j]&&stack2[top]>b[j]){
push(a[j],b[j]);
bol=1;
break;
}else{
pop();
}
}
if(!bol||top==0){
push(a[j],b[j]);
cnt++;
}
}
}
cout<<cnt<<'\n';
}
fclose(stdin);
fclose(stdout);
return 0;
}