#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define pi pair<int,int>
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
template<class T> using upq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int lwrbound(const vector<T>& a, const T& b, const int s = 0){return int(lower_bound(s + all(a), b) - a.begin());}
template<class T> int uprbound(const vector<T>& a, const T& b, const int s = 0){return int(upper_bound(s + all(a), b) - a.begin());}
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define ROF(i, a, b) for(int i = (a); i >= (b); i--)
#define sumof(x) accumulate(all(x), 0ll)
#define dbg(x) "[" << #x " = " << (x) << "]"
#define el "\n"
using ll = long long;
using ld = long double;
template<class T> bool ckmx(T& a, const T b){return (a < b ? a = b, true : false);}
template<class T> bool ckmn(T& a, const T b){return (a > b ? a = b, true : false);}
const int N = 1e6 + 5, base = 20, MOD = 2e9 + 11;
int mul(int x, int y){
return 1ll * x * y % MOD;
}
int add(int x, int y){
return (1ll * x + y < MOD ? 1ll * x + y : 1ll * x + y - MOD);
}
int sub(int x, int y){
return add(x - y, MOD);
}
// a[b[i]] is at the i_th position when sorted :lol
int n, m, a[N], b[N], pw[N];
struct Node{
int hsh, cnt;
Node(int hsh = 0, int cnt = 0): hsh(hsh), cnt(cnt) {}
friend Node operator + (const Node& l, const Node& r){
if(!l.cnt) return r;
if(!r.cnt) return l;
int nw_hsh = add(mul(l.hsh, pw[r.cnt]), r.hsh);
//cout << l.hsh <<" " << r.hsh << " " << nw_hsh << " " << r.cnt << " " << pw[r.cnt] << el;
return Node(nw_hsh, l.cnt + r.cnt);
}
};
Node st[N << 2];
void upd(int id, int l, int r, int x, int val){
if(l == r){
st[id] = Node(val, (val > 0));
}
else{
int m = l+r>>1;
if(x <= m)
upd(id << 1, l, m, x, val);
else upd(id << 1|1, m+1, r, x, val);
st[id] = st[id << 1] + st[id << 1|1];
}
}
void Main()
{
cin >> n >> m;
FOR(i, 1, n) cin >> a[i]; FOR(i, 1, m) cin >> b[i];
pw[0] = 1;
FOR(i, 1, m){
pw[i] = mul(pw[i - 1], base);
}
int hsh1 = 0, check = 0;
FOR(i, 1, n){
hsh1 = add(mul(hsh1, base), 1);
check = add(mul(check, base), a[i]);
}
vector<int> id(m + 1); iota(all(id), 0);
sort(1 + all(id), [&](int x, int y){return b[x] < b[y];});
FOR(i, 1, m) b[id[i]] = i;
vi ans;
FOR(i, 1, m){
upd(1, 1, m, b[i], i);
if(i - n >= 1){
upd(1, 1, m, b[i - n], 0);
check = add(check, hsh1);
}
if(i - n >= 0){
assert(st[1].cnt == n);
// cout << st[1].hsh << " " << check << el;
if(st[1].hsh == check){
ans.eb(i - n + 1);
}
}
}
cout << sz(ans) << el;
for(int& v : ans) cout << v <<" "; cout << el;
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP", "r", stdin);
freopen(name".OUT", "w", stdout);
}
int t = 1; while(t--) Main();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
mat.cpp: In function 'int32_t main()':
mat.cpp:128:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
128 | freopen(name".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mat.cpp:129:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
129 | freopen(name".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... |