#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 10'000'000;
const int T = 20;
const int P = 2000;
struct vec : vector<int> {
array<optional<size_t>, MAX_N + 1> where;
bool add_or_remove(int a) {
if (where[a]) {
where[this->back()] = where[a];
std::swap(this->at(where[a].value()), this->back());
this->pop_back();
where[a].reset();
}
else {
where[a] = this->size();
this->push_back(a);
}
return where[a].has_value();
}
pair<int, int> get_two(auto& rng) {
auto i = rng() % this->size();
auto j = rng() % (this->size() - 1);
if (i == j) { ++j; }
return {this->at(i), this->at(j)};
}
void clear() {
this->clear();
where = {};
}
};
struct seg_tree {
vector<int> data;
int base;
void set_size(int n) {
base = bit_ceil((uint32_t)n);
data.resize(2 * base);
}
void update(int i, int val, bool add) {
i += base;
if (add) {
data[i] += val;
}
else {
data[i] = val;
}
for (i /= 2; i; i /= 2) {
data[i] = max(data[2 * i], data[2 * i + 1]);
}
}
int get_max() {
return data[1];
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
vector<int> primes;
vector<int> sieve(MAX_N + 1);
for (int num = 2; num <= MAX_N; ++num) {
if (sieve[num]) { continue; }
primes.push_back(num);
for (int i = num; i <= MAX_N; i += num) {
sieve[i] = num;
}
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
vector<seg_tree> cnt(P);
for (int i = 0; i < P; ++i) {
cnt[i].set_size(primes[i]);
}
seg_tree total;
total.set_size(P);
int n, q;
cin >> n >> q;
vec stones;
vector<int> tmp(MAX_N + 1);
while (q--) {
int a;
cin >> a;
bool added = stones.add_or_remove(a);
for (int i = 0; i < P; ++i) {
cnt[i].update(a % primes[i], (added ? +1 : -1), true);
total.update(i, cnt[i].get_max(), false);
}
if (stones.size() <= 2) {
cout << stones.size() << '\n';
continue;
}
auto check = [&](int mod) {
int maxx = 0;
for (auto& s : stones) {
maxx = max(maxx, ++tmp[s % mod]);
}
for (auto& s : stones) {
--tmp[s % mod];
}
return maxx;
};
int res = total.get_max();
if (stones.size() < 1151) {
for (int i = 0; i < T; ++i) {
auto [x, y] = stones.get_two(rng);
int dist = abs(x - y);
int d = sieve[dist];
if (d <= 17389) { continue; }
res = max(res, check(d));
}
}
cout << res << '\n';
}
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 | #include <bits/stdc++.h> using namespace std; const int MAX_N = 10'000'000; const int T = 20; const int P = 2000; struct vec : vector<int> { array<optional<size_t>, MAX_N + 1> where; bool add_or_remove(int a) { if (where[a]) { where[this->back()] = where[a]; std::swap(this->at(where[a].value()), this->back()); this->pop_back(); where[a].reset(); } else { where[a] = this->size(); this->push_back(a); } return where[a].has_value(); } pair<int, int> get_two(auto& rng) { auto i = rng() % this->size(); auto j = rng() % (this->size() - 1); if (i == j) { ++j; } return {this->at(i), this->at(j)}; } void clear() { this->clear(); where = {}; } }; struct seg_tree { vector<int> data; int base; void set_size(int n) { base = bit_ceil((uint32_t)n); data.resize(2 * base); } void update(int i, int val, bool add) { i += base; if (add) { data[i] += val; } else { data[i] = val; } for (i /= 2; i; i /= 2) { data[i] = max(data[2 * i], data[2 * i + 1]); } } int get_max() { return data[1]; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); vector<int> primes; vector<int> sieve(MAX_N + 1); for (int num = 2; num <= MAX_N; ++num) { if (sieve[num]) { continue; } primes.push_back(num); for (int i = num; i <= MAX_N; i += num) { sieve[i] = num; } } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); vector<seg_tree> cnt(P); for (int i = 0; i < P; ++i) { cnt[i].set_size(primes[i]); } seg_tree total; total.set_size(P); int n, q; cin >> n >> q; vec stones; vector<int> tmp(MAX_N + 1); while (q--) { int a; cin >> a; bool added = stones.add_or_remove(a); for (int i = 0; i < P; ++i) { cnt[i].update(a % primes[i], (added ? +1 : -1), true); total.update(i, cnt[i].get_max(), false); } if (stones.size() <= 2) { cout << stones.size() << '\n'; continue; } auto check = [&](int mod) { int maxx = 0; for (auto& s : stones) { maxx = max(maxx, ++tmp[s % mod]); } for (auto& s : stones) { --tmp[s % mod]; } return maxx; }; int res = total.get_max(); if (stones.size() < 1151) { for (int i = 0; i < T; ++i) { auto [x, y] = stones.get_two(rng); int dist = abs(x - y); int d = sieve[dist]; if (d <= 17389) { continue; } res = max(res, check(d)); } } cout << res << '\n'; } return 0; } |
English