MEX maximizing

Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples:

for the array [0,0,1,0,2][0,0,1,0,2] MEX equals to 3 because numbers 0,1 and 2 are presented in the array and 3 is the minimum non-negative integer not presented in the array;
for the array [1,2,3,4][1,2,3,4] MEX equals to 0 because 0 is the minimum non-negative integer not presented in the array;
for the array [0,1,4,3][0,1,4,3] MEX equals to 2 because 2 is the minimum non-negative integer not presented in the array.
You are given an empty array a=[]a=[] (in other words, a zero-length array). You are also given a positive integer x.

You are also given q queries. The j-th query consists of one integer yj and means that you have to append one element yjy_j to the array. The array length increases by 1 after a query.

In one move, you can choose any index ii and set ai:=ai+xa_i:=a_i+x or ai:=aixa_i:=a_i−x (i.e. increase or decrease any element of the array by x). The only restriction is that aia_i cannot become negative. Since initially the array is empty, you can perform moves only after the first query.

You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).

You have to find the answer after each of q queries (i.e. the j-th answer corresponds to the array of length j).

Operations are discarded before each query. I.e. the array a after the j-th query equals to [y1,y2,,yj][y_1,y_2,…,y_j].

Input
The first line of the input contains two integers q,x(1q,x4105)q,x (1≤q,x≤4⋅10^5) — the number of queries and the value of xx.

The next qq lines describe queries. The j-th query consists of one integer yj(0yj109)y_j(0≤y_j≤10^9) and means that you have to append one element yjy_j to the array.

Output
Print the answer to the initial problem after each query — for the query j print the maximum value of MEX after first j queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.

Examples

input
7 3
0
1
2
2
0
0
10
output
1
2
3
3
4
4
7
input
4 3
1
2
1
2
output
0
0
0
0

Note
In the first example:

After the first query, the array is a=[0]a=[0]: you don’t need to perform any operations, maximum possible MEX is 1.
After the second query, the array is a=[0,1]a=[0,1]: you don’t need to perform any operations, maximum possible MEX is 2.
After the third query, the array is a=[0,1,2]a=[0,1,2]: you don’t need to perform any operations, maximum possible MEX is 3.
After the fourth query, the array is a=[0,1,2,2]a=[0,1,2,2]: you don’t need to perform any operations, maximum possible MEX is 3 (you can’t make it greater with operations).
After the fifth query, the array is a=[0,1,2,2,0]a=[0,1,2,2,0]: you can perform a[4]:=a[4]+3=3a[4]:=a[4]+3=3. The array changes to be a=[0,1,2,2,3]a=[0,1,2,2,3]. Now MEX is maximum possible and equals to 4.
After the sixth query, the array is a=[0,1,2,2,0,0]a=[0,1,2,2,0,0]: you can perform a[4]:=a[4]+3=0+3=3a[4]:=a[4]+3=0+3=3. The array changes to be a=[0,1,2,2,3,0]a=[0,1,2,2,3,0]. Now MEX is maximum possible and equals to 4.
After the seventh query, the array is a=[0,1,2,2,0,0,10]a=[0,1,2,2,0,0,10]. You can perform the following operations:
a[3]:=a[3]+3=2+3=5,a[4]:=a[4]+3=0+3=3,a[5]:=a[5]+3=0+3=3,a[5]:=a[5]+3=3+3=6,a[6]:=a[6]3=103=7,a[6]:=a[6]3=73=4.a[3]:=a[3]+3=2+3=5,\\ a[4]:=a[4]+3=0+3=3,\\ a[5]:=a[5]+3=0+3=3,\\ a[5]:=a[5]+3=3+3=6,\\ a[6]:=a[6]−3=10−3=7,\\ a[6]:=a[6]−3=7−3=4.
The resulting array will be a=[0,1,2,5,3,6,4]a=[0,1,2,5,3,6,4]. Now MEX is maximum possible and equals to 7.
由於題目中沒有限制操作範圍,因此一個數num1num_1可以變成任意一個數num2num_2,使得num1modx=num2modxnum_1\bmod x=num_2\bmod x。於是可以定義一個數組cntcntcnt[y]cnt[y]來存儲模xxyy的數的個數,顯然MEX=min(cnt[i]×x+i)i[0,x1]MEX=min(cnt[i]×x+i)\quad\forall i\in\mathbb [0,x-1],可以利用線段樹維護。複雜度爲O(qlogx)O(qlogx)

#include<bits/stdc++.h>

#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;

inline int qr() {
    int f = 0, fu = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-') {
            fu = -1;
        }
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        f = (f << 3) + (f << 1) + c - 48;
        c = getchar();
    }
    return f * fu;
}

const int N = 4e5 + 10;

struct Tree {
    struct {
        int l, r;
        pii dat;
    } t[N * 4];

    void build(int p, int l, int r) {
        t[p].l = l, t[p].r = r;
        if (l == r) {
            t[p].dat = {0, l};
            return;
        }
        int mid = (l + r) >> 1;
        build(p << 1, l, mid);
        build(p << 1 | 1, mid + 1, r);
        if (t[p << 1].dat.fi <= t[p << 1 | 1].dat.fi)t[p].dat = t[p << 1].dat;
        else t[p].dat = t[p << 1 | 1].dat;
    }

    void change(int p, int x) {
        if (t[p].l == t[p].r) {
            t[p].dat.fi++;
            return;
        }
        int mid = (t[p].l + t[p].r) >> 1;
        if (x <= mid)change(p << 1, x);
        else change(p << 1 | 1, x);
        if (t[p << 1].dat.fi <= t[p << 1 | 1].dat.fi)t[p].dat = t[p << 1].dat;
        else t[p].dat = t[p << 1 | 1].dat;
    }
} tr;

int q, x;

int main() {
    q = qr(), x = qr();
    tr.build(1, 1, x);
    while (q--) {
        tr.change(1, qr() % x + 1);
        pi(tr.t[1].dat.fi * x + tr.t[1].dat.se - 1);
    }
    return 0;
}

觀察發現,在向序列中添加元素的過程中MEXMEX是單調非遞減的,因此只需維護最值,即MEXMEX。時間複雜度爲O(x+q)O(x+q)

#include<bits/stdc++.h>

#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;

inline int qr() {
    int f = 0, fu = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-') {
            fu = -1;
        }
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        f = (f << 3) + (f << 1) + c - 48;
        c = getchar();
    }
    return f * fu;
}

const int N = 4e5 + 10;

int cnt[N], ans, q, x;

int main() {
    q = qr(), x = qr();
    while (q--) {
        cnt[qr() % x]++;
        while (cnt[ans % x])
            cnt[ans % x]--, ans++;
        pi(ans);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章