#include <bits/stdc++.h>
using namespace std;
#ifdef KURUMI
#include "algo/debug.h"
#endif
#define endl '\n'
#define fi first
#define se second
#define sz(v) (int)v.size()
#define all(v) v.begin(), v.end()
#define filter(v) v.resize(unique(all(v)) - v.begin())
#define dbg(x) "[" #x << " = " << x << "]"
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T1, typename T2> T2 rand(T1 l, T2 r) {
return uniform_int_distribution<T2>(l, r)(rng);
}
template<typename T1, typename T2> T2 wrand(T1 l, T2 r, int seed) {
if(seed == 0) return rand(l, r);
else return (seed > 0 ? max(rand(l, r), wrand(l, r, seed - 1)) : min(rand(l, r), wrand(l, r, seed + 1)));
}
template<typename T> bool maximize(T &a, T b) {
if(a < b) {
a = b;
return true;
}else return false;
}
template<typename T> bool minimize(T &a, T b) {
if(a > b) {
a = b;
return true;
}else return false;
}
typedef long long ll;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tp3;
namespace Modulo {
const int MOD = (119 << 23) + 1;
int add(int x, int y) {
return (x + y) % MOD;
}
int sub(int x, int y) {
return ((x - y) % MOD + MOD) % MOD;
}
int prod(int x, int y) {
return 1LL * x * y % MOD;
}
}
const int N = 1e6 + 3;
int n, m;
int a[N], b[N];
vector<int> cps;
void input() {
cin >> n >> m;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
for(int i = 1; i <= m; i++) {
cin >> b[i];
cps.push_back(b[i]);
}
sort(all(cps)); filter(cps);
for(int i = 1; i <= m; i++) {
b[i] = lower_bound(all(cps), b[i]) - cps.begin() + 1;
}
}
namespace subtask_1 {
bool approve() {
return n <= 100 && m <= 1000;
}
void execute() {
vector<int> answer;
for(int i = 1; i <= m - n + 1; i++) {
int j = i + n - 1;
bool passed = true;
for(int l = i; l <= j; l++) {
for(int r = l + 1; r <= j; r++) {
if(a[l - i + 1] < a[r - i + 1]) {
if(b[l] > b[r]) {
passed = false;
break;
}
}
}
if(!passed) break;
}
if(passed) answer.emplace_back(i);
}
cout << sz(answer) << endl;
for(int p : answer)
cout << p << " ";
}
}
namespace subtask_3 {
bool approve() {
return true;
}
using namespace Modulo;
const int BASE = 1e6 + 3;
int pwr[N], id[N];
struct SegmentTree {
struct Node {
int val, cnt;
friend Node pull(Node a, Node b) {
if(!a.cnt) return b;
if(!b.cnt) return a;
Node c;
c.val = add(prod(a.val, pwr[b.cnt]), b.val);
c.cnt = a.cnt + b.cnt;
return c;
}
};
vector<Node> tr;
SegmentTree (int n) : tr(n << 2 | 3) {}
void update(int id, int l, int r, int p, int val, int cnt) {
if(l == r) return tr[id] = {val, cnt}, void();
int mid = (l + r) >> 1;
if(p <= mid) update(id << 1, l, mid, p, val, cnt);
else update(id << 1 | 1, mid + 1, r, p, val, cnt);
tr[id] = pull(tr[id << 1], tr[id << 1 | 1]);
}
};
void execute() {
for(int i = 1; i <= n; i++)
id[a[i]] = i;
int pattern = 0, andOne = 0; pwr[0] = 1;
for(int i = 1; i <= n; i++) {
pwr[i] = prod(pwr[i - 1], BASE);
andOne = add(prod(andOne, BASE), 1);
pattern = add(prod(pattern, BASE), a[i]);
}
SegmentTree seg(m); vector<int> answer;
for(int i = 1; i <= m; i++) {
seg.update(1, 1, m, b[i], i, 1);
// cout << b[i] << " " << i << endl;
if(i >= n) {
if(seg.tr[1].val == pattern) answer.push_back(i - n + 1);
pattern = add(pattern, andOne);
seg.update(1, 1, m, b[i - n + 1], 0, 0);
}
}
cout << sz(answer) << endl;
for(int p : answer) cout << p << " ";
}
}
void process() {
// if(subtask_1::approve()) return subtask_1::execute();
if(subtask_3::approve()) return subtask_3::execute();
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#define task "MOHINHGIA"
if(fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int testcase = 1; // cin >> testcase;
for(int i = 1; i <= testcase; i++) {
input();
process();
}
cerr << "Saa, watashtachi no deeto hajimemashou" << endl;
cerr << "Atarashii kiseki wo koko kara hajimeru shining place nee mou ichido kimi to..." << endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
mat.cpp: In function 'int main()':
mat.cpp:196:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
196 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mat.cpp:197:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
197 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |