#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
/*const int MOD = ;
inline int lgput(int a, int b) {
int ans = 1;
while(b > 0) {
if(b & 1) ans = (1LL * ans * a) % MOD;
b >>= 1;
a = (1LL * a * a) % MOD;
}
return ans;
}
inline void mod(int &x) {
if(x >= MOD)
x -= MOD;
}
inline void add(int &x, int y) {
x += y;
mod(x);
}
inline void sub(int &x, int y) {
x += MOD - y;
mod(x);
}
inline void mul(int &x, int y) {
x = (1LL * x * y) % MOD;
}
inline int inv(int x) {
return lgput(x, MOD - 2);
}*/
/*int fact[], invfact[];
inline void prec(int n) {
fact[0] = 1;
for(int i = 1; i <= n; i++) {
fact[i] = (1LL * fact[i - 1] * i) % MOD;
}
invfact[n] = lgput(fact[n], MOD - 2);
for(int i = n - 1; i >= 0; i--) {
invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD;
}
}
inline int comb(int n, int k) {
if(n < k) return 0;
return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD;
}*/
using namespace std;
const int MAXN = (int) 1e6;
const int B = 1e9 + 7;
ull pw[MAXN + 1];
pair <int, int> h[MAXN + 1];
int nrm[MAXN + 1], n;
struct Node {
ull hsh;
int cnt;
}aint[4 * MAXN + 1];
inline Node refresh(Node &a, Node &b) {
return {a.hsh * pw[b.cnt] + b.hsh, a.cnt + b.cnt};
}
void add(int nod, int left, int right, int pos, int val) {
if(left == right) {
aint[nod] = {val, 1};
return ;
}
int mid = (left + right) / 2;
if(pos <= mid) add(2 * nod, left, mid, pos, val);
else add(2 * nod + 1, mid + 1, right, pos, val);
aint[nod] = refresh(aint[2 * nod], aint[2 * nod + 1]);
}
void del(int nod, int left, int right, int pos) {
if(left == right) {
aint[nod] = {0, 0};
return ;
}
int mid = (left + right) / 2;
if(pos <= mid) del(2 * nod, left, mid, pos);
else del(2 * nod + 1, mid + 1, right, pos);
aint[nod] = refresh(aint[2 * nod], aint[2 * nod + 1]);
}
int main() {
#if 0
ifstream cin("A.in");
ofstream cout("A.out");
#endif
int i, m;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> m >> n;
pw[0] = 1;
for(i = 1; i <= n; i++) {
pw[i] = pw[i - 1] * B;
}
ull hsh = 0;
for(i = 1; i <= m; i++) {
int x;
cin >> x;
hsh += x * pw[m - i];
}
for(i = 1; i <= n; i++) {
cin >> h[i].first;
h[i].second = i;
}
sort(h + 1, h + n + 1);
for(i = 1; i <= n; i++) {
nrm[h[i].second] = i;
}
for(i = 1; i < m; i++) {
add(1, 1, n, nrm[i], i);
}
ull sum = 0;
for(i = 0; i < m; i++) {
sum += pw[i];
}
vector <int> sol;
for(i = 1; i <= n - m + 1; i++) {
add(1, 1, n, nrm[i + m - 1], i + m - 1);
if(aint[1].hsh - sum * (i - 1) == hsh) {
sol.push_back(i);
}
del(1, 1, n, nrm[i]);
}
cout << sol.size() << "\n";
for(auto it : sol) {
cout << it << " ";
}
return 0;
}
Compilation message
mat.cpp: In function 'void add(int, int, int, int, int)':
mat.cpp:81:28: warning: narrowing conversion of 'val' from 'int' to 'long long unsigned int' inside { } [-Wnarrowing]
aint[nod] = {val, 1};
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
1912 KB |
Output is correct |
2 |
Correct |
12 ms |
1912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
1912 KB |
Output is correct |
2 |
Correct |
12 ms |
1936 KB |
Output is correct |
3 |
Correct |
4 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
13176 KB |
Output is correct |
2 |
Correct |
109 ms |
13512 KB |
Output is correct |
3 |
Correct |
184 ms |
13688 KB |
Output is correct |
4 |
Correct |
172 ms |
13644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
139 ms |
14072 KB |
Output is correct |
2 |
Correct |
177 ms |
13816 KB |
Output is correct |
3 |
Correct |
120 ms |
13864 KB |
Output is correct |
4 |
Correct |
120 ms |
15872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
14052 KB |
Output is correct |
2 |
Correct |
117 ms |
13608 KB |
Output is correct |
3 |
Correct |
133 ms |
13640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
677 ms |
54636 KB |
Output is correct |
2 |
Correct |
856 ms |
65536 KB |
Output is correct |
3 |
Correct |
730 ms |
56052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
625 ms |
52984 KB |
Output is correct |
2 |
Correct |
962 ms |
59512 KB |
Output is correct |
3 |
Correct |
834 ms |
65536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
702 ms |
54772 KB |
Output is correct |
2 |
Correct |
643 ms |
65536 KB |
Output is correct |
3 |
Correct |
826 ms |
62200 KB |
Output is correct |
4 |
Correct |
540 ms |
65536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
708 ms |
62132 KB |
Output is correct |
2 |
Correct |
745 ms |
63088 KB |
Output is correct |
3 |
Correct |
307 ms |
30712 KB |
Output is correct |
4 |
Correct |
606 ms |
65536 KB |
Output is correct |