This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ii pair<int,int>
#define fi first
#define se second
const int maxn=1e6+7;
int p[maxn];
int a[maxn];
ii pre[maxn];int h[maxn];
ii nex[maxn];int g[maxn];
int kmp[maxn];
int n,m;
void init(){
vector<int>pos(n+2,0);
for(int i=1;i<=n;i++)pos[p[i]]=i;
for(int i=1;i<=n;i++)swap(pos[i],p[i]);
for(int i=1;i<=n;i++){
nex[i]={i+1,pos[i+1]};
pre[i]={i-1,pos[i-1]};
}
for(int i=n;i>=1;i--){
int k=p[i];
h[i]=pre[k].se;
g[i]=nex[k].se;
pre[nex[k].fi]=pre[k];
nex[pre[k].fi]=nex[k];
}
}
bool can(int i,int j,int u,int v,bool type){
if(i>n)return 0;
if(type==0){
if(u&&p[j-i+u]>p[j])return 0;
if(v&&p[j-i+v]<p[j])return 0;
}
else{
if(u&&a[j-i+u]>a[j])return 0;
if(v&&a[j-i+v]<a[j])return 0;
}
return 1;
}
main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>p[i];
init();
for(int i=1;i<=m;i++)cin>>a[i];
// for(int i=1;i<=n;i++){
// cout<<h[i]<<" "<<g[i]<<"\n";
// }
kmp[1]=0;
for(int i=2,j=0;i<=n;i++){
while(j&&!can(j+1,i,h[j+1],g[j+1],0))j=kmp[j];
if(can(j+1,i,h[j+1],g[j+1],0))j++;
kmp[i]=j;
}
// for(int i=1;i<=n;i++)cout<<kmp[i]<<"\n";
// cout<<8<<" "<<can(2,7,h[2],g[2],1)<<"\n";
vector<int>ans;
for(int i=1,j=0;i<=m;i++){
while(j&&!can(j+1,i,h[j+1],g[j+1],1)){
j=kmp[j];
}
// if(i==7)cout<<j<<"\n";
if(can(j+1,i,h[j+1],g[j+1],1))j++;
// if(i==7)cout<<j<<"\n";
if(j==n)ans.push_back(i-n+1);
}
cout<<ans.size()<<"\n";
for(int x:ans)cout<<x<<" ";
return 0;
}
Compilation message (stderr)
mat.cpp:42:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
42 | main()
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |