/* -----------------------
Autor: Tomasz Boguslawski
-------------------------- */
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<sstream>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include <fstream>
#include<math.h>
#include <random>
#define LL long long
#define FOR(x, b, e) for(LL x = b; x <= (e); x++)
#define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s)
#define FORD(x, b, e) for(LL x = b; x >= (e); x--)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define DEBUG if (debug)
#define MIN(a,b) ((a>b)?b:a)
#define MAX(a,b) ((a>b)?a:b)
using namespace std;
random_device dev;
mt19937 rng(dev());
class Maxer
{
LL* T;
//LL* I;
public:
LL M;
Maxer(LL required)
{
M=2; while (M<required) M*=2;
T=new LL[2*M+2];
//I=new LL[2*M+2];
FOR(i,0,2*M) { T[i]=0; } //I[i]=-1; }
//FOR(i,M,2*M) I[i]=i;
//I[1]=M;
//cout << "M=" << M << "\n";
}
LL getMax() { return T[1]; }
void setValue(LL index, LL value)
{
T[index]=value;
LL curr=index/2;
LL newValue, child;
while(curr>=1)
{
child=curr*2;
newValue=T[child]; //newInd=I[child];
child++;
if (T[child]>newValue) { newValue=T[child]; } //newInd=I[child]; }
//if ((T[curr]==newValue) && (I[curr]==newInd)) return;
if (T[curr]==newValue) return;
T[curr]=newValue; //I[curr]=newInd;
curr/=2;
}
}
};
bool isPrime[10000005];
LL primes[1000000];
LL primeCount;
void findPrimes(LL upTo)
{
FOR(i,1,upTo) isPrime[i]=true;
isPrime[0]=false; isPrime[1]=false;
LL p=2; LL c;
while (p*p<=upTo)
{
if (isPrime[p])
{
c = p*p;
while (c<=upTo) { isPrime[c]=false; c+=p; }
}
p+=1;
}
primeCount=0;
FOR(i,1,upTo) if (isPrime[i]) primes[++primeCount]=i;
}
LL* mods[1001];
LL* indx[1001];
LL primeForIndex[4200000];
LL modForIndex[4200000];
LL primesUsed;
Maxer* mx;
LL M;
bool state[10000007];
void prepare()
{
findPrimes(100000);
LL sum=0; FOR(i,1,primesUsed) sum+=primes[i];
mx=new Maxer(sum);
M=mx->M;
//cout << "Sum=" << sum << " M=" << M << "\n";
LL index=M;
FOR(i,1,primesUsed)
{
//cout << i << ": " << primes[i] << "\n"; cout.flush();
mods[i]=new LL[primes[i]];
indx[i]=new LL[primes[i]];
FOR(j,0,primes[i]-1)
{
mods[i][j]=0;
indx[i][j]=index;
primeForIndex[index-M]=i;
modForIndex[index-M]=j;
index++;
}
}
//cout << "lastPrime=" << primes[primesUsed] << " sum=" << sum << " M=" << M << "\n";
}
void handle(LL field)
{
state[field]=!(state[field]);
//cout << "handling field " << field << "; state changed to: " << state[field] << "\n"; cout.flush();
bool adding=state[field];
LL prime; LL modulo; LL index;
FOR(k,1,primesUsed)
{
prime=primes[k];
modulo=field%prime;
//cout << " k=" << k << " prime=" << prime << " modulo=" << modulo << "\n";
if (adding) mods[k][modulo]++; else mods[k][modulo]--;
index=indx[k][modulo];
//cout << " index=" << index << "\n";
mx->setValue(index, mods[k][modulo]);
}
cout << mx->getMax() << "\n";
}
LL n,q;
/// MAIN
int main(int argc, char* argv[])
{
// magic formula, which makes streams work faster:
ios_base::sync_with_stdio(0);
primesUsed=1000;
prepare();
n=10000000;
q=1000000;
FOR(i,1,n) state[i]=false;
cin >> n; cin >> q;
FOR(i,1,n) state[i]=false;
LL field;
FOR(t,1,q)
{
cin >> field;
handle(field);
}
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 | /* ----------------------- Autor: Tomasz Boguslawski -------------------------- */ #include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<sstream> #include<cstring> #include<map> #include<vector> #include<set> #include<queue> #include<algorithm> #include <fstream> #include<math.h> #include <random> #define LL long long #define FOR(x, b, e) for(LL x = b; x <= (e); x++) #define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s) #define FORD(x, b, e) for(LL x = b; x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG if (debug) #define MIN(a,b) ((a>b)?b:a) #define MAX(a,b) ((a>b)?a:b) using namespace std; random_device dev; mt19937 rng(dev()); class Maxer { LL* T; //LL* I; public: LL M; Maxer(LL required) { M=2; while (M<required) M*=2; T=new LL[2*M+2]; //I=new LL[2*M+2]; FOR(i,0,2*M) { T[i]=0; } //I[i]=-1; } //FOR(i,M,2*M) I[i]=i; //I[1]=M; //cout << "M=" << M << "\n"; } LL getMax() { return T[1]; } void setValue(LL index, LL value) { T[index]=value; LL curr=index/2; LL newValue, child; while(curr>=1) { child=curr*2; newValue=T[child]; //newInd=I[child]; child++; if (T[child]>newValue) { newValue=T[child]; } //newInd=I[child]; } //if ((T[curr]==newValue) && (I[curr]==newInd)) return; if (T[curr]==newValue) return; T[curr]=newValue; //I[curr]=newInd; curr/=2; } } }; bool isPrime[10000005]; LL primes[1000000]; LL primeCount; void findPrimes(LL upTo) { FOR(i,1,upTo) isPrime[i]=true; isPrime[0]=false; isPrime[1]=false; LL p=2; LL c; while (p*p<=upTo) { if (isPrime[p]) { c = p*p; while (c<=upTo) { isPrime[c]=false; c+=p; } } p+=1; } primeCount=0; FOR(i,1,upTo) if (isPrime[i]) primes[++primeCount]=i; } LL* mods[1001]; LL* indx[1001]; LL primeForIndex[4200000]; LL modForIndex[4200000]; LL primesUsed; Maxer* mx; LL M; bool state[10000007]; void prepare() { findPrimes(100000); LL sum=0; FOR(i,1,primesUsed) sum+=primes[i]; mx=new Maxer(sum); M=mx->M; //cout << "Sum=" << sum << " M=" << M << "\n"; LL index=M; FOR(i,1,primesUsed) { //cout << i << ": " << primes[i] << "\n"; cout.flush(); mods[i]=new LL[primes[i]]; indx[i]=new LL[primes[i]]; FOR(j,0,primes[i]-1) { mods[i][j]=0; indx[i][j]=index; primeForIndex[index-M]=i; modForIndex[index-M]=j; index++; } } //cout << "lastPrime=" << primes[primesUsed] << " sum=" << sum << " M=" << M << "\n"; } void handle(LL field) { state[field]=!(state[field]); //cout << "handling field " << field << "; state changed to: " << state[field] << "\n"; cout.flush(); bool adding=state[field]; LL prime; LL modulo; LL index; FOR(k,1,primesUsed) { prime=primes[k]; modulo=field%prime; //cout << " k=" << k << " prime=" << prime << " modulo=" << modulo << "\n"; if (adding) mods[k][modulo]++; else mods[k][modulo]--; index=indx[k][modulo]; //cout << " index=" << index << "\n"; mx->setValue(index, mods[k][modulo]); } cout << mx->getMax() << "\n"; } LL n,q; /// MAIN int main(int argc, char* argv[]) { // magic formula, which makes streams work faster: ios_base::sync_with_stdio(0); primesUsed=1000; prepare(); n=10000000; q=1000000; FOR(i,1,n) state[i]=false; cin >> n; cin >> q; FOR(i,1,n) state[i]=false; LL field; FOR(t,1,q) { cin >> field; handle(field); } return 0; } |
English