#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <string>
#include <queue>
#include <numeric>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MAX = 10000000;
const int LIMIT = 500; // ?
bool T[MAX];
int isP[MAX];
int nextP[MAX];
vector<int> P;
unordered_set<int> S;
set<pii> cnt[MAX];
int n;
int smallRes;
struct bucket {
bucket(int _k) : k(_k) {}
void modify(int t, int v) {
t %= k;
heap.erase({cnt[t], t});
cnt[t] += v;
heap.insert({cnt[t], t});
}
int top() {
return heap.empty() ? 0 : heap.begin()->first;
}
int k;
set<pii, greater<>> heap;
unordered_map<int, int> cnt;
};
void compP() {
nextP[1] = 1;
for (int i = 2; i < n; i++) {
if (isP[i] == 0) {
isP[i] = i;
nextP[i] = 1;
P.push_back(i);
for (ll j = (ll)i * i; j < n; j += i) {
isP[j] = i;
}
} else {
}
}
}
unordered_map<int, bucket> B;
set<pair<int, int>, greater<>> Bscores;
bucket& getBucket(int t) {
auto it = B.find(t);
if (it == B.end()) {
it = B.insert({t, bucket(t)}).first;
}
return it->second;
}
int candP[MAX];
int Pind = 1;
int solveSmall(int a) {
for (int b : S) {
if (a == b) {
continue;
}
int t = abs(a - b);
while (t != 1) {
int p = isP[t];
if (candP[p] != Pind) {
auto &bt = getBucket(p);
Bscores.erase({bt.top(), p});
bt.modify(a, T[a] ? 1 : -1);
Bscores.insert({bt.top(), p});
candP[p] = Pind;
}
t /= p;
}
}
Pind++;
return Bscores.empty() ? S.size() : Bscores.begin()->first + !S.empty();
}
void prepSmall() {
B.clear();
Bscores.clear();
for (int a : S) {
for (int b : S) {
if (a <= b) {
continue;
}
int t = a - b;
while (t != 1) {
int p = isP[t];
if (candP[p] != Pind) {
auto &bt = getBucket(p);
Bscores.erase({bt.top(), p});
bt.modify(a, T[a] ? 1 : -1);
Bscores.insert({bt.top(), p});
}
t /= p;
}
}
}
Pind++;
}
int solveBig(int a) {
int res = 0;
int op = 0;
for (int p : P) {
if (p * LIMIT > 2 * n) {
break;
}
op++;
auto &bt = getBucket(p);
bt.modify(a, T[a] ? 1 : -1);
res = max(res, bt.top());
}
return res;
}
int main() {
int q;
cin >> n >> q;
compP();
bool small = true;
while (q--) {
int a;
cin >> a;
a--;
if (T[a]) {
S.erase(a);
if (!small && S.size() == LIMIT) {
small = true;
prepSmall();
}
} else {
S.insert(a);
if (small && S.size() == 2 * LIMIT) {
small = false;
}
}
T[a] ^= 1;
cout << (small ? solveSmall(a) : solveBig(a)) << endl;
}
return 0;
}
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 | #include <iostream> #include <vector> #include <algorithm> #include <map> #include <set> #include <cmath> #include <string> #include <queue> #include <numeric> #include <unordered_set> #include <unordered_map> using namespace std; using ll = long long; using pii = pair<int, int>; const int MAX = 10000000; const int LIMIT = 500; // ? bool T[MAX]; int isP[MAX]; int nextP[MAX]; vector<int> P; unordered_set<int> S; set<pii> cnt[MAX]; int n; int smallRes; struct bucket { bucket(int _k) : k(_k) {} void modify(int t, int v) { t %= k; heap.erase({cnt[t], t}); cnt[t] += v; heap.insert({cnt[t], t}); } int top() { return heap.empty() ? 0 : heap.begin()->first; } int k; set<pii, greater<>> heap; unordered_map<int, int> cnt; }; void compP() { nextP[1] = 1; for (int i = 2; i < n; i++) { if (isP[i] == 0) { isP[i] = i; nextP[i] = 1; P.push_back(i); for (ll j = (ll)i * i; j < n; j += i) { isP[j] = i; } } else { } } } unordered_map<int, bucket> B; set<pair<int, int>, greater<>> Bscores; bucket& getBucket(int t) { auto it = B.find(t); if (it == B.end()) { it = B.insert({t, bucket(t)}).first; } return it->second; } int candP[MAX]; int Pind = 1; int solveSmall(int a) { for (int b : S) { if (a == b) { continue; } int t = abs(a - b); while (t != 1) { int p = isP[t]; if (candP[p] != Pind) { auto &bt = getBucket(p); Bscores.erase({bt.top(), p}); bt.modify(a, T[a] ? 1 : -1); Bscores.insert({bt.top(), p}); candP[p] = Pind; } t /= p; } } Pind++; return Bscores.empty() ? S.size() : Bscores.begin()->first + !S.empty(); } void prepSmall() { B.clear(); Bscores.clear(); for (int a : S) { for (int b : S) { if (a <= b) { continue; } int t = a - b; while (t != 1) { int p = isP[t]; if (candP[p] != Pind) { auto &bt = getBucket(p); Bscores.erase({bt.top(), p}); bt.modify(a, T[a] ? 1 : -1); Bscores.insert({bt.top(), p}); } t /= p; } } } Pind++; } int solveBig(int a) { int res = 0; int op = 0; for (int p : P) { if (p * LIMIT > 2 * n) { break; } op++; auto &bt = getBucket(p); bt.modify(a, T[a] ? 1 : -1); res = max(res, bt.top()); } return res; } int main() { int q; cin >> n >> q; compP(); bool small = true; while (q--) { int a; cin >> a; a--; if (T[a]) { S.erase(a); if (!small && S.size() == LIMIT) { small = true; prepSmall(); } } else { S.insert(a); if (small && S.size() == 2 * LIMIT) { small = false; } } T[a] ^= 1; cout << (small ? solveSmall(a) : solveBig(a)) << endl; } return 0; } |
English