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
#include <iostream>
#include <set>
#include <map>

using namespace std;

int main(int argc, char** argv) {
    
    int N, Q;
    cin >> N >> Q;

    int x;

    set<pair<int, pair<int, int>>> posortowane;
    map<pair<int, int>, int> pary;
    set<int> active;

    for (int i=0; i<Q; i++) {
        cin >> x;
        if (active.count(x)) {
            active.erase(x);
            for (int dziel=2; dziel < N; dziel ++) {
                int reszta = x % dziel;
                int cnt = pary[{dziel, reszta}];
                pary[{dziel, reszta}]--;
                posortowane.erase({cnt, {dziel, reszta}});
                cnt--;
                if (cnt > 0)
                    posortowane.insert({cnt, {dziel, reszta}});
            }
        } else {
            active.insert(x);
            for (int dziel=2; dziel < N; dziel ++) {
                int reszta = x % dziel;
                int cnt = pary[{dziel, reszta}];
                pary[{dziel, reszta}]++;
                cnt++;
                if (cnt > 0)
                    posortowane.erase({cnt, {dziel, reszta}});
                posortowane.insert({cnt, {dziel, reszta}});
            }

        }
        // obsluzyc 0
        if (posortowane.empty()) {
            cout << 0 << endl;
        } else {
            cout << prev(posortowane.end())->first << endl;
        }
    }
}