제출 #205955

#제출 시각아이디문제언어결과실행 시간메모리
205955parsa_mobedMatching (CEOI11_mat)C++14
92 / 100
2070 ms47328 KiB
#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 (R - L == 1) {seg[id] = val; return ;} shift(id); if (l < mid) Set(l, val, L, mid, id<<1); else 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(int idx) { for (idx++; idx < N; idx += idx&(-idx)) fen[idx]++; } void rem(int idx) { for (idx++; idx < N; idx += idx&(-idx)) fen[idx]--; } int get(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(b[i - n] - 1) * pw[n - 1] + Get(b[i - n] + 1, m); rem(b[i - n]), Set(b[i - n], 0); } apply(1, 1); CurHash = CurHash * Base + get(b[i]) + Get(b[i], m); add(b[i]), 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...