#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
////// kod pochodzi z tego bloga: https://codeforces.com/blog/entry/62393
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
///////
pii jump[10000001];
int prime_num[10000001];
unordered_map<ll,int,custom_hash> modulos;
unordered_set<ll,custom_hash> czy_mam[1000001];
int modulos2[50][250];
int rel_prime[700000];
int is[10000001];
vi opers;
int n,q;
int ans_cnt[1000003];
int cur_ans = 0;
vi cur_vect;
void add_pair(int a, int b)
{
int d = abs(opers[a]-opers[b]);
while(d > 1 && jump[d].ff != -1)
{
int p = jump[d].ff;
if(czy_mam[a].find(p) == czy_mam[a].end())
{
ans_cnt[++modulos[p+(opers[a]%rel_prime[p])*1e9]]++;
czy_mam[a].insert(p);
}
if(czy_mam[b].find(p) == czy_mam[b].end())
{
ans_cnt[++modulos[p+(opers[b]%rel_prime[p])*1e9]]++;
czy_mam[b].insert(p);
}
d = jump[d].ss;
}
}
void add_num(int ind)
{
rep(i,siz(cur_vect))
{
if(is[opers[cur_vect[i]]] != cur_vect[i])
{
swap(cur_vect[i],cur_vect[siz(cur_vect)-1]);
cur_vect.pop_back();
i--;
continue;
}
add_pair(cur_vect[i],ind);
}
cur_vect.pb(ind);
rep2(i,1,49) ans_cnt[++modulos2[i][opers[ind]%rel_prime[i]]]++;
}
void del_num(int ind)
{
forall(it,czy_mam[ind]) ans_cnt[modulos[it+(opers[ind]%rel_prime[it])*1e9]--]--;
rep2(i,1,49) ans_cnt[modulos2[i][opers[ind]%rel_prime[i]]--]--;
}
void fix_ans()
{
if(cur_ans == 0) cur_ans = 1;
if(ans_cnt[cur_ans+1] != 0) cur_ans++;
if(ans_cnt[cur_ans] == 0) cur_ans--;
if(cur_ans == 0) cur_ans = 1;
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
random_start();
int cnt = 1;
rep2(i,2,10000000)
{
if(jump[i].ff == 0)
{
rel_prime[cnt] = i;
prime_num[i] = cnt;
jump[i].ff = cnt;
if((ll)i*(ll)i > 10000000)
{
cnt++;
continue;
}
for(int j = i*i; j <= 10000000; j+=i) jump[j].ff = cnt;
cnt++;
}
}
int x;
int d;
rep2(i,2,10000000)
{
if(jump[i].ff < 50)
{
jump[i].ff = -1;
continue;
}
x = i;
d = rel_prime[jump[i].ff];
while(x%d == 0) x /= d;
jump[i].ss = x;
}
cin >> n >> q;
opers.resize(q);
rep(i,q)
{
cin >> opers[i];
is[opers[i]] = -1;
}
ans_cnt[0] = 1e9;
int ile = 0;
rep(i,q)
{
if(is[opers[i]] == -1)
{
ile++;
add_num(i);
is[opers[i]] = i;
}
else
{
ile--;
del_num(is[opers[i]]);
is[opers[i]] = -1;
}
fix_ans();
if(ile != 0) cout << cur_ans << "\n";
else cout << "0\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 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 | #include <bits/stdc++.h> #pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define ld long double #define ull unsigned long long #define ff first #define ss second #define pii pair<int,int> #define pll pair<long long, long long> #define vi vector<int> #define vl vector<long long> #define pb push_back #define rep(i, b) for(int i = 0; i < (b); ++i) #define rep2(i,a,b) for(int i = a; i <= (b); ++i) #define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c) #define count_bits(x) __builtin_popcountll((x)) #define all(x) (x).begin(),(x).end() #define siz(x) (int)(x).size() #define forall(it,x) for(auto& it:(x)) using namespace __gnu_pbds; using namespace std; typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set; mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());} ll los(ll a, ll b) {return a + (mt() % (b-a+1));} const int INF = 1e9+50; const ll INF_L = 1e18+40; const ll MOD = 1e9+7; ////// kod pochodzi z tego bloga: https://codeforces.com/blog/entry/62393 struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; /////// pii jump[10000001]; int prime_num[10000001]; unordered_map<ll,int,custom_hash> modulos; unordered_set<ll,custom_hash> czy_mam[1000001]; int modulos2[50][250]; int rel_prime[700000]; int is[10000001]; vi opers; int n,q; int ans_cnt[1000003]; int cur_ans = 0; vi cur_vect; void add_pair(int a, int b) { int d = abs(opers[a]-opers[b]); while(d > 1 && jump[d].ff != -1) { int p = jump[d].ff; if(czy_mam[a].find(p) == czy_mam[a].end()) { ans_cnt[++modulos[p+(opers[a]%rel_prime[p])*1e9]]++; czy_mam[a].insert(p); } if(czy_mam[b].find(p) == czy_mam[b].end()) { ans_cnt[++modulos[p+(opers[b]%rel_prime[p])*1e9]]++; czy_mam[b].insert(p); } d = jump[d].ss; } } void add_num(int ind) { rep(i,siz(cur_vect)) { if(is[opers[cur_vect[i]]] != cur_vect[i]) { swap(cur_vect[i],cur_vect[siz(cur_vect)-1]); cur_vect.pop_back(); i--; continue; } add_pair(cur_vect[i],ind); } cur_vect.pb(ind); rep2(i,1,49) ans_cnt[++modulos2[i][opers[ind]%rel_prime[i]]]++; } void del_num(int ind) { forall(it,czy_mam[ind]) ans_cnt[modulos[it+(opers[ind]%rel_prime[it])*1e9]--]--; rep2(i,1,49) ans_cnt[modulos2[i][opers[ind]%rel_prime[i]]--]--; } void fix_ans() { if(cur_ans == 0) cur_ans = 1; if(ans_cnt[cur_ans+1] != 0) cur_ans++; if(ans_cnt[cur_ans] == 0) cur_ans--; if(cur_ans == 0) cur_ans = 1; } int main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); random_start(); int cnt = 1; rep2(i,2,10000000) { if(jump[i].ff == 0) { rel_prime[cnt] = i; prime_num[i] = cnt; jump[i].ff = cnt; if((ll)i*(ll)i > 10000000) { cnt++; continue; } for(int j = i*i; j <= 10000000; j+=i) jump[j].ff = cnt; cnt++; } } int x; int d; rep2(i,2,10000000) { if(jump[i].ff < 50) { jump[i].ff = -1; continue; } x = i; d = rel_prime[jump[i].ff]; while(x%d == 0) x /= d; jump[i].ss = x; } cin >> n >> q; opers.resize(q); rep(i,q) { cin >> opers[i]; is[opers[i]] = -1; } ans_cnt[0] = 1e9; int ile = 0; rep(i,q) { if(is[opers[i]] == -1) { ile++; add_num(i); is[opers[i]] = i; } else { ile--; del_num(is[opers[i]]); is[opers[i]] = -1; } fix_ans(); if(ile != 0) cout << cur_ans << "\n"; else cout << "0\n"; } } |
English