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+1);
for(int i=1;i<=n;i++){
pos[p[i]]=i;
pre[i]={i-1,p[i-1]};
nex[i]={i+1,p[i+1]};
}
for(int i=n;i>=1;i--){
h[i]=pre[pos[i]].se;
g[i]=nex[pos[i]].se;
pre[nex[pos[i]].fi]=pre[pos[i]];
nex[pre[pos[i]].fi]=nex[pos[i]];
}
}
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:40:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
40 | 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... |