이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define mid ((L+R)>>1)
const int N = 1e6 + 5;
int MOD = 1e9 + 7, 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] = (1ll * seg[id] * pw[val]) % MOD, 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];
if (seg[id] >= MOD) seg[id] -= MOD;
}
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);
int x = Get(l, r, L, mid, id<<1) + Get(l, r, mid, R, id<<1|1);
if (x >= MOD) x -= MOD;
return x;
}
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] = (1ll * pw[i - 1] * Base) % MOD;
// --------------------------------------------------------------
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 = (1ll * Hash * Base + a[i]) % MOD;
// --------------------------------------------------------------
int CurHash = 0;
for (int i = 0; i < m; i++) {
if (i >= n) {
CurHash -= (1ll * get_fen(b[i - n] - 1) * pw[n-1] + Get(b[i - n] + 1, m)) % MOD;
if (CurHash < 0) CurHash += MOD;
add_fen(b[i - n], -1), Set(b[i - n], 0);
}
apply(1, 1);
CurHash = (1ll * CurHash * Base + get_fen(b[i]) + Get(b[i], m)) % MOD;
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... |