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 ll long long
#define ld long double
#define endl "\n"
const int N = 1e6 + 5;
int pos[N],pi[N*2];
int a[N],b[N],le[N],ri[N],ra[N],n,m;
vector<int> nw;
bool check(int idx,int mid) {
if(mid > n) return false;
return nw[idx] >= nw[idx - le[mid]] && nw[idx] <= nw[idx - ri[mid]];
}
struct BIT_MAX{
vector<int> bit;
int n;
void init(int len) {
n = len;
bit.resize(n + 1,0);
}
void add(int pos) {
for(int i = pos;i <= n;i += i&(-i)) {
bit[i] = max(bit[i],pos);
}
}
int query(int pos) {
int res =0;
while(pos > 0) {
res = max(res,bit[pos]);
pos -= pos&(-pos);
}
return res;
}
};
struct BIT_MIN{
vector<int> bit;
int n;
void init(int len) {
n = len;
bit.resize(n + 1,INT_MAX);
}
void add(int pos) {
int x = pos;
while(pos > 0) {
bit[pos] = min(bit[pos],x);
pos -= pos&(-pos);
}
}
int query(int pos) {
int res = INT_MAX;
for(int i = pos;i <= n;i += i&(-i)) {
res = min(res,bit[i]);
}
return res;
}
};
BIT_MAX front;
BIT_MIN back;
signed main() {
// freopen("in.txt","r",stdin);
// freopen("some.txt","w",stdout);
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i = 1;i <= n;i++) {
cin>>ra[i];
a[ra[i]] = i;
}
for(int i = 1;i <= m;i++) {
cin>>b[i];
}
set<int> st;
front.init(n);
back.init(n);
for(int i = 1;i <= n;i++) {
int l = front.query(a[i]);
if(l == 0) {
le[i] = 0;
} else le[i] = i - ra[l];
int r = back.query(a[i]);
if(r == INT_MAX) {
ri[i] = 0;
} else {
ri[i] = i - ra[r];
}
front.add(a[i]);
back.add(a[i]);
}
for(int i= 1;i <= n;i++) {
nw.push_back(a[i]);
}
nw.push_back(-1);
for(int i = 1;i <= m;i++) {
nw.push_back(b[i]);
}
pi[0] = 0;
for(int i = 1;i < nw.size();i++) {
if(i == n) {
pi[i] = 0;
continue;
}
int j = pi[i - 1];
while(j > 0 && !check(i,j + 1)) {
j = pi[j - 1];
}
if(check(i,j + 1)) {
j++;
}
pi[i] = j;
}
vector<int> pos;
for(int i = n + 1;i < nw.size();i++) {
if(pi[i] == n) {
pos.push_back(i - n*2 + 1);
}
}
cout<<pos.size()<<endl;
for(auto u : pos) cout<<u<<" ";
}
Compilation message (stderr)
mat.cpp: In function 'int main()':
mat.cpp:109:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for(int i = 1;i < nw.size();i++) {
| ~~^~~~~~~~~~~
mat.cpp:124:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
124 | for(int i = n + 1;i < nw.size();i++) {
| ~~^~~~~~~~~~~
# | 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... |