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;
using ll = long long;
using db = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)(x).size()
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define sor(x) sort(all(x))
#define ft front()
#define bk back()
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define R0F(i, a) ROF(i, 0, a)
#define EACH(a, x) for (auto& a : x)
const int MOD = 1e9 + 7;
const int MX = 1e6 + 10;
const ll INF = 1e18;
int N, M, S[2 * MX]; vi allM; int place[MX]; int tree[MX];
int pref[2 * MX]; vi ans;
// Binary Indexed Tree ~ O(lg N)
void update(int ind, int val) {
while (ind <= M) { tree[ind] += val; ind += (ind & -ind); }
}
int query(int ind) {
int res = 0; while (ind > 0) { res += tree[ind]; ind -= (ind & -ind); } return res;
}
// Variation on KMP Algorithm with BIT ~ O(N lg N) ?
void genPref() {
int L = 1; // Left pointer of current suffix -- L is nondecreasing
FOR(i, 1, N + M + 1) {
if (S[i] == -1) { pref[i] = 0; while (L < i) update(S[L++], -1); L++; continue; }
int j = pref[i - 1]; if (j == N) j = pref[j - 1];
while (L < i - j) update(S[L++], -1);
while (j > 0 && query(S[i]) != place[j]) {
j = pref[j - 1]; while (L < i - j) update(S[L++], -1);
}
if (query(S[i]) == place[j]) j++; pref[i] = j; update(S[i], 1);
if (pref[i] == N) ans.pb(i - 2 * N + 1);
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N >> M; F0R(i, N) { int X; cin >> X; S[--X] = i + 1; }
F0R(i, N) { place[i] = query(S[i]); update(S[i], 1); }
S[N] = -1; F0R(i, N) update(S[i], -1);
FOR(i, N + 1, N + M + 1) cin >> S[i], allM.pb(S[i]);
sort(all(allM)); allM.erase(unique(all(allM)), allM.end());
FOR(i, N + 1, N + M + 1) S[i] = ub(all(allM), S[i]) - allM.bg();
//F0R(i, N + M + 1) cout << S[i] << "\n";
genPref(); cout << sz(ans) << "\n"; EACH(i, ans) cout << i << " ";
}
Compilation message (stderr)
mat.cpp: In function 'void genPref()':
mat.cpp:76:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
76 | if (query(S[i]) == place[j]) j++; pref[i] = j; update(S[i], 1);
| ^~
mat.cpp:76:43: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
76 | if (query(S[i]) == place[j]) j++; pref[i] = j; update(S[i], 1);
| ^~~~
# | 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... |