#include <bits/stdc++.h>
#define rep(i, p, k) for (int i = (p); i < (k); ++i)
#define all(v) (v).begin(), (v).end()
#define ll long long
#define fi first
#define sc second
#ifndef DEBUG
#define debug(...)
#else
#include "debug.h"
#endif
using namespace std;
const int MM = 1e7 + 13;
int sv[MM];
const int C = 50;
struct RandSet {
map<int, int> s;
vector<int> t;
mt19937 gen{0xbeeeeee};
void insert(int x) {
s[x] = (int)t.size();
t.push_back(x);
}
void erase(int x) {
auto it = s.find(x);
if (x == t.back()) {
t.pop_back();
s.erase(it);
return;
}
auto it2 = s.find(t.back());
swap(t[it->sc], t.back());
t.pop_back();
it2->sc = it->sc;
s.erase(it);
}
int rand() { return t[gen() % t.size()]; }
};
int solve3(int *t) {
int a = t[0], b = t[1], c = t[2];
if (b > c)
swap(b, c);
if (a > b)
swap(a, b);
if (b > c)
swap(b, c);
return 2 + (a + c == 2 * b);
}
struct State {
map<pair<int, int>, int> cnt;
int tot = 0;
RandSet rs;
int pp[2]{};
int result() {
if (tot <= 2)
return tot;
if (tot == 3) {
return solve3(rs.t.data());
}
int res = max(pp[0], pp[1]);
for (auto [_, r] : cnt)
res = max(res, r);
return res;
}
void add(int x) { pp[x % 2]++; }
void remove(int x) { pp[x % 2]--; }
};
void brut(int n, int q) {
int pp[2]{};
vector<int> t(n);
vector<int> pos;
vector<int> cnt(n);
auto recalc = [&]() {
int res = max(pp[0], pp[1]);
int tot = pp[0] + pp[1];
if (tot <= 2)
return tot;
if (tot == 3) {
return solve3(pos.data());
}
for (int i = 3; i * (res - 1) < n && res != tot; i++)
if (sv[i] == i) {
memset(cnt.data(), 0, i * sizeof(int));
for (auto x : pos)
cnt[x % i]++;
res = max(res, *max_element(cnt.begin(), cnt.begin() + i));
}
return res;
};
while (q--) {
int x;
cin >> x;
--x;
if (t[x] ^= 1) {
pp[x % 2]++;
pos.push_back(x);
} else {
pp[x % 2]--;
auto it = find(all(pos), x);
swap(*it, pos.back());
pos.pop_back();
}
cout << recalc() << '\n';
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
for (int i = 2; i < MM; i++)
if (!sv[i])
for (int j = i; j < MM; j += i)
sv[j] = i;
int n, q;
cin >> n >> q;
if (1LL * n * q <= 1e10) {
brut(n, q);
return 0;
}
vector<int> t(n);
State state;
while (q--) {
int x;
cin >> x;
--x;
if (t[x] ^= 1) {
state.add(x);
} else {
state.remove(x);
}
cout << state.result() << '\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 | #include <bits/stdc++.h> #define rep(i, p, k) for (int i = (p); i < (k); ++i) #define all(v) (v).begin(), (v).end() #define ll long long #define fi first #define sc second #ifndef DEBUG #define debug(...) #else #include "debug.h" #endif using namespace std; const int MM = 1e7 + 13; int sv[MM]; const int C = 50; struct RandSet { map<int, int> s; vector<int> t; mt19937 gen{0xbeeeeee}; void insert(int x) { s[x] = (int)t.size(); t.push_back(x); } void erase(int x) { auto it = s.find(x); if (x == t.back()) { t.pop_back(); s.erase(it); return; } auto it2 = s.find(t.back()); swap(t[it->sc], t.back()); t.pop_back(); it2->sc = it->sc; s.erase(it); } int rand() { return t[gen() % t.size()]; } }; int solve3(int *t) { int a = t[0], b = t[1], c = t[2]; if (b > c) swap(b, c); if (a > b) swap(a, b); if (b > c) swap(b, c); return 2 + (a + c == 2 * b); } struct State { map<pair<int, int>, int> cnt; int tot = 0; RandSet rs; int pp[2]{}; int result() { if (tot <= 2) return tot; if (tot == 3) { return solve3(rs.t.data()); } int res = max(pp[0], pp[1]); for (auto [_, r] : cnt) res = max(res, r); return res; } void add(int x) { pp[x % 2]++; } void remove(int x) { pp[x % 2]--; } }; void brut(int n, int q) { int pp[2]{}; vector<int> t(n); vector<int> pos; vector<int> cnt(n); auto recalc = [&]() { int res = max(pp[0], pp[1]); int tot = pp[0] + pp[1]; if (tot <= 2) return tot; if (tot == 3) { return solve3(pos.data()); } for (int i = 3; i * (res - 1) < n && res != tot; i++) if (sv[i] == i) { memset(cnt.data(), 0, i * sizeof(int)); for (auto x : pos) cnt[x % i]++; res = max(res, *max_element(cnt.begin(), cnt.begin() + i)); } return res; }; while (q--) { int x; cin >> x; --x; if (t[x] ^= 1) { pp[x % 2]++; pos.push_back(x); } else { pp[x % 2]--; auto it = find(all(pos), x); swap(*it, pos.back()); pos.pop_back(); } cout << recalc() << '\n'; } } int main() { cin.tie(0)->sync_with_stdio(0); for (int i = 2; i < MM; i++) if (!sv[i]) for (int j = i; j < MM; j += i) sv[j] = i; int n, q; cin >> n >> q; if (1LL * n * q <= 1e10) { brut(n, q); return 0; } vector<int> t(n); State state; while (q--) { int x; cin >> x; --x; if (t[x] ^= 1) { state.add(x); } else { state.remove(x); } cout << state.result() << '\n'; } } |
English