#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
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);
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
716 KB |
Output is correct |
2 |
Correct |
8 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
816 KB |
Output is correct |
2 |
Correct |
8 ms |
716 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
3728 KB |
Output is correct |
2 |
Correct |
64 ms |
4780 KB |
Output is correct |
3 |
Correct |
108 ms |
6396 KB |
Output is correct |
4 |
Correct |
110 ms |
6280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
4520 KB |
Output is correct |
2 |
Correct |
95 ms |
5604 KB |
Output is correct |
3 |
Correct |
81 ms |
6084 KB |
Output is correct |
4 |
Correct |
70 ms |
6916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
4592 KB |
Output is correct |
2 |
Correct |
71 ms |
5564 KB |
Output is correct |
3 |
Correct |
95 ms |
5684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
502 ms |
29300 KB |
Output is correct |
2 |
Correct |
853 ms |
44144 KB |
Output is correct |
3 |
Correct |
354 ms |
20520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
470 ms |
28864 KB |
Output is correct |
2 |
Correct |
585 ms |
22796 KB |
Output is correct |
3 |
Correct |
845 ms |
40348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
436 ms |
25408 KB |
Output is correct |
2 |
Correct |
366 ms |
33720 KB |
Output is correct |
3 |
Correct |
489 ms |
26472 KB |
Output is correct |
4 |
Correct |
580 ms |
42192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
408 ms |
26100 KB |
Output is correct |
2 |
Correct |
520 ms |
27032 KB |
Output is correct |
3 |
Correct |
226 ms |
13620 KB |
Output is correct |
4 |
Correct |
657 ms |
41532 KB |
Output is correct |