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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx2")

#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
//using namespace __gnu_pbds;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using str = string;
 
//typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> oset;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
 
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}"; }
void _print() { cerr << "]\n"; }

template <typename T, typename... V>
void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); }

#ifdef LOCAL
#define debug(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << endl;
#else
#define debug(x...)
#endif

const int mod = 1e9+7;
const ll infi  = 1e18+1;
const ld eps = 1e-14L;

int n, q;

const int MXN = 1e7;
const int MXSMALL = 4000;
const int MXT = 35;

int factor[MXN+3] = {0};
int nxt[MXN+3] = {0};
int largefactor[MXN+3] = {0};

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// https://github.com/kth-competitive-programming/kactl/blob/main/content/various/FastInput.h
inline char gc() { // like getchar()
	static char buf[1 << 24];
	static size_t bc, be;
	if (bc >= be) {
		buf[0] = 0, bc = 0;
		be = fread(buf, 1, sizeof(buf), stdin);
	}
	return buf[bc++]; // returns 0 on EOF
}

int readInt() {
	int a, c;
	while ((a = gc()) < 40);
	while ((c = gc()) >= 48) a = a * 10 + c - 480;
	return a - 48;
}

struct Cont {
    //https://github.com/kth-competitive-programming/kactl/blob/main/content/various/FastMod.h
    struct FastMod {
	    ull b, m;
	    FastMod(ull b) : b(b), m(-1ULL / b) {}
	    ull reduce(ull a) { // a % b + (0 or b)
		    return a - (ull)((__uint128_t(m) * a) >> 64) * b;
	    }
    };

    int prime, ans, anscnt;
    vector<int> cnt;
    FastMod fm;
    Cont(int p) : fm(p) {
        prime = p;
        ans = 0;
        anscnt = 0;
        cnt.resize(p);
    }

    void inc(int i) {
        int pos = fm.reduce(i);
        if(pos >= prime) pos -= prime;
        if(cnt[pos]++ == ans){
            anscnt = 1;
            ans++;
        } else if(cnt[pos] == ans){
            anscnt++;
        }
    }

    void dec(int i) {
        int pos = fm.reduce(i);
        if(pos >= prime) pos -= prime;
        if(cnt[pos]-- == ans){
            if(--anscnt == 0){
                ans--;
                for(auto e : cnt){
                    anscnt += (ans == e);
                }
            }
        }
    }
};

struct Small {
    vector<Cont> small;

    Small(vector<int> &primes){
        for(auto &e : primes) small.emplace_back(e);
    }

    int change(int i, bool type) {
        int ans = 0;
        if(type) for(auto &e : small) e.inc(i), ans = max(ans, e.ans);
        else for(auto &e : small) e.dec(i), ans = max(ans, e.ans);
        return ans;
    }
};

struct Large {
    vector<int> alive, ind, uni;
    Large() {
        ind.resize(n+1, -1);
        alive.reserve(q);
    }

    void inc(int i) {
        ind[i] = alive.size();
        alive.push_back(i);
    }

    void dec(int i){
        int id = ind[i];
        int bck = alive.back();
        alive[id] = bck;
        ind[bck] = id;
        alive.pop_back();
        ind[i] = -1;
    }

    int calc() {
        int sajz = alive.size();
        if(sajz <= 1) return sajz;
        int ans = 0;
        uni.reserve(sajz);

        auto solve = [&](int v) -> void {
            uni.clear();
            for(auto e : alive){
                if(e == v) continue;
                int dist = v > e ? v - e : e - v;
                int p = largefactor[dist];
                if(p) uni.push_back(p);
            }
            int res = 1;
            if(!uni.empty()){
                sort(uni.begin(), uni.end());
                int now = 1;
                for(int j=1;j<(int)uni.size();j++){
                    if(uni[j] == uni[j-1]) now++;
                    else {
                        res = max(res, now+1);
                        now = 1;
                    }
                }
                res = max(res, now+1);
            }
            ans = max(ans, res);
        };

        if(sajz < MXT){
            for(auto e : alive) solve(e);
        } else {
            for(int i=0;i<MXT;i++) solve(alive[rng() % sajz]);
        }
        return ans;
    }
};

char obuf[1<<24];
int opos = 0;

void mywrite(int x){
    char tmp[12];
    int len = 0;
    do {
        tmp[len++] = '0' + x % 10;
        x/=10;
    } while(x);
    for(int i=len-1;i>=0;i--) obuf[opos++] = tmp[i];
    obuf[opos++] = '\n'; 
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //cin >> n >> q;
    n = readInt(); q = readInt();
    vector<int> primes, sprimes;
    primes.reserve(n/5);
    for(int i=2;i<=n;i++){
        if(!factor[i]){
            factor[i] = i;
            primes.push_back(i);
            if(i <= MXSMALL) sprimes.push_back(i);
        }
        for(auto e : primes){
            ll j = (ll)e * (ll)i;
            if(j > n || e > factor[i]) break;
            factor[j] = e;
            if(e == factor[i]){
                nxt[j] = nxt[i];
                break;
            } else {
                nxt[j] = i;
            }
        }
    }

    for(int i=2;i<=n;i++){
        int val = i;
        while(val > 1){
            int p = factor[val];
            val = nxt[val];
            if(p > MXSMALL){
                largefactor[i] = p;
                break;
            }
        }
    }

    Small small(sprimes);
    Large large;
    vector<char> alive(n+1, 0);
    int MXLARGE = (2LL * n) / MXSMALL + 5;
    while(q--){
        int i; //cin >> i;
        i = readInt();
        int ans = small.change(i, alive[i] == 0);
        if(alive[i] == 0){
            large.inc(i);
        } else {
            large.dec(i);
        }
        alive[i] ^= 1;
        int sajz = large.alive.size();
        if(sajz < MXLARGE) ans = max(ans, large.calc());
        //cout << ans << '\n';
        mywrite(ans);
    }
    fwrite(obuf, 1, opos, stdout);
    return 0;
}