rt,求时间复杂度
#include<bits/stdc++.h>
using namespace std;
int a[500001],b[500001],c[500001],l[500001],r[500001];
stack<int> ccf1;
stack<int> ccf2;
int main()
{
int n,q,i,j,ans=0;
freopen("stack.in", "r", stdin);
freopen("stack.out", "w", stdout);
cin>>n>>q;
for(i=1;i<=n;i++)
{
cin>>a[i];
}
for(i=1;i<=n;i++)
{
cin>>b[i];
}
for(i=1;i<=q;i++)
{
cin>>l[i]>>r[i];
}
for(i=1;i<=q;i++)
{
for(j=l[i];j<=r[i];j++)
{
if(j==l[i])
{
ccf1.push(a[j]);
ccf2.push(b[j]);
ans++;
}
else
{
while(ccf1.empty()!=1&&ccf2.empty()!=1)
{
if(a[j]!=ccf1.top()&&b[j]<ccf2.top())
{
ccf1.push(a[j]) ;ccf2.push(b[j]);
break;
}
else
{
ccf1.pop();ccf2.pop();
}
}
if(ccf1.empty()==1&&ccf2.empty()==1)
{
ans++;
ccf1.push(a[j]) ;ccf2.push(b[j]);
}
}
}
c[i]=ans;
ans=0;
while(ccf1.empty()!=1&&ccf2.empty()!=1)
{
ccf1.pop();ccf2.pop();
}
}
for(i=1;i<=q;i++)
cout<<c[i]<<endl;
return 0;
}
```