#include <iostream>
#include <vector>
#include <unordered_map>
#include <climits>
using namespace std;
const int DRAWS = 80;
struct Divisors {
int count = 0;
int divisors[10];
};
inline long long encodeKey(int divisor, int remainder) {
return ((long long)divisor << 32) | (unsigned int)remainder;
}
inline int keyDivisor(long long key) { return (int)(key >> 32); }
inline int keyRemainder(long long key) { return (int)(key & 0xFFFFFFFF); }
vector<int> generatePrimes(int n) {
vector<int> primes;
vector<bool> isPrime(n + 1, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2; i <= n; ++i) {
if (isPrime[i]) {
primes.push_back(i);
if ((long long)i * i <= n) {
for (int j = i * i; j <= n; j += i) {
isPrime[j] = false;
}
}
}
}
return primes;
}
vector<Divisors> generatePrimeDivisors(int q, const vector<int> &primes) {
vector<Divisors> primeDivisors(q + 1);
for (int prime : primes) {
int number = prime;
while (number <= q) {
primeDivisors[number].divisors[primeDivisors[number].count++] = prime;
number += prime;
}
}
return primeDivisors;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
uint32_t rng_state = 637258367;
auto rng = [&]() -> uint32_t { rng_state ^= rng_state << 13; rng_state ^= rng_state >> 17; rng_state ^= rng_state << 5; return rng_state; };
int n, q;
cin >> n >> q;
vector<int> primes = generatePrimes(n);
vector<Divisors> primeDivisors = generatePrimeDivisors(n, primes);
vector<bool> stones(n + 1, false);
int stonesCount = 0;
vector<int> stoneList;
vector<int> stoneIndex(n + 1, -1);
unordered_map<long long, int> bestScores;
int bestScore = 0;
vector<long long> bestCandidates;
int skipRemaining = 0;
unordered_map<long long, int> candidates;
candidates.reserve(DRAWS * 8);
for (int i = 1; i <= q; ++i) {
int stone;
cin >> stone;
bool adding = !stones[stone];
if (!adding) {
stones[stone] = false;
stonesCount--;
int index = stoneIndex[stone];
stoneList[index] = stoneList.back();
stoneIndex[stoneList.back()] = index;
stoneList.pop_back();
stoneIndex[stone] = -1;
} else {
stones[stone] = true;
stonesCount++;
stoneIndex[stone] = stoneList.size();
stoneList.push_back(stone);
}
if (stonesCount == 0) {
cout << 0 << '\n';
} else if (stonesCount == 1) {
cout << 1 << '\n';
} else if (skipRemaining > 0) {
skipRemaining--;
int diff = adding ? 1 : -1;
bool bestChanged = false;
for (long long c : bestCandidates) {
if (stone % keyDivisor(c) == keyRemainder(c)) {
bestScores[c] += diff;
if (diff == 1 && bestScores[c] > bestScore) {
bestScore = bestScores[c];
} else if (diff == -1 && bestScores[c] + 1 == bestScore) {
bestChanged = true;
}
}
}
if (bestChanged) {
bestScore = 0;
for (long long c : bestCandidates) {
bestScore = max(bestScore, bestScores[c]);
}
}
cout << bestScore << '\n';
} else {
candidates.clear();
int drawBest = 0;
for (int i = 0; i < DRAWS; ++i) {
int start = rng() % stonesCount;
int end = rng() % stonesCount;
if (start == end) {
continue;
}
int distance = abs(stoneList[end] - stoneList[start]);
for (int i = 0; i < primeDivisors[distance].count; ++i) {
int divisor = primeDivisors[distance].divisors[i];
int remainder = stoneList[start] % divisor;
long long key = encodeKey(divisor, remainder);
candidates[key]++;
drawBest = max(drawBest, candidates[key]);
}
}
bestCandidates.clear();
for (const auto &[key, count] : candidates) {
if (count >= drawBest - 100) {
bestCandidates.push_back(key);
}
}
bestScores.clear();
bestScore = 0;
int worstScore = INT_MAX;
for (int s : stoneList) {
for (long long c : bestCandidates) {
if (s % keyDivisor(c) == keyRemainder(c)) {
bestScores[c]++;
bestScore = max(bestScore, bestScores[c]);
}
}
}
for (long long c : bestCandidates) {
worstScore = min(worstScore, bestScores[c]);
}
bestScore = max(bestScore, 1);
skipRemaining = max(0, (bestScore - worstScore) / 2);
cout << bestScore << '\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 | #include <iostream> #include <vector> #include <unordered_map> #include <climits> using namespace std; const int DRAWS = 80; struct Divisors { int count = 0; int divisors[10]; }; inline long long encodeKey(int divisor, int remainder) { return ((long long)divisor << 32) | (unsigned int)remainder; } inline int keyDivisor(long long key) { return (int)(key >> 32); } inline int keyRemainder(long long key) { return (int)(key & 0xFFFFFFFF); } vector<int> generatePrimes(int n) { vector<int> primes; vector<bool> isPrime(n + 1, true); isPrime[0] = isPrime[1] = false; for (int i = 2; i <= n; ++i) { if (isPrime[i]) { primes.push_back(i); if ((long long)i * i <= n) { for (int j = i * i; j <= n; j += i) { isPrime[j] = false; } } } } return primes; } vector<Divisors> generatePrimeDivisors(int q, const vector<int> &primes) { vector<Divisors> primeDivisors(q + 1); for (int prime : primes) { int number = prime; while (number <= q) { primeDivisors[number].divisors[primeDivisors[number].count++] = prime; number += prime; } } return primeDivisors; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); uint32_t rng_state = 637258367; auto rng = [&]() -> uint32_t { rng_state ^= rng_state << 13; rng_state ^= rng_state >> 17; rng_state ^= rng_state << 5; return rng_state; }; int n, q; cin >> n >> q; vector<int> primes = generatePrimes(n); vector<Divisors> primeDivisors = generatePrimeDivisors(n, primes); vector<bool> stones(n + 1, false); int stonesCount = 0; vector<int> stoneList; vector<int> stoneIndex(n + 1, -1); unordered_map<long long, int> bestScores; int bestScore = 0; vector<long long> bestCandidates; int skipRemaining = 0; unordered_map<long long, int> candidates; candidates.reserve(DRAWS * 8); for (int i = 1; i <= q; ++i) { int stone; cin >> stone; bool adding = !stones[stone]; if (!adding) { stones[stone] = false; stonesCount--; int index = stoneIndex[stone]; stoneList[index] = stoneList.back(); stoneIndex[stoneList.back()] = index; stoneList.pop_back(); stoneIndex[stone] = -1; } else { stones[stone] = true; stonesCount++; stoneIndex[stone] = stoneList.size(); stoneList.push_back(stone); } if (stonesCount == 0) { cout << 0 << '\n'; } else if (stonesCount == 1) { cout << 1 << '\n'; } else if (skipRemaining > 0) { skipRemaining--; int diff = adding ? 1 : -1; bool bestChanged = false; for (long long c : bestCandidates) { if (stone % keyDivisor(c) == keyRemainder(c)) { bestScores[c] += diff; if (diff == 1 && bestScores[c] > bestScore) { bestScore = bestScores[c]; } else if (diff == -1 && bestScores[c] + 1 == bestScore) { bestChanged = true; } } } if (bestChanged) { bestScore = 0; for (long long c : bestCandidates) { bestScore = max(bestScore, bestScores[c]); } } cout << bestScore << '\n'; } else { candidates.clear(); int drawBest = 0; for (int i = 0; i < DRAWS; ++i) { int start = rng() % stonesCount; int end = rng() % stonesCount; if (start == end) { continue; } int distance = abs(stoneList[end] - stoneList[start]); for (int i = 0; i < primeDivisors[distance].count; ++i) { int divisor = primeDivisors[distance].divisors[i]; int remainder = stoneList[start] % divisor; long long key = encodeKey(divisor, remainder); candidates[key]++; drawBest = max(drawBest, candidates[key]); } } bestCandidates.clear(); for (const auto &[key, count] : candidates) { if (count >= drawBest - 100) { bestCandidates.push_back(key); } } bestScores.clear(); bestScore = 0; int worstScore = INT_MAX; for (int s : stoneList) { for (long long c : bestCandidates) { if (s % keyDivisor(c) == keyRemainder(c)) { bestScores[c]++; bestScore = max(bestScore, bestScores[c]); } } } for (long long c : bestCandidates) { worstScore = min(worstScore, bestScores[c]); } bestScore = max(bestScore, 1); skipRemaining = max(0, (bestScore - worstScore) / 2); cout << bestScore << '\n'; } } } |
English