#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define ff first
#define ss second
#define turbo ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mid ((l+r)/2)
#define midLL ((l+r)/2LL)
#define mide ((lo+hi)/2)
#define mideLL ((lo+hi)/2LL)
#define endl '\n'
#define random_shuffle shandom_ruffle
#define lowbit(x) x&(-x)
#define bits(x) __builtin_popcount(x)
#define mins(se) (*se.begin())
#define maxs(se) (*--se.end())
#define all(x) x.begin(), x.end()
#define log2_floor(i) (i ? __builtin_clzll(1) - __builtin_clzll(i) : -1)
#define siz(cont) ((int)cont.size())
#define each(it, cont) for(auto &it : cont)
#define ein(cont) for(auto &it : cont) cin >> it;
#define mp(a, b) make_pair(a, b)
#define FOR(i, st, end) for (int i = st; i < end; i++)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
using vll = vector<pll>;
using vii = vector<pii>;
using vi = vector<int>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvi = vector<vi>;
using vvii = vector<vii>;
using vli = vector<pli>;
using vvli = vector<vli>;
using vil = vector<pil>;
using vvil = vector<vil>;
using vc = vector<char>;
using vvc = vector<vector<char>>;
using vb = vector<bool>;
using vvb = vector<vb>;
using umapii = unordered_map<int, int>;
using vumapii = vector<umapii>;
using umapiumapii = unordered_map<int, umapii>;
using namespace __gnu_pbds;
template <class K, class V>
using fast_map = gp_hash_table<
K, V,
hash<K>,
equal_to<K>,
direct_mask_range_hashing<>,
linear_probe_fn<>,
hash_standard_resize_policy<
hash_exponential_size_policy<>,
hash_load_check_resize_trigger<>,
true
>
>;
template <class K>
using fast_set = gp_hash_table<
K, null_type,
hash<K>,
equal_to<K>,
direct_mask_range_hashing<>,
linear_probe_fn<>,
hash_standard_resize_policy<
hash_exponential_size_policy<>,
hash_load_check_resize_trigger<>,
true
>
>;
#ifdef JDKURWE
void err() { std::cout << "\n"; fflush(stdout); }
template<class T, class... Ts>
void err(T arg, Ts &... args) {
std::cout << arg << ' ';
err(args...);
}
#else
void err() {}
template<class T, class... Ts>
void err(T arg, Ts &... args) {
err(args...);
}
#endif
using namespace std;
ll n;
ll pot = 1;
vi sit, sit_ind;
vi pi;
vector<fast_map<int, int>> rcnt;
fast_map<int, int> top;
int best;
fast_set<int> kam_se;
vi q_sum, q, q_min;
constexpr int STTL = 2000;
int max_pi, rethink_strat=-1;
bool strat = false;
vi cnt_clearer;
fast_set<int> se_clearer;
void sito() {
sit = sit_ind = vi(n+10);
for (ll i = 2; i < n; i++)
if (!sit[i]) {
sit[i] = i;
sit_ind[i] = siz(pi);
pi.pb(i);
for (ll j = i*i; j < n; j+=i)
if (!sit[j])
sit[j] = i;
}
}
fast_set<int> to_update;
void factor(int x) {
if (x < 2)
return;
for (;x != 1; x/=sit[x])
to_update.insert(sit[x]);
}
void map_add(int i, int r) {
int tcnt = rcnt[i][r];
top[tcnt]--;
if (tcnt == best)
best++;
rcnt[i][r]++;
top[tcnt+1]++;
cnt_clearer[i]++;
if (cnt_clearer[i] == 1)
se_clearer.insert(i);
}
void map_rem(int i, int r) {
int tcnt = rcnt[i][r];
top[tcnt]--;
if (!top[tcnt] and tcnt == best)
best--;
rcnt[i][r]--;
top[tcnt-1]++;
cnt_clearer[i]--;
if (!cnt_clearer[i])
se_clearer.erase(i);
}
void add(int x) {
FOR(i, 0, siz(pi)) {
if (pi[i] > max_pi)
break;
int r = x%pi[i];
map_add(i, r);
}
}
void rem(int x) {
FOR(i, 0, siz(pi)) {
if (pi[i] > max_pi)
break;
int r = x%pi[i];
map_rem(i, r);
}
}
void add_small_q(int x, fast_set<int> &kse=kam_se) {
to_update.clear();
for (auto it : kse)
factor(abs(it-x));
for (auto it : to_update) {
int r = x%it;
int old = rcnt[sit_ind[it]][r];
if (old == 0)
map_add(sit_ind[it], r);
map_add(sit_ind[it], r);
}
}
void rem_small_q(int x) {
to_update.clear();
for (auto it : kam_se)
factor(abs(it-x));
for (auto it : to_update) {
int r = x%it;
map_rem(sit_ind[it], r);
}
}
void init_strat() {
vb kam(n+1);
int qq; cin >> qq;
q = q_sum = q_min = vi(qq);
set<pii> mi_se;
int sum = 0;
for (int i = 0; i < qq; i++) {
cin >> q[i];
if (!kam[q[i]]) {
sum++;
kam[q[i]] = true;
}
else {
kam[q[i]] = false;
sum--;
}
q_sum[i] = sum;
}
for (int i = siz(q)-1; i >= 0; i--) {
mi_se.insert({q_sum[i], i});
if (i+STTL < siz(q))
mi_se.erase({q_sum[i+STTL], i+STTL});
q_min[i] = mins(mi_se).ff;
}
}
void clear_for_strat() {
top.clear();
best = 0;
for (auto it : se_clearer) {
rcnt[it].clear();
cnt_clearer[it] = 0;
}
se_clearer.clear();
}
void switch_to_small() {
clear_for_strat();
fast_set<int> kam_copy;
for (auto it : kam_se) {
kam_copy.insert(it);
add_small_q(it, kam_copy);
}
}
void switch_to_big() {
clear_for_strat();
for (auto it : kam_se)
add(it);
}
void rescale(int new_mx) {
if (new_mx > max_pi) {
int start = 0;
while (start < siz(pi) and pi[start] <= max_pi)
start++;
max_pi = new_mx;
for (int i = start; i < siz(pi); i++) {
if (pi[i] > max_pi)
break;
for (auto it : kam_se) {
int r = it%pi[i];
map_add(i, r);
}
}
return;
}
// scale down
for (int i = siz(pi)-1; i >= 0 and pi[i] > new_mx; i--)
if (cnt_clearer[i])
for (auto it : kam_se) {
int r = it%pi[i];
map_rem(i, r);
}
max_pi = new_mx;
}
void strat_check(int i) {
if (i < rethink_strat)
return;
if (strat) {
if (q_min[i] >= STTL*2) {
int lower = q_min[i]/2;
int new_mx = (n+lower-1)/lower;
rescale(new_mx);
rethink_strat+=STTL;
}
else {
switch_to_small();
strat = false;
rethink_strat += STTL;
}
return;
}
if (q_min[i] >= STTL*2) {
max_pi = n/STTL;
rethink_strat+=STTL;
strat = true;
switch_to_big();
}
else {
rethink_strat+=100;
}
}
signed main() {
turbo;
cin >> n;
sito();
rcnt = vector<fast_map<int, int>>(siz(pi));
best = 0;
q_sum = vi(q);
cnt_clearer = vi(siz(pi));
init_strat();
rethink_strat = STTL;
for (int i = 0; i < siz(q); i++) {
int x = q[i];
if (kam_se.find(x) != kam_se.end()) {
kam_se.erase(x);
if (strat)
rem(x);
else
rem_small_q(x);
}
else {
kam_se.insert(x);
if (strat)
add(x);
else
add_small_q(x);
}
if (best == 0 and !kam_se.empty())
cout << 1 << endl;
else if (kam_se.empty())
cout << 0 << endl;
else
cout << best << endl;
if (i == rethink_strat)
strat_check(i);
}
}
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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | #include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/hash_policy.hpp> #define pb push_back #define ppb pop_back #define eb emplace_back #define ff first #define ss second #define turbo ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define mid ((l+r)/2) #define midLL ((l+r)/2LL) #define mide ((lo+hi)/2) #define mideLL ((lo+hi)/2LL) #define endl '\n' #define random_shuffle shandom_ruffle #define lowbit(x) x&(-x) #define bits(x) __builtin_popcount(x) #define mins(se) (*se.begin()) #define maxs(se) (*--se.end()) #define all(x) x.begin(), x.end() #define log2_floor(i) (i ? __builtin_clzll(1) - __builtin_clzll(i) : -1) #define siz(cont) ((int)cont.size()) #define each(it, cont) for(auto &it : cont) #define ein(cont) for(auto &it : cont) cin >> it; #define mp(a, b) make_pair(a, b) #define FOR(i, st, end) for (int i = st; i < end; i++) using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using pli = pair<ll, int>; using pil = pair<int, ll>; using vll = vector<pll>; using vii = vector<pii>; using vi = vector<int>; using vl = vector<ll>; using vvl = vector<vl>; using vvi = vector<vi>; using vvii = vector<vii>; using vli = vector<pli>; using vvli = vector<vli>; using vil = vector<pil>; using vvil = vector<vil>; using vc = vector<char>; using vvc = vector<vector<char>>; using vb = vector<bool>; using vvb = vector<vb>; using umapii = unordered_map<int, int>; using vumapii = vector<umapii>; using umapiumapii = unordered_map<int, umapii>; using namespace __gnu_pbds; template <class K, class V> using fast_map = gp_hash_table< K, V, hash<K>, equal_to<K>, direct_mask_range_hashing<>, linear_probe_fn<>, hash_standard_resize_policy< hash_exponential_size_policy<>, hash_load_check_resize_trigger<>, true > >; template <class K> using fast_set = gp_hash_table< K, null_type, hash<K>, equal_to<K>, direct_mask_range_hashing<>, linear_probe_fn<>, hash_standard_resize_policy< hash_exponential_size_policy<>, hash_load_check_resize_trigger<>, true > >; #ifdef JDKURWE void err() { std::cout << "\n"; fflush(stdout); } template<class T, class... Ts> void err(T arg, Ts &... args) { std::cout << arg << ' '; err(args...); } #else void err() {} template<class T, class... Ts> void err(T arg, Ts &... args) { err(args...); } #endif using namespace std; ll n; ll pot = 1; vi sit, sit_ind; vi pi; vector<fast_map<int, int>> rcnt; fast_map<int, int> top; int best; fast_set<int> kam_se; vi q_sum, q, q_min; constexpr int STTL = 2000; int max_pi, rethink_strat=-1; bool strat = false; vi cnt_clearer; fast_set<int> se_clearer; void sito() { sit = sit_ind = vi(n+10); for (ll i = 2; i < n; i++) if (!sit[i]) { sit[i] = i; sit_ind[i] = siz(pi); pi.pb(i); for (ll j = i*i; j < n; j+=i) if (!sit[j]) sit[j] = i; } } fast_set<int> to_update; void factor(int x) { if (x < 2) return; for (;x != 1; x/=sit[x]) to_update.insert(sit[x]); } void map_add(int i, int r) { int tcnt = rcnt[i][r]; top[tcnt]--; if (tcnt == best) best++; rcnt[i][r]++; top[tcnt+1]++; cnt_clearer[i]++; if (cnt_clearer[i] == 1) se_clearer.insert(i); } void map_rem(int i, int r) { int tcnt = rcnt[i][r]; top[tcnt]--; if (!top[tcnt] and tcnt == best) best--; rcnt[i][r]--; top[tcnt-1]++; cnt_clearer[i]--; if (!cnt_clearer[i]) se_clearer.erase(i); } void add(int x) { FOR(i, 0, siz(pi)) { if (pi[i] > max_pi) break; int r = x%pi[i]; map_add(i, r); } } void rem(int x) { FOR(i, 0, siz(pi)) { if (pi[i] > max_pi) break; int r = x%pi[i]; map_rem(i, r); } } void add_small_q(int x, fast_set<int> &kse=kam_se) { to_update.clear(); for (auto it : kse) factor(abs(it-x)); for (auto it : to_update) { int r = x%it; int old = rcnt[sit_ind[it]][r]; if (old == 0) map_add(sit_ind[it], r); map_add(sit_ind[it], r); } } void rem_small_q(int x) { to_update.clear(); for (auto it : kam_se) factor(abs(it-x)); for (auto it : to_update) { int r = x%it; map_rem(sit_ind[it], r); } } void init_strat() { vb kam(n+1); int qq; cin >> qq; q = q_sum = q_min = vi(qq); set<pii> mi_se; int sum = 0; for (int i = 0; i < qq; i++) { cin >> q[i]; if (!kam[q[i]]) { sum++; kam[q[i]] = true; } else { kam[q[i]] = false; sum--; } q_sum[i] = sum; } for (int i = siz(q)-1; i >= 0; i--) { mi_se.insert({q_sum[i], i}); if (i+STTL < siz(q)) mi_se.erase({q_sum[i+STTL], i+STTL}); q_min[i] = mins(mi_se).ff; } } void clear_for_strat() { top.clear(); best = 0; for (auto it : se_clearer) { rcnt[it].clear(); cnt_clearer[it] = 0; } se_clearer.clear(); } void switch_to_small() { clear_for_strat(); fast_set<int> kam_copy; for (auto it : kam_se) { kam_copy.insert(it); add_small_q(it, kam_copy); } } void switch_to_big() { clear_for_strat(); for (auto it : kam_se) add(it); } void rescale(int new_mx) { if (new_mx > max_pi) { int start = 0; while (start < siz(pi) and pi[start] <= max_pi) start++; max_pi = new_mx; for (int i = start; i < siz(pi); i++) { if (pi[i] > max_pi) break; for (auto it : kam_se) { int r = it%pi[i]; map_add(i, r); } } return; } // scale down for (int i = siz(pi)-1; i >= 0 and pi[i] > new_mx; i--) if (cnt_clearer[i]) for (auto it : kam_se) { int r = it%pi[i]; map_rem(i, r); } max_pi = new_mx; } void strat_check(int i) { if (i < rethink_strat) return; if (strat) { if (q_min[i] >= STTL*2) { int lower = q_min[i]/2; int new_mx = (n+lower-1)/lower; rescale(new_mx); rethink_strat+=STTL; } else { switch_to_small(); strat = false; rethink_strat += STTL; } return; } if (q_min[i] >= STTL*2) { max_pi = n/STTL; rethink_strat+=STTL; strat = true; switch_to_big(); } else { rethink_strat+=100; } } signed main() { turbo; cin >> n; sito(); rcnt = vector<fast_map<int, int>>(siz(pi)); best = 0; q_sum = vi(q); cnt_clearer = vi(siz(pi)); init_strat(); rethink_strat = STTL; for (int i = 0; i < siz(q); i++) { int x = q[i]; if (kam_se.find(x) != kam_se.end()) { kam_se.erase(x); if (strat) rem(x); else rem_small_q(x); } else { kam_se.insert(x); if (strat) add(x); else add_small_q(x); } if (best == 0 and !kam_se.empty()) cout << 1 << endl; else if (kam_se.empty()) cout << 0 << endl; else cout << best << endl; if (i == rethink_strat) strat_check(i); } } |
English