제출 #772296

#제출 시각아이디문제언어결과실행 시간메모리
772296NK_Matching (CEOI11_mat)C++17
0 / 100
43 ms65536 KiB
	// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'
#define sz(x) int(x.size())
#define pb push_back

using ll = long long;
template<class T> using V = vector<T>;


const ll MOD1 = ll(1e9)+7;
const ll MOD2 = 998244353;
const ll base = int(1e6) + 1007;

const int nax = 1 << 20;

ll POW1[2*nax], POW2[2*nax];

struct T {
	ll H1, H2; int len;
	bool operator==(T a) {
		return a.H1 == H1 && a.H2 == H2;
	};	
};

T comb(T a, T b) { 
	T c = a; c.len += b.len;
	c.H1 = ((a.H1 * 1LL * POW1[b.len]) + b.H1) % MOD1;
	c.H2 = ((a.H2 * 1LL * POW2[b.len]) + b.H2) % MOD2;
	return c;
}

T seg[2*nax];
void pull(int x) { seg[x] = comb(seg[2*x], seg[2*x+1]); }
void upd(int p, T v) { seg[p += nax] = v; for(p /= 2; p; p /= 2) pull(p); }
T get() { return seg[1]; }

int main() {
	cin.tie(0)->sync_with_stdio(0);
		
	for(int t = 0; t < 2*nax; t++) {
		if (t == 0) POW1[0] = POW2[0] = 1;
		else {
			POW1[t] = (POW1[t - 1] * 1LL * base) % MOD1;
			POW2[t] = (POW2[t - 1] * 1LL * base) % MOD2;
		}
		seg[t] = T{0, 0, 0};
	}

	int N, M; cin >> N >> M;

	V<int> A(N); for(auto& x : A) { cin >> x; --x; }
	V<int> B(M); for(auto& x : B) cin >> x;

	{
		map<int, int> mp; int cur = 0;
		for(auto& x : B) mp[x]++;
		for(auto& p : mp) p.second = cur++;
		for(auto& x : B) x = mp[x];
	}


	T H = {0, 0, 0}, R = {0, 0, 0}, REM = {0, 0, 0}; 
	for(int i = 0; i < N; i++) {
		H = comb(H, {A[i] + 1, A[i] + 1, 1});
		R = comb(R, {1, 1, 1});
	}

	V<int> ans;

	for(int i = 0; i < N; i++) upd(B[i], {i + 1, i + 1, 1});

	// cout << "H -> " << H.H1 << " " << H.H2 << " " << H.len << endl;
	// cout << "R -> " << R.H1 << " " << R.H2 << " " << R.len << endl;

	auto check = [&](int i) {
		T res = get(); 

		// REMOVE REM
		if ((res.H1 -= REM.H1) < 0) res.H1 += MOD1;
		if ((res.H2 -= REM.H2) < 0) res.H2 += MOD2;

		if (res == H) ans.push_back(i);
	};

	check(0);

	for(int i = N; i < M; i++) {
		// ADD R
		if ((REM.H1 += R.H1) >= MOD1) REM.H1 -= MOD1;
		if ((REM.H2 += R.H2) >= MOD2) REM.H2 -= MOD2;

		// ADD i
		upd(B[i], {i + 1, i + 1, 1});

		// REMOVE i - N
		upd(B[i - N], {0, 0, 0});

		check(i - N + 1);
	}

	cout << sz(ans) << nl;
	for(auto& x : ans) cout << x+1 << " ";
	cout << nl;

    return 0;
}


#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...