이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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));
}
}
// ffs set ul asta consuma minim 32 bytes / int ?!?!
// void GetSmallBig(const vector<int> &p, vector<int> &small, vector<int> &big) {
// auto Comp = [&](int a, int b) {
// return p[a] < p[b];
// };
// set<int, decltype(Comp)> vals(Comp);
// vals.emplace(0);
// for (int i = 1; i < (int)p.size(); ++i) {
// auto it = vals.emplace(i).first;
// if (it != vals.begin()) small[i] = *prev(it);
// if (next(it) != vals.end()) big[i] = *next(it);
// }
// }
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... |