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
#ifndef UNCLE //jakas hybryda bru 3
 #pragma GCC optimize("O3,unroll-loops")
#endif

#include <bits/stdc++.h>
using namespace std;

#define FOR(i,p,q) for(int i=(p); i<=(q); ++i)
#define ROF(i,p,q) for(int i=(p); i>=(q); --i)
#define REP(i,q) for(int i=0; i<(q); ++i)
#define pb push_back
#define as assign
#define rz resize
#define Co const
#define al(X) X.begin(), X.end()
#define ral(X) X.rbegin(), X.rend()
#define sz(X) (int)((X).size())
#define ckmx(a,b) a=max(a,b)
#define ckmn(a,b) a=min(a,b)
#define V vector
typedef long long ll;
typedef long double ld;
typedef mt19937_64 mt;
#ifndef UNCLE
 typedef basic_string<bool> vb;
 typedef basic_string<int> vi;
 typedef basic_string<ll> vl;
#else
 typedef V<bool> vb;
 typedef V<int> vi;
 typedef V<ll> vl;
#endif

constexpr ll INFl=(ll)1e18+14;
// constexpr int INFi=1e9+14, MX_N=1e7, B=1, R=20; //B,R do zmiany
constexpr int INFi=1e9+14, MX_N=1e7, B=350, R=25; //B,R do zmiany


mt rng1(14);
mt rng2(rng1());
mt rng3(rng2());
mt rng4(rng3());
mt rng5(rng4());
mt rng(rng5());

int Rand(int p, int q){
    return p+(int)(rng())%(q-p+1);
}

int N,M,Q;
int cqt[MX_N+1], isP[MX_N+1+100];
bool ami[MX_N];
vi s,pri;
inline bool Rk(Co int i,Co int ne,Co int pd,Co int x){
    if(x!=-1&&(x<=pri[B-1]||(isP[x]!=1&&isP[x]<=pri[B-1]))) return 0;
    if(ne==0) return 1;
    if(i==-1) return 0;
    if(pd==-1){
        if(Rk(i-1,ne-1,i,-1)) return 1;
    }else if(x==-1){
        if(Rk(i-1,ne-1,i,s[pd]-s[i])) return 1; //nie musze abs
    }else{
        if(Rk(i-1,ne-1,i,__gcd(x,s[pd]-s[i]))) return 1;
    }
    if(i+1!=ne){
        if(Rk(i-1,ne,pd,x)) return 1;
    }
    return 0;
}
void Inpt(){


    cin>>N>>Q;
    set<int> st; //set no już niech będzie;
    unordered_set<int> ust;
    int m=0, prs=0;
    V<vi> dp(B,vi(pri[B-1],0));
    vi qtrsd(Q+2,0);
    qtrsd[0]=INFi;
    REP(_,Q){
        int vv,rs=0; cin>>vv; --vv;
        if(ami[vv]){
            st.erase(vv),--m,ami[vv]=0;
            ust.erase(vv);
            REP(k,B){
                int ck=pri[k],cc=vv%ck;
                --qtrsd[dp[k][cc]];
                --dp[k][cc];
                ++qtrsd[dp[k][cc]];
            }
            if(qtrsd[prs]){ cout<<prs<<"\n"; continue;}
            rs=prs-1;
        }else{
            ami[vv]=1, st.insert(vv),++m;
            ust.insert(vv);
            REP(k,B){
                int ck=pri[k],cc=vv%ck;
                --qtrsd[dp[k][cc]];
                ++dp[k][cc];
                ++qtrsd[dp[k][cc]];
            }
            if(qtrsd[prs+1]){ cout<<(++prs)<<"\n"; continue;}
            rs=prs;
        }
        if(m<=1){ cout<<m<<"\n"; continue;}
        if(m==2){
            if(*(st.begin())+1==*(--st.end())) {prs=1; cout<<"1\n";}
            else {prs=2; cout<<"2\n";}
            continue;
        }
        int mnX=*st.begin(),mxX=*(--st.end()),wid=mxX-mnX+1, mxK=(wid-1)/(rs);
        auto Sv1=[&]() -> void {//(Newt(m,rs+1)*M*(gcd)), choc imo cap
            s.clear();
            for(int cv:st) s.pb(cv);
            if(Rk(m-1,rs+1,-1,-1)) ++rs;
        };
        auto Sv2=[&]() -> void {//O(mxK*M(%))
            int j=B;
            while(pri[j]<=mxK){ // Wierzę że szybciej się zakończy; wysatrzą mi ~30 'losowych' reszt;
                int ck=pri[j];
                bool br=0;
                // if(m<wid/ck*R){
                //     for(int cv:st){
                //         if(++cqt[cv%ck]==rs+1){++rs, br=1; break;}
                //     }
                //     for(int cv:st) cqt[cv%ck]=0;
                // }else{
                    vi s;
                    for(int vv:ust){
                        if(sz(s)>R*R) break;
                        s.pb(vv);
                    }
                    shuffle(al(s),rng);
                    REP(i,R){
                        if(i>=sz(s)) break;
                        int cp=s[i],cqt=1;
                        for(int j=cp+ck;j<=mxX;j+=ck) if(ami[j]) ++cqt;
                        for(int j=cp-ck;j>=mnX;j-=ck) if(ami[j]) ++cqt;
                        if(cqt==rs+1){++ rs; br=1; break;}
                    }
                // }

                if(br) break;
                ++j;
            }
        };
        if(mxK<pri[B-1]){ cout<<(prs=rs)<<"\n"; continue;}
        if(M<=17) Sv1();
        else
        Sv2();

        cout<<rs<<"\n";
        prs=rs;
    }
}
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    isP[0]=isP[1]=0;
    FOR(i,2,MX_N+100) isP[i]=1;
    FOR(i,2,MX_N+100) if(isP[i]==1){
        pri.pb(i);
        for(int j=i+i;j<=MX_N+100;j+=i) isP[j]=i;
    }

    Inpt();
}