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>
#define dbg() cerr <<
#define name(x) (#x) << ": " << (x) << ' ' <<
using namespace std;
const int INF = (int)2e9;
template<class T, class F = function<T(const T&, const T&)>>
struct Fenwick {
int n;
T def;
F f;
vector<T> tree;
Fenwick(int n_, T def_, F f_) : n(n_), def(def_), f(f_), tree(n + 1, def) {}
inline int lsb(int x) {
return x & -x;
}
void Update(int pos, T val) {
for (++pos; pos <= n; pos += lsb(pos)) {
tree[pos] = f(tree[pos], val);
}
}
T Query(int pos) {
T ret = def;
for (++pos; pos; pos -= lsb(pos)) {
ret = f(ret, tree[pos]);
}
return ret;
}
};
void GetSmallBig(const vector<int> &p, vector<int> &small, vector<int> &big) {
int n = p.size();
Fenwick<pair<int, int>> mn(n, {-1, -1}, [](pair<int, int> a, pair<int, int> b) {
return max(a, b);
});
Fenwick<pair<int, int>> mx(n, {INF, -1}, [](pair<int, int> a, pair<int, int> b) {
return min(a, b);
});
for (int i = 0; i < n; ++i) {
small[i] = mn.Query(p[i]).second;
big[i] = mx.Query(n - p[i] - 1).second;
mn.Update(p[i], make_pair(p[i], i));
mx.Update(n - p[i] - 1, make_pair(p[i], i));
}
}
bool Match(int k, int i, const vector<int> &p,
const vector<int> &small, const vector<int> &big) {
bool ret = true;
ret &= big[k] == -1 || p[i] < p[i - k + big[k]];
ret &= small[k] == -1 || p[i] > p[i - k + small[k]];
return ret;
}
void GetPi(const vector<int> &p, vector<int> &pi,
const vector<int> &small, const vector<int> &big) {
pi[0] = 0;
int k = 0;
for (int i = 1; i < (int)p.size(); ++i) {
while (k != 0 && !Match(k, i, p, small, big)) k = pi[k - 1];
if (Match(k, i, p, small, big)) ++k;
pi[i] = k;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
vector<int> p(n);
for (int i = 0; i < n; ++i) {
int s; cin >> s; --s;
p[s] = i;
}
vector<int> small(n, -1), big(n, -1), pi(n);
GetSmallBig(p, small, big);
GetPi(p, pi, small, big);
vector<int> text(m);
for (int i = 0; i < m; ++i) {
cin >> text[i];
}
vector<int> ans;
int k = 0;
for (int i = 0; i < m; ++i) {
while (k != 0 && !Match(k, i, text, small, big)) k = pi[k - 1];
if (Match(k, i, text, small, big)) ++k;
if (k == n) {
ans.emplace_back(i - n + 1);
k = pi[k - 1];
}
}
cout << ans.size() << endl;
for (int &x : ans) {
cout << 1 + x << ' ';
}
cout << endl;
}
# | 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... |