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 mid ((L+R)>>1)
const int N = 1e6 + 5;
int Base = 1e6 + 3;
int a[N], b[N], p[N], pw[N], seg[N<<2], lazy[N<<2], fen[N], Hash, n, m;
vector <int> ans, vec;
void apply(int id, int val) {
seg[id] = seg[id] * pw[val], lazy[id] += val;
}
void shift(int id) {
if (lazy[id] == 0) return ;
apply(id<<1, lazy[id]), apply(id<<1|1, lazy[id]);
lazy[id] = 0;
}
void Set(int l, int val, int L = 0, int R = m, int id = 1) {
if (l + 1 <= L || R <= l) return ;
if (l <= L && R <= l + 1) {
seg[id] = val;
return ;
}
shift(id);
Set(l, val, L, mid, id<<1);
Set(l, val, mid, R, id<<1|1);
seg[id] = seg[id<<1] + seg[id<<1|1];
}
int Get(int l, int r, int L = 0, int R = m, int id = 1) {
if (r <= L || R <= l) return 0;
if (l <= L && R <= r) return seg[id];
shift(id);
return Get(l, r, L, mid, id<<1) + Get(l, r, mid, R, id<<1|1);
}
void add_fen(int idx, int val) {
for (idx++; idx < N; idx += idx&(-idx)) fen[idx] += val;
}
int get_fen(int idx) {
int res = 0;
for (idx++; idx; idx -= idx&(-idx)) res += fen[idx];
return res;
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
pw[0] = 1;
for (int i = 1; i < N; i++) pw[i] = pw[i - 1] * Base;
// --------------------------------------------------------------
cin >> n >> m;
for (int i = 0; i < n; i++) {int x; cin >> x; a[x - 1] = i;}
for (int i = 0; i < m; i++) cin >> b[i], vec.push_back(b[i]);
sort(vec.begin(), vec.end());
for (int i = 0; i < m; i++) b[i] = lower_bound(vec.begin(), vec.end(), b[i]) - vec.begin(), p[b[i]] = i;
for (int i = 0; i < n; i++) Hash = Hash * Base + a[i];
// --------------------------------------------------------------
int CurHash = 0;
for (int i = 0; i < m; i++) {
if (i >= n) {
CurHash -= get_fen(b[i - n] - 1) * pw[n - 1] + Get(b[i - n] + 1, m);
add_fen(b[i - n], -1), Set(b[i - n], 0);
}
apply(1, 1);
CurHash = CurHash * Base + get_fen(b[i]) + Get(b[i], m);
add_fen(b[i], 1), Set(b[i], 1);
if (i >= n - 1) if (CurHash == Hash) ans.push_back(i - n + 2);
}
cout << ans.size() << "\n";
for (int i : ans) cout << i << " ";
cout << "\n";
return 0;
}
# | 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... |