//#ifdef DEBUG
//#define _GLIBCXX_DEBUG
//#endif
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "lib/debug.h"
#else
#define debug(...) 228
#endif
typedef long long ll;
typedef long double ld;
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
vector<int> read_vec(int n) {
vector<int> v(n);
for (int& t : v) cin >> t;
return v;
}
template<typename T>
inline void upd_max(T& a, T b) {
if (a < b) {
a = b;
}
}
template<typename T>
inline void upd_min(T& a, T b) {
if (a > b) {
a = b;
}
}
using namespace std::chrono;
int n, q;
const int maxN = 1e6 + 10;
int a[maxN];
int best[maxN];
mt19937 rnd(228);
const int maxP = 1e7 + 10;
int was[maxP];
int lp[maxP];
int SZ_VEC = 0;
int id_in_vec[maxP];
int cur_vec[maxN];
vector<int> primes;
const int CUT = 7;
const int MAX_PRIMES = 4;
int LP[maxP][MAX_PRIMES];
int cur_x, cur_y;
int sz[MAX_PRIMES];
int val[MAX_PRIMES];
int CNT = 0;
void clear_set() {
CNT = 0;
}
inline void gen_new(int x, int y) {
cur_x = cur_vec[x];
cur_y = cur_vec[y];
assert(cur_x != cur_y);
int d = abs(cur_x - cur_y);
assert(CNT == 0);
FOR(it, 0, MAX_PRIMES) {
int p = LP[d][it];
if (p == 0) break;
sz[CNT] = 0;
val[CNT] = p;
FOR(ps, 0, SZ_VEC) {
if (ps == x || ps == y) continue;
if ((cur_vec[ps] - cur_x) % p == 0) sz[CNT]++;
}
CNT++;
}
}
inline void add_to_vec(int x) {
id_in_vec[x] = SZ_VEC;
cur_vec[SZ_VEC++] = x;
if (SZ_VEC >= 2 && rnd() % SZ_VEC <= 1) {
clear_set();
gen_new(SZ_VEC - 1, rnd() % (SZ_VEC - 1));
}
else {
if (SZ_VEC > 2) {
FOR(i, 0, CNT) {
if ((x - cur_x) % val[i] == 0) sz[i]++;
}
}
}
}
inline void remove_from_vec(int x) {
if (cur_x == x || cur_y == x) {
clear_set();
int p = id_in_vec[x];
int y = cur_vec[SZ_VEC - 1];
if (x != y) {
id_in_vec[y] = p;
cur_vec[p] = y;
}
SZ_VEC--;
cur_x = 0;
cur_y = 0;
if (SZ_VEC >= 2) {
int p = rnd() % SZ_VEC;
int q = (p + 1 + (rnd() % (SZ_VEC - 1))) % SZ_VEC;
gen_new(p, q);
}
}
else {
int p = id_in_vec[x];
int y = cur_vec[SZ_VEC - 1];
if (x != y) {
id_in_vec[y] = p;
cur_vec[p] = y;
}
FOR(it, 0, CNT) {
if ((x - cur_x) % val[it] == 0) sz[it]--;
}
SZ_VEC--;
}
}
void init() {
primes.reserve(670'000);
FOR(i, 2, maxP) {
if (lp[i] == 0) {
lp[i] = i;
primes.pb(i);
}
for (int j = 0; j < (int)primes.size() && primes[j] <= lp[i] && i * primes[j] < maxP; j++) {
lp[i * primes[j]] = primes[j];
}
}
FOR(i, 2, maxP) {
int c = i;
int ptr = 0;
while (c > 1) {
int x = lp[c];
if (CUT == 0 || x > primes[CUT - 1]) {
LP[i][ptr++] = x;
}
while (c % x == 0) c /= x;
}
assert(ptr <= MAX_PRIMES);
}
}
int main() {
auto start = high_resolution_clock::now();
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef DEBUG
freopen("input.txt", "r", stdin);
#endif
init();
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds >(stop - start);
debug("init ", (ld)duration.count() / 1e6);
cin >> n >> q;
// n = 1e7;
// q = 1e6;
FOR(i, 0, q) {
cin >> a[i];
// a[i] = rnd() % n + 1;
if (was[a[i]]) {
was[a[i]] ^= 1;
a[i] = ~a[i];
}
else {
was[a[i]] ^= 1;
}
}
vector<int> pref(CUT + 1);
FOR(i, 0, CUT) pref[i + 1] = pref[i] + primes[i];
debug(pref[CUT]);
vector<int> cnt(pref[CUT]);
FOR(i, 0, q) {
if (a[i] > 0) {
FOR(x, 0, CUT) {
cnt[pref[x] + a[i] % primes[x]]++;
}
}
else {
FOR(x, 0, CUT) {
cnt[pref[x] + (~a[i]) % primes[x]]--;
}
}
best[i] = *max_element(cnt.begin(), cnt.end());
}
const int TL = 8.5 * 1000'000;
int ITER = 0;
while (true) {
++ITER;
// if (ITER > 200) break;
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds >(stop - start);
if (duration.count() > TL) break;
SZ_VEC = 0;
clear_set();
FOR(i, 0, q) {
if (a[i] > 0) {
//adding
add_to_vec(a[i]);
}
else {
//removing
int x = ~a[i];
remove_from_vec(x);
}
if (SZ_VEC >= 2) {
FOR(it, 0, CNT) {
upd_max(best[i], sz[it] + 2);
}
}
}
}
// std::cout << " ITER " << ITER << std::endl;
FOR(i, 0, q) cout << best[i] << '\n';
debug(ITER);
// debug(X, Y, (ld)Y / X);
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 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | //#ifdef DEBUG //#define _GLIBCXX_DEBUG //#endif #pragma GCC optimize("O3") #include<bits/stdc++.h> using namespace std; #ifdef DEBUG #include "lib/debug.h" #else #define debug(...) 228 #endif typedef long long ll; typedef long double ld; #define pb push_back #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define F0R(i, a) FOR(i, 0, a) vector<int> read_vec(int n) { vector<int> v(n); for (int& t : v) cin >> t; return v; } template<typename T> inline void upd_max(T& a, T b) { if (a < b) { a = b; } } template<typename T> inline void upd_min(T& a, T b) { if (a > b) { a = b; } } using namespace std::chrono; int n, q; const int maxN = 1e6 + 10; int a[maxN]; int best[maxN]; mt19937 rnd(228); const int maxP = 1e7 + 10; int was[maxP]; int lp[maxP]; int SZ_VEC = 0; int id_in_vec[maxP]; int cur_vec[maxN]; vector<int> primes; const int CUT = 7; const int MAX_PRIMES = 4; int LP[maxP][MAX_PRIMES]; int cur_x, cur_y; int sz[MAX_PRIMES]; int val[MAX_PRIMES]; int CNT = 0; void clear_set() { CNT = 0; } inline void gen_new(int x, int y) { cur_x = cur_vec[x]; cur_y = cur_vec[y]; assert(cur_x != cur_y); int d = abs(cur_x - cur_y); assert(CNT == 0); FOR(it, 0, MAX_PRIMES) { int p = LP[d][it]; if (p == 0) break; sz[CNT] = 0; val[CNT] = p; FOR(ps, 0, SZ_VEC) { if (ps == x || ps == y) continue; if ((cur_vec[ps] - cur_x) % p == 0) sz[CNT]++; } CNT++; } } inline void add_to_vec(int x) { id_in_vec[x] = SZ_VEC; cur_vec[SZ_VEC++] = x; if (SZ_VEC >= 2 && rnd() % SZ_VEC <= 1) { clear_set(); gen_new(SZ_VEC - 1, rnd() % (SZ_VEC - 1)); } else { if (SZ_VEC > 2) { FOR(i, 0, CNT) { if ((x - cur_x) % val[i] == 0) sz[i]++; } } } } inline void remove_from_vec(int x) { if (cur_x == x || cur_y == x) { clear_set(); int p = id_in_vec[x]; int y = cur_vec[SZ_VEC - 1]; if (x != y) { id_in_vec[y] = p; cur_vec[p] = y; } SZ_VEC--; cur_x = 0; cur_y = 0; if (SZ_VEC >= 2) { int p = rnd() % SZ_VEC; int q = (p + 1 + (rnd() % (SZ_VEC - 1))) % SZ_VEC; gen_new(p, q); } } else { int p = id_in_vec[x]; int y = cur_vec[SZ_VEC - 1]; if (x != y) { id_in_vec[y] = p; cur_vec[p] = y; } FOR(it, 0, CNT) { if ((x - cur_x) % val[it] == 0) sz[it]--; } SZ_VEC--; } } void init() { primes.reserve(670'000); FOR(i, 2, maxP) { if (lp[i] == 0) { lp[i] = i; primes.pb(i); } for (int j = 0; j < (int)primes.size() && primes[j] <= lp[i] && i * primes[j] < maxP; j++) { lp[i * primes[j]] = primes[j]; } } FOR(i, 2, maxP) { int c = i; int ptr = 0; while (c > 1) { int x = lp[c]; if (CUT == 0 || x > primes[CUT - 1]) { LP[i][ptr++] = x; } while (c % x == 0) c /= x; } assert(ptr <= MAX_PRIMES); } } int main() { auto start = high_resolution_clock::now(); ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifdef DEBUG freopen("input.txt", "r", stdin); #endif init(); auto stop = high_resolution_clock::now(); auto duration = duration_cast<microseconds >(stop - start); debug("init ", (ld)duration.count() / 1e6); cin >> n >> q; // n = 1e7; // q = 1e6; FOR(i, 0, q) { cin >> a[i]; // a[i] = rnd() % n + 1; if (was[a[i]]) { was[a[i]] ^= 1; a[i] = ~a[i]; } else { was[a[i]] ^= 1; } } vector<int> pref(CUT + 1); FOR(i, 0, CUT) pref[i + 1] = pref[i] + primes[i]; debug(pref[CUT]); vector<int> cnt(pref[CUT]); FOR(i, 0, q) { if (a[i] > 0) { FOR(x, 0, CUT) { cnt[pref[x] + a[i] % primes[x]]++; } } else { FOR(x, 0, CUT) { cnt[pref[x] + (~a[i]) % primes[x]]--; } } best[i] = *max_element(cnt.begin(), cnt.end()); } const int TL = 8.5 * 1000'000; int ITER = 0; while (true) { ++ITER; // if (ITER > 200) break; auto stop = high_resolution_clock::now(); auto duration = duration_cast<microseconds >(stop - start); if (duration.count() > TL) break; SZ_VEC = 0; clear_set(); FOR(i, 0, q) { if (a[i] > 0) { //adding add_to_vec(a[i]); } else { //removing int x = ~a[i]; remove_from_vec(x); } if (SZ_VEC >= 2) { FOR(it, 0, CNT) { upd_max(best[i], sz[it] + 2); } } } } // std::cout << " ITER " << ITER << std::endl; FOR(i, 0, q) cout << best[i] << '\n'; debug(ITER); // debug(X, Y, (ld)Y / X); return 0; } |
English