#include <algorithm>
#include <cassert>
#include <iostream>
#include <random>
#include <sched.h>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define all(a) begin(a), end(a)
mt19937 rng{147852314};
int rand(int l, int r) { return uniform_int_distribution<>(l, r)(rng); }
constexpr int N = 1e7 + 10;
constexpr int X = 100;
int lp[N];
void init() {
vector<int> primes(N);
int ptr = 0;
lp[1] = 1;
for (int i = 2; i < N; ++i) {
if (!lp[i]) {
lp[i] = i;
primes[ptr++] = i;
}
for (int it = 0; it < ptr; it++) {
auto p = primes[it];
int v = i * p;
if (v >= N) {
break;
}
lp[v] = p;
if (p == lp[i]) {
break;
}
}
}
}
vector<int> get_primes(int n) {
vector<int> res;
while (n != 1) {
int t = lp[n];
res.push_back(t);
while (n % t == 0) {
n /= t;
}
}
return res;
}
int cnt_buf[N];
vector<pair<pair<int, int>, int>> recalc(vector<int> const &a, int x, int lim) {
assert(lim > 0);
const int n = a.size();
vector<int> ks{2};
if (ll(n) * (n - 1) / 2 <= x) {
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
ks.push_back(abs(a[i] - a[j]));
}
}
} else {
for (int iter = 0; iter < x; iter++) {
int i = rand(0, n - 1), j = rand(0, n - 1);
if (i == j) {
iter--;
continue;
}
ks.push_back(abs(a[i] - a[j]));
}
}
sort(all(ks));
ks.erase(unique(all(ks)), end(ks));
vector<int> ks2;
for (auto i : ks) {
for (auto j : get_primes(i)) {
ks2.push_back(j);
}
}
ks = ks2;
sort(all(ks));
ks.erase(unique(all(ks)), end(ks));
vector<pair<pair<int, int>, int>> res;
for (auto k : ks) {
if (k == 1) {
continue;
}
for (auto i : a) {
++cnt_buf[i % k];
}
for (auto i : a) {
int d = i % k;
int &c = cnt_buf[d];
if (c >= lim) {
res.push_back({{k, d}, c});
}
c = 0;
}
}
return res;
}
int pos[N];
int freq[N];
void solve() {
int n, q;
cin >> n >> q;
for (int i = 0; i <= n; i++) {
pos[i] = -1;
freq[i] = 0;
}
int res = 0;
vector<int> nums;
vector<pair<pair<int, int>, int>> cnt;
int scheduled = 0;
for (int iter = 0; iter < q; iter++) {
int x;
cin >> x;
int d;
int &p = pos[x];
if (p != -1) {
pos[nums.back()] = p;
swap(nums[p], nums.back());
nums.pop_back();
p = -1;
d = -1;
} else {
p = nums.size();
nums.push_back(x);
d = +1;
}
for (auto &[p, v] : cnt) {
auto [k, b] = p;
if (x % k == b) {
--freq[v];
v += d;
++freq[v];
res = max(res, v);
}
}
const int s = nums.size();
if (iter >= scheduled || s <= 1) {
const int g = s / 3, t = s - 2 * g;
while (res >= 0) {
freq[res--] = 0;
}
cnt = recalc(nums, X, max(1, g - 1));
res = 0;
for (auto [_, v] : cnt) {
freq[v]++;
res = max(res, v);
}
scheduled = iter + t;
}
while (res && !freq[res]) {
res--;
}
cout << res << "\n";
}
}
int main() {
init();
ios_base::sync_with_stdio(0);
cin.tie(0);
int q = 1;
// cin >> q;
while (q--) {
solve();
}
}
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 197 198 199 200 | #include <algorithm> #include <cassert> #include <iostream> #include <random> #include <sched.h> #include <set> #include <vector> using namespace std; using ll = long long; #define all(a) begin(a), end(a) mt19937 rng{147852314}; int rand(int l, int r) { return uniform_int_distribution<>(l, r)(rng); } constexpr int N = 1e7 + 10; constexpr int X = 100; int lp[N]; void init() { vector<int> primes(N); int ptr = 0; lp[1] = 1; for (int i = 2; i < N; ++i) { if (!lp[i]) { lp[i] = i; primes[ptr++] = i; } for (int it = 0; it < ptr; it++) { auto p = primes[it]; int v = i * p; if (v >= N) { break; } lp[v] = p; if (p == lp[i]) { break; } } } } vector<int> get_primes(int n) { vector<int> res; while (n != 1) { int t = lp[n]; res.push_back(t); while (n % t == 0) { n /= t; } } return res; } int cnt_buf[N]; vector<pair<pair<int, int>, int>> recalc(vector<int> const &a, int x, int lim) { assert(lim > 0); const int n = a.size(); vector<int> ks{2}; if (ll(n) * (n - 1) / 2 <= x) { for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { ks.push_back(abs(a[i] - a[j])); } } } else { for (int iter = 0; iter < x; iter++) { int i = rand(0, n - 1), j = rand(0, n - 1); if (i == j) { iter--; continue; } ks.push_back(abs(a[i] - a[j])); } } sort(all(ks)); ks.erase(unique(all(ks)), end(ks)); vector<int> ks2; for (auto i : ks) { for (auto j : get_primes(i)) { ks2.push_back(j); } } ks = ks2; sort(all(ks)); ks.erase(unique(all(ks)), end(ks)); vector<pair<pair<int, int>, int>> res; for (auto k : ks) { if (k == 1) { continue; } for (auto i : a) { ++cnt_buf[i % k]; } for (auto i : a) { int d = i % k; int &c = cnt_buf[d]; if (c >= lim) { res.push_back({{k, d}, c}); } c = 0; } } return res; } int pos[N]; int freq[N]; void solve() { int n, q; cin >> n >> q; for (int i = 0; i <= n; i++) { pos[i] = -1; freq[i] = 0; } int res = 0; vector<int> nums; vector<pair<pair<int, int>, int>> cnt; int scheduled = 0; for (int iter = 0; iter < q; iter++) { int x; cin >> x; int d; int &p = pos[x]; if (p != -1) { pos[nums.back()] = p; swap(nums[p], nums.back()); nums.pop_back(); p = -1; d = -1; } else { p = nums.size(); nums.push_back(x); d = +1; } for (auto &[p, v] : cnt) { auto [k, b] = p; if (x % k == b) { --freq[v]; v += d; ++freq[v]; res = max(res, v); } } const int s = nums.size(); if (iter >= scheduled || s <= 1) { const int g = s / 3, t = s - 2 * g; while (res >= 0) { freq[res--] = 0; } cnt = recalc(nums, X, max(1, g - 1)); res = 0; for (auto [_, v] : cnt) { freq[v]++; res = max(res, v); } scheduled = iter + t; } while (res && !freq[res]) { res--; } cout << res << "\n"; } } int main() { init(); ios_base::sync_with_stdio(0); cin.tie(0); int q = 1; // cin >> q; while (q--) { solve(); } } |
English