이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
#define rep(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define f first
#define s second
const int MAXN = 1e6 + 5;
int m, n;
int p[MAXN];
int a[MAXN * 2];
int up[MAXN];
int down[MAXN];
int pf[MAXN * 2];
int t[MAXN * 4];
void update(int v, int tl, int tr, int p, int x) {
// if (tr - tl == 1) {
// t[v] += x;
// return;
// }
// int tm = tl + (tr - tl) / 2;
// if (p < tm) {
// update(v * 2 + 1, tl, tm, p, x);
// } else {
// update(v * 2 + 2, tm, tr, p, x);
// }
// t[v] = t[v * 2 + 1] + t[v * 2 + 2];
t[p] += x;
}
int find_last(int v, int tl, int tr, int l, int r) {
// if (r <= tl || tr <= l) return -1;
// int tm = tl + (tr - tl) / 2;
// if (l <= tl && tr <= r) {
// if (t[v] == 0) return -1;
// if (tr - tl == 1) return tl;
// if (t[v * 2 + 2] != 0) return find_last(v + 2 + 2, tm, tr, l, r);
// return find_last(v * 2 + 1, tl, tm, l, r);
// }
// int res = find_last(v * 2 + 2, tm, tr, l, r);
// if (res == -1) res = find_last(v * 2 + 1, tl, tm, l, r);
// return res;
for (int i = r - 1; i >= l; --i) {
if (t[i]) return i;
}
return -1;
}
int find_first(int v, int tl, int tr, int l, int r) {
// if (r <= tl || tr <= l) return -1;
// int tm = tl + (tr - tl) / 2;
// if (l <= tl && tr <= r) {
// if (t[v] == 0) return -1;
// if (tr - tl == 1) return tl;
// if (t[v * 2 + 1] != 0) return find_last(v + 2 + 1, tl, tm, l, r);
// return find_last(v * 2 + 2, tm, tr, l, r);
// }
// int res = find_last(v * 2 + 1, tl, tm, l, r);
// if (res == -1) res = find_last(v * 2 + 2, tm, tr, l, r);
// return res;
for (int i = l; i < r; ++i) {
if (t[i]) return i;
}
return -1;
}
void calc() {
fill(up, up + m, -1);
fill(down, down + m, -1);
rep(i, m) {
update(0, 0, m, a[i], 1);
}
for (int i = m - 1; i >= 0; --i) {
update(0, 0, m, a[i], -1);
up[i] = find_first(0, 0, m, a[i], m);
down[i] = find_last(0, 0, m, 0, a[i]);
}
rep(i, m) {
if (up[i] != -1) up[i] = p[up[i]];
if (down[i] != -1) down[i] = p[down[i]];
}
// for (int i = 0; i < m; ++i) {
// auto it = s.lower_bound({a[i], 0});
// if (it != s.end()) {
// up[i] = it->s;
// }
// if (it != s.begin()) {
// down[i] = prev(it)->s;
// }
// s.insert({a[i], i});
// }
}
int main() {
FAST_IO;
cin >> m >> n;
rep(i, m) {
cin >> p[i];
p[i]--;
}
rep(i, m) {
a[p[i]] = i;
}
a[m] = -1;
rep(i, n) {
cin >> a[m + 1 + i];
}
calc();
pf[0] = 0;
for (int i = 1; i < n + m + 1; ++i) {
if (i == m) {
pf[i] = 0;
continue;
}
int j = pf[i - 1];
while (j > 0) {
if (j == m) {
j = pf[j - 1];
continue;
}
int u = up[j];
int d = down[j];
if ((d != -1 && a[i - j + d] > a[i]) || (u != -1 && a[i - j + u] < a[i])) {
j = pf[j - 1];
continue;
}
pf[i] = j + 1;
break;
}
if (j == 0) pf[i] = 1;
}
// rep(i, n + m + 1) {
// cout << a[i] << ' ';
// }
// cout << endl;
// rep(i, m) {
// cout << down[i] << ' ';
// }
// cout << endl;
// rep(i, m) {
// cout << up[i] << ' ';
// }
// cout << endl;
// rep(i, n + m + 1) {
// cout << pf[i] << ' ';
// }
// cout << endl;
vector<int> ans;
for (int i = m + 1 + m - 1; i < n + m + 1; ++i) {
if (pf[i] == m) {
ans.push_back(i - m + 1 - m - 1);
}
}
cout << ans.size() << endl;
for (auto it : ans) {
cout << it + 1 << ' ';
}
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... |