Submission #348258

#TimeUsernameProblemLanguageResultExecution timeMemory
348258jklepecMatching (CEOI11_mat)C++11
72 / 100
2072 ms43972 KiB
#include<bits/stdc++.h>
#define breturn return
#define ll long long
using namespace std;
const ll b = 31337;
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 10;
int n, m,bn,  arr[maxn], a, pb[maxn], che[maxn], chash, rhash, tour[40000001], bre, act[40000001], cnt, rpb[maxn], rb;
vector<pair<int, int> > perm;
vector<int> odg, v;
int mul(ll x, int y) {
	breturn (x * y)%mod;
}
int ad(int x, int y) {
	if(x + y >= mod) breturn x + y - mod;
	else if(x + y < 0) breturn x + y + mod; 
	breturn x + y;
} 
ll quer(int ax, int bx, int x = 1, int l = 0, int r = bn - 1) {
	if(l > bx or r < ax) breturn 0;
	if(l >= ax and r <= bx) breturn tour[x];
	int mid = (l + r)/2;
	breturn ad(quer(ax, bx, x * 2, l, mid), quer(ax, bx, x * 2 + 1, mid + 1, r));
} 
int smol(int ax, int bx, int x = 1, int l = 0, int r = bn - 1) {
	if(l > bx or r < ax) breturn 0;
	if(l >= ax and r <= bx) breturn act[x];
	int mid = (l + r)/2;
	breturn smol(ax, bx, x * 2, l, mid) + smol(ax, bx, x * 2 + 1, mid + 1, r);
}  
ll bp(ll p, ll x) {
	if(x == 0) breturn 1;
	ll xx = bp(p, x/2);	
	if(x%2) breturn mul(mul(xx, xx), p);
	breturn mul(xx, xx);	
} 
int bs(int val) {
	int lo = 0, hi = v.size() - 1, mid;
	while(lo != hi) {
		mid = (lo + hi)/2;
		if(v[mid] < val) lo = mid + 1;
		else hi = mid;
	}
	breturn lo + 1;
} 
int main() {
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n >> m;
	bn = (1 << ((int)log2(m - 1) + 1));
	pb[0] = 1;
	rpb[0] = 1;
	for(int i = 0; i < n; i++) {
		cin >> a;
		arr[a - 1] = i + 1;
	}
	rb = bp(b, mod - 2);
	for(int i = 1; i < m; i++)	pb[i] = mul(pb[i - 1], b), rpb[i] = mul(rpb[i - 1], rb);
	for(int i = 0; i < n; i++) {
		chash = mul(chash, b);
		chash = ad(chash, arr[i]);
	}
	for(int i = 0; i < m; i++) {
		cin >> arr[i];
		v.push_back(arr[i]);
	}
	sort(v.begin(), v.end());
	for(int i = 0; i < n; i++) perm.push_back({arr[i], i});	
	sort(perm.begin(), perm.end());
	for(int i = 0; i < n; i++) che[perm[i].second] = i + 1;	
	for(int i = 0; i < n; i++) {
		rhash = mul(rhash, b);
		rhash = ad(rhash, che[i]);
		int x = bs(arr[i]);
		tour[x + bn] = pb[n - i - 1];
		act[x + bn] = 1;
	}
	for(int i = bn - 1; i > 0; i--) tour[i] = ad(tour[i * 2], tour[i * 2 +1]), act[i] = act[i * 2] + act[i * 2 + 1];
	if(rhash == chash) odg.push_back(1);
	for(int i = n; i < m; i++) {		
		int x = bs(arr[i - n]);
		rhash = ad(rhash, -mul(smol(0, x), pb[n - 1]));
		rhash = ad(rhash, -mul(quer(x + 1, bn - 1), pb[cnt]));
		x += bn;
		act[x] = 0;
		tour[x] = 0;
		x /= 2;
		while(x) act[x] = act[x * 2] + act[x * 2 + 1], tour[x] = ad(tour[x * 2], tour[x * 2 + 1]), x /= 2;
		x = bs(arr[i]);		
		rhash = ad(rhash, mul(quer(x + 1, bn - 1), pb[cnt]));	
		rhash = mul(rhash, b);		
		rhash = ad(rhash, smol(0, x) + 1);
		x += bn;
		act[x] = 1;		
		cnt ++;	
		tour[x] = rpb[cnt];
		x /= 2;
		while(x) act[x] = act[x * 2] + act[x * 2 + 1], tour[x] = ad(tour[x * 2], tour[x * 2 + 1]), x /= 2;	
		if(rhash == chash) odg.push_back(i - n + 2);						
	}
	cout << odg.size() << '\n';
	for(int i = 0; i < odg.size(); i++) cout << odg[i] << " ";
}

Compilation message (stderr)

mat.cpp: In function 'int main()':
mat.cpp:101:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |  for(int i = 0; i < odg.size(); i++) cout << odg[i] << " ";
      |                 ~~^~~~~~~~~~~~
#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...