1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include<bits/stdc++.h>
#define VAR(i,n) __typeof(n) i = (n)
#define loop(i,j,s) for(int i=j;i<s;i++)
#define loopback(i,j,s) for(int i=j;i>=s;i--)
#define foreach(i,c) for(VAR(i,(c).begin());i!=(c).end();i++)
#define pln( x ) cout << x << "\n"
#define ps( x ) cout << x << " "
#define entr cout << "\n"
#define pcnt(i) __builtin_popcount(i)
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define SIZE(c) (c).size()
#define ALL(c) (c).begin(), (c).end()
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef vector<vector<int> > VVI;
const int INFTY=20000000;
const int MAX=10000001;
const int MOD=10000000;

// --- SPF sieve ---
int spf[MAX];
VI primes, smallPrimes;
int sqN;

void buildSieve(int n){
	loop(i, 2, n + 1){
		if(spf[i] == 0){
			spf[i] = i;
			primes.pb(i);
			for(ll j = (ll)i * i; j <= n; j += i)
				if(spf[j] == 0) spf[j] = i;
		}
	}
	sqN = max(1, (int)sqrt((double)n));
	for(int p : primes)
		if(p <= sqN) smallPrimes.pb(p);
}

int largePrimeFactor(int v){
	while(v > 1 && spf[v] <= sqN)
		v /= spf[v];
	return (v > 1) ? v : 0;
}

// --- Small primes: flat arrays with O(1) max via freq counters ---
int cnt_flat[2000000];
int freq_flat[20000000];
int max_small_arr[500];
int sp_cnt_off[500];
int sp_freq_off[500];
int nSmall;

void initSmallPrimes(int n, int q){
	nSmall = SIZE(smallPrimes);
	int cntOff = 0, freqOff = 0;
	loop(i, 0, nSmall){
		int k = smallPrimes[i];
		sp_cnt_off[i] = cntOff;
		sp_freq_off[i] = freqOff;
		freq_flat[freqOff] = k;
		max_small_arr[i] = 0;
		cntOff += k;
		freqOff += min(n / k + 2, q + 2);
	}
}

void toggleSmallPrimes(int a, int delta){
	loop(i, 0, nSmall){
		int k = smallPrimes[i];
		int r = a % k;
		int* c = cnt_flat + sp_cnt_off[i];
		int* f = freq_flat + sp_freq_off[i];
		int oldVal = c[r];
		int newVal = oldVal + delta;
		c[r] = newVal;
		f[oldVal]--;
		f[newVal]++;
		if(delta > 0){
			if(newVal > max_small_arr[i]) max_small_arr[i] = newVal;
		} else {
			if(f[oldVal] == 0 && oldVal == max_small_arr[i])
				max_small_arr[i]--;
		}
	}
}

int querySmallPrimes(int bound){
	int best = 0;
	loop(i, 0, nSmall){
		if(smallPrimes[i] > bound) break;
		best = max(best, max_small_arr[i]);
	}
	return best;
}

// --- Large primes: pairwise difference approach ---
struct MyHash {
	size_t operator()(ll key) const {
		key = (key ^ (key >> 30)) * 0xbf58476d1ce4e5b9LL;
		key = (key ^ (key >> 27)) * 0x94d049bb133111ebLL;
		return key ^ (key >> 31);
	}
};

unordered_map<ll, int, MyHash> large_cnt; // (p*MAX+r) -> count
int large_freq[4000]; // max count < √n
int large_best;
bool large_dirty;

ll lcKey(int p, int r){ return (ll)p * MAX + r; }

void rebuildLargePrimes(VI& slist){
	large_cnt.clear();
	memset(large_freq, 0, sizeof(large_freq));
	large_best = 0;

	loop(i, 0, SIZE(slist)){
		unordered_map<int, int> temp;
		loop(j, 0, SIZE(slist)){
			if(i == j) continue;
			int diff = abs(slist[i] - slist[j]);
			int p = largePrimeFactor(diff);
			if(p > 0) temp[p]++;
		}
		for(auto& [p, cnt] : temp){
			int r = slist[i] % p;
			ll key = lcKey(p, r);
			int total = cnt + 1;
			if(!large_cnt.count(key)){
				large_cnt[key] = total;
				if(total >= 2 && total < 4000) large_freq[total]++;
				large_best = max(large_best, total);
			}
		}
	}
	large_dirty = false;
}

void processLargePrimes(int a, VI& slist, bool adding){
	unordered_map<int, int> temp;
	for(int s : slist){
		if(s == a) continue;
		int diff = abs(a - s);
		int p = largePrimeFactor(diff);
		if(p > 0) temp[p]++;
	}
	for(auto& [p, cnt] : temp){
		int r = a % p;
		ll key = lcKey(p, r);
		int oldCount, newCount;
		if(adding){
			oldCount = cnt;
			newCount = cnt + 1;
		} else {
			newCount = cnt;
			oldCount = cnt + 1;
		}
		if(oldCount >= 2 && oldCount < 4000) large_freq[oldCount]--;
		if(newCount >= 2 && newCount < 4000) large_freq[newCount]++;
		if(newCount >= 2) large_cnt[key] = newCount;
		else {
			auto it = large_cnt.find(key);
			if(it != large_cnt.end()) large_cnt.erase(it);
		}
		if(newCount > large_best) large_best = newCount;
	}
	while(large_best > 0 && large_freq[large_best] == 0) large_best--;
}

// --- Stone management ---
int stones[MAX];
int pos_in_slist[MAX];

void toggleStone(int a, VI& slist, int& d){
	bool wasPresent = stones[a];

	if(wasPresent){
		stones[a] = 0;
		d--;
		int idx = pos_in_slist[a];
		int last = slist.back();
		slist[idx] = last;
		pos_in_slist[last] = idx;
		slist.pop_back();
		pos_in_slist[a] = -1;
		toggleSmallPrimes(a, -1);
	} else {
		stones[a] = 1;
		d++;
		pos_in_slist[a] = SIZE(slist);
		slist.pb(a);
		toggleSmallPrimes(a, +1);
	}

	// Large prime maintenance
	if(d <= 2 * sqN){
		if(large_dirty){
			rebuildLargePrimes(slist);
		} else {
			processLargePrimes(a, slist, !wasPresent);
		}
	} else {
		if(!large_dirty) large_dirty = true;
	}
}

int solve(int d, int n){
	if(d == 0) return 0;
	int bound = (int)min((ll)n, 2LL * n / d);
	int best = max(1, querySmallPrimes(bound));
	if(d <= 2 * sqN)
		best = max(best, large_best);
	return best;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n, q;
	cin >> n >> q;

	buildSieve(n);
	initSmallPrimes(n, q);
	memset(pos_in_slist, -1, sizeof(pos_in_slist));
	large_best = 0;
	large_dirty = false;

	VI slist;
	int d = 0;

	loop(qi, 0, q){
		int a;
		cin >> a;
		a--;
		toggleStone(a, slist, d);
		pln(solve(d, n));
	}
	return 0;
}