#include <algorithm>
#include <iostream>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <vector>
using namespace std;
const int N = 10'000'000;
const int Q = 1'000'000;
int primes[664'579 + 1];
bool isPrime[N + 1];
int primeDivisors[N + 1][10];
int primeDivisorsCounts[N + 1];
unordered_map<int, vector<int>> primeToStoneCount;
vector<int> resultCounts(N + 1);
int res{};
//int SMALL_PRIMES = 303;
int SMALL_PRIMES = 800; // 10 sec
int THRESHOLD = 200;
int THRESHOLD_HIST = 1.05 * THRESHOLD;
struct PairHash {
size_t operator()(const std::pair<int, int> &p) const {
return std::hash<int>{}(p.first) ^ (std::hash<int>{}(p.second) << 1);
}
};
int handleSmallStone(
unordered_map<pair<int, int>, int, PairHash> &primeToStoneCountSmall,
map<int, int> &resultsCountSmall,
unordered_set<int> &stones,
int a
) {
unordered_set<int> used;
for (auto s : stones) {
int dst = abs(s - a);
if (dst < 2) {
continue;
}
bool adding = stones.contains(a);
// cerr << "divisors of " << dst << ": ";
// for (int i = 0; i < primeDivisorsCounts[dst]; i++) {
// cerr << primeDivisors[dst][i] << " ";
// }
// cerr << "\n";
for (int i = 0; i < primeDivisorsCounts[dst]; i++) {
int p = primeDivisors[dst][i];
if (used.contains(p)) {
continue;
}
used.insert(p);
int ap = a % p;
std::pair<int, int> pap = {p, ap};
if (primeToStoneCountSmall.contains(pap)) {
--resultsCountSmall[primeToStoneCountSmall[pap]];
if (resultsCountSmall[primeToStoneCountSmall[pap]] == 0) {
resultsCountSmall.erase(primeToStoneCountSmall[pap]);
}
}
if (adding) {
++primeToStoneCountSmall[pap];
if (primeToStoneCountSmall[pap] == 1) {
primeToStoneCountSmall[pap] = 2;
}
++resultsCountSmall[primeToStoneCountSmall[pap]];
} else {
--primeToStoneCountSmall[pap];
if (primeToStoneCountSmall[pap] == 1) {
primeToStoneCountSmall.erase(pap);
} else {
++resultsCountSmall[primeToStoneCountSmall[pap]];
}
}
}
// cerr << "primeToStoneCountSmall\n";
// for (auto &[p, count] : primeToStoneCountSmall) {
// cerr << "(" << p.first << ", " << p.second << "): " << count << " ";
// }
// cerr << "\n";
// cerr << "resultsCountSmall\n";
// for (auto &[p, count] : resultsCountSmall) {
// cerr << "(" << p << ", " << count << ")\n";
// }
// cerr << "\n";
}
if (resultsCountSmall.empty()) {
return ssize(stones) < 2 ? ssize(stones) : 1;
} else {
return resultsCountSmall.rbegin()->first;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
fill(isPrime, isPrime + N + 1, true);
for (int i = 2; i <= N; i++) {
primeDivisorsCounts[i] = 0;
}
for (int i = 2; i <= N; i++) {
if (!isPrime[i]) {
continue;
}
for (int j = 2 * i; j <= N; j += i) {
isPrime[j] = false;
primeDivisors[j][primeDivisorsCounts[j]++] = i;
}
}
for (int i = 2; i <= N; i++) {
if (isPrime[i]) {
primeDivisors[i][0] = i;
primeDivisorsCounts[i] = 1;
}
}
int idx = 0;
for (int i = 2; i <= N; i++) {
if (isPrime[i]) {
primes[idx++] = i;
}
}
unordered_set<int> stones;
for (int i = 0; i < SMALL_PRIMES; i++) {
primeToStoneCount[primes[i]].resize(primes[i], 0);
}
unordered_map<pair<int, int>, int, PairHash> primeToStoneCountSmall;
map<int, int> resultsCountSmall;
bool smallActive = true;
bool recalculate = false;
int counter = 0;
while (q--) {
++counter;
int a;
cin >> a;
if (stones.contains(a)) {
stones.erase(a);
for (auto &[p, vec] : primeToStoneCount) {
int ap = a % p;
int newCount = --resultCounts[vec[ap]];
if (res == vec[ap] && newCount == 0) {
--res;
}
--vec[ap];
++resultCounts[vec[ap]];
}
} else {
stones.insert(a);
for (auto &[p, vec] : primeToStoneCount) {
int ap = a % p;
--resultCounts[vec[ap]];
if (vec[ap] == res) {
++res;
}
++vec[ap];
++resultCounts[vec[ap]];
}
}
if (ssize(stones) < THRESHOLD) {
smallActive = true;
} else if (ssize(stones) >= THRESHOLD_HIST) {
smallActive = false;
recalculate = true;
}
int smallRes{};
if (smallActive) {
if (recalculate) {
recalculate = false;
// for (auto s : stones) {
// cout << s << " ";
// }
// cout << "\n";
primeToStoneCountSmall.clear();
resultsCountSmall.clear();
auto stonesCopy = stones;
stones.clear();
for (auto s : stonesCopy) {
stones.insert(s);
smallRes = handleSmallStone(primeToStoneCountSmall, resultsCountSmall, stones, s);
}
} else {
smallRes = handleSmallStone(primeToStoneCountSmall, resultsCountSmall, stones, a);
}
cout << smallRes << "\n";
} else {
cout << res << "\n";
}
// cerr << smallRes << " " << res << "\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 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 | #include <algorithm> #include <iostream> #include <unordered_set> #include <unordered_map> #include <map> #include <vector> using namespace std; const int N = 10'000'000; const int Q = 1'000'000; int primes[664'579 + 1]; bool isPrime[N + 1]; int primeDivisors[N + 1][10]; int primeDivisorsCounts[N + 1]; unordered_map<int, vector<int>> primeToStoneCount; vector<int> resultCounts(N + 1); int res{}; //int SMALL_PRIMES = 303; int SMALL_PRIMES = 800; // 10 sec int THRESHOLD = 200; int THRESHOLD_HIST = 1.05 * THRESHOLD; struct PairHash { size_t operator()(const std::pair<int, int> &p) const { return std::hash<int>{}(p.first) ^ (std::hash<int>{}(p.second) << 1); } }; int handleSmallStone( unordered_map<pair<int, int>, int, PairHash> &primeToStoneCountSmall, map<int, int> &resultsCountSmall, unordered_set<int> &stones, int a ) { unordered_set<int> used; for (auto s : stones) { int dst = abs(s - a); if (dst < 2) { continue; } bool adding = stones.contains(a); // cerr << "divisors of " << dst << ": "; // for (int i = 0; i < primeDivisorsCounts[dst]; i++) { // cerr << primeDivisors[dst][i] << " "; // } // cerr << "\n"; for (int i = 0; i < primeDivisorsCounts[dst]; i++) { int p = primeDivisors[dst][i]; if (used.contains(p)) { continue; } used.insert(p); int ap = a % p; std::pair<int, int> pap = {p, ap}; if (primeToStoneCountSmall.contains(pap)) { --resultsCountSmall[primeToStoneCountSmall[pap]]; if (resultsCountSmall[primeToStoneCountSmall[pap]] == 0) { resultsCountSmall.erase(primeToStoneCountSmall[pap]); } } if (adding) { ++primeToStoneCountSmall[pap]; if (primeToStoneCountSmall[pap] == 1) { primeToStoneCountSmall[pap] = 2; } ++resultsCountSmall[primeToStoneCountSmall[pap]]; } else { --primeToStoneCountSmall[pap]; if (primeToStoneCountSmall[pap] == 1) { primeToStoneCountSmall.erase(pap); } else { ++resultsCountSmall[primeToStoneCountSmall[pap]]; } } } // cerr << "primeToStoneCountSmall\n"; // for (auto &[p, count] : primeToStoneCountSmall) { // cerr << "(" << p.first << ", " << p.second << "): " << count << " "; // } // cerr << "\n"; // cerr << "resultsCountSmall\n"; // for (auto &[p, count] : resultsCountSmall) { // cerr << "(" << p << ", " << count << ")\n"; // } // cerr << "\n"; } if (resultsCountSmall.empty()) { return ssize(stones) < 2 ? ssize(stones) : 1; } else { return resultsCountSmall.rbegin()->first; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; fill(isPrime, isPrime + N + 1, true); for (int i = 2; i <= N; i++) { primeDivisorsCounts[i] = 0; } for (int i = 2; i <= N; i++) { if (!isPrime[i]) { continue; } for (int j = 2 * i; j <= N; j += i) { isPrime[j] = false; primeDivisors[j][primeDivisorsCounts[j]++] = i; } } for (int i = 2; i <= N; i++) { if (isPrime[i]) { primeDivisors[i][0] = i; primeDivisorsCounts[i] = 1; } } int idx = 0; for (int i = 2; i <= N; i++) { if (isPrime[i]) { primes[idx++] = i; } } unordered_set<int> stones; for (int i = 0; i < SMALL_PRIMES; i++) { primeToStoneCount[primes[i]].resize(primes[i], 0); } unordered_map<pair<int, int>, int, PairHash> primeToStoneCountSmall; map<int, int> resultsCountSmall; bool smallActive = true; bool recalculate = false; int counter = 0; while (q--) { ++counter; int a; cin >> a; if (stones.contains(a)) { stones.erase(a); for (auto &[p, vec] : primeToStoneCount) { int ap = a % p; int newCount = --resultCounts[vec[ap]]; if (res == vec[ap] && newCount == 0) { --res; } --vec[ap]; ++resultCounts[vec[ap]]; } } else { stones.insert(a); for (auto &[p, vec] : primeToStoneCount) { int ap = a % p; --resultCounts[vec[ap]]; if (vec[ap] == res) { ++res; } ++vec[ap]; ++resultCounts[vec[ap]]; } } if (ssize(stones) < THRESHOLD) { smallActive = true; } else if (ssize(stones) >= THRESHOLD_HIST) { smallActive = false; recalculate = true; } int smallRes{}; if (smallActive) { if (recalculate) { recalculate = false; // for (auto s : stones) { // cout << s << " "; // } // cout << "\n"; primeToStoneCountSmall.clear(); resultsCountSmall.clear(); auto stonesCopy = stones; stones.clear(); for (auto s : stonesCopy) { stones.insert(s); smallRes = handleSmallStone(primeToStoneCountSmall, resultsCountSmall, stones, s); } } else { smallRes = handleSmallStone(primeToStoneCountSmall, resultsCountSmall, stones, a); } cout << smallRes << "\n"; } else { cout << res << "\n"; } // cerr << smallRes << " " << res << "\n"; } } |
English