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;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
struct BIT {
vector<int> tr;
BIT (int sz) : tr(sz + 1, INT_MIN) {}
int p (int k) { return k & -k; }
void update (int k, int val) {
for (; k < tr.size(); k += p(k))
tr[k] = max(tr[k], val);
}
int query (int k, int ans = INT_MIN) {
for (; k; k -= p(k)) ans = max(ans, tr[k]);
return ans;
}
};
const int mn = 2e6 + 6;
int p[mn], s[mn], lb[mn], rb[mn], z[mn];
bool ok (int i, int nxt) {
// check if [i..nxt] is isomorphic to [0..p]
// knowing that [i..nxt-1] is already isomorphic to [0..p-1]
int p = nxt - i;
if (lb[p] != -1) {
int dist = p - lb[p];
if (nxt - dist < 0 || s[nxt - dist] > s[nxt]) return 0; // s[nxt - dist] must be < s[nxt]
}
if (rb[p] != -1) {
int dist = p - rb[p];
if (nxt - dist < 0 || s[nxt - dist] < s[nxt]) return 0; // s[nxt - dist] must be > s[nxt]
}
return 1;
}
void zFunc (int n) {
int x = 0, y = 0;
for (int i = 1; i < n; i++) {
z[i] = max(0, min(y - i + 1, z[i - x]));
while (i + z[i] < n && ok(i, i + z[i]))
x = i, z[i]++, y++;
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
int len = n + m;
for (int i = 1; i <= n; i++) {
cin >> p[i];
s[--p[i]] = i;
}
//s[n] = INT_MAX;
for (int i = n; i < n + m; i++) cin >> s[i];
BIT lt(n + 1), rt(n + 1);
for (int i = 0; i < n; i++) {
int lval = lt.query(s[i]), rval = rt.query(n - s[i] + 1);
lb[i] = (lval == INT_MIN ? -1 : p[lval]);
rb[i] = (rval == INT_MIN ? -1 : p[-rval]);
lt.update(s[i], s[i]);
rt.update(n - s[i] + 1, -s[i]);
}
zFunc(len);
vector<int> ans;
for (int i = n; i < len; i++)
if (z[i] >= n) ans.push_back(i - n + 1);
cout << ans.size() << "\n";
for (int u : ans) cout << u << " ";
return 0;
}
Compilation message (stderr)
mat.cpp: In member function 'void BIT::update(int, int)':
mat.cpp:20:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for (; k < tr.size(); k += p(k))
| ~~^~~~~~~~~~~
# | 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... |