//fast
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define all(x) x.begin(),x.end()
#define rep(n) for (int i = 0 ; i<n ; i++)
#define pb push_back
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,q;
cin >> n >> q;
bool on[n+1] = {0};
vector<pair<int,vector<int>>> xd;
priority_queue<pair<int,pair<int,int>>> best;
bool sito[n+1] = {0};
for (int i = 2 ; i<=n ; i++){
if (!sito[i]){
xd.pb({i,{}});
for (int j = 0 ; j<i ; j++){
xd.back().second.pb(0);
best.push({0,{xd.size()-1,xd.back().second.size()-1}});
}
for (int j = i ; j<=n ; j+=i){
sito[j] = 1;
}
}
}
while (q--){
int a;
cin >> a;
on[a] = !on[a];
int pom = -1+2*on[a];
rep(xd.size()){
int x = xd[i].first;
xd[i].second[a%x]+=pom;
best.push({xd[i].second[a%x],{i,a%x}});
}
while (xd[best.top().second.first].second[best.top().second.second]!=best.top().first) best.pop();
cout << best.top().first << '\n';
}
}
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 | //fast #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define all(x) x.begin(),x.end() #define rep(n) for (int i = 0 ; i<n ; i++) #define pb push_back int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n,q; cin >> n >> q; bool on[n+1] = {0}; vector<pair<int,vector<int>>> xd; priority_queue<pair<int,pair<int,int>>> best; bool sito[n+1] = {0}; for (int i = 2 ; i<=n ; i++){ if (!sito[i]){ xd.pb({i,{}}); for (int j = 0 ; j<i ; j++){ xd.back().second.pb(0); best.push({0,{xd.size()-1,xd.back().second.size()-1}}); } for (int j = i ; j<=n ; j+=i){ sito[j] = 1; } } } while (q--){ int a; cin >> a; on[a] = !on[a]; int pom = -1+2*on[a]; rep(xd.size()){ int x = xd[i].first; xd[i].second[a%x]+=pom; best.push({xd[i].second[a%x],{i,a%x}}); } while (xd[best.top().second.first].second[best.top().second.second]!=best.top().first) best.pop(); cout << best.top().first << '\n'; } } |
English