Codeforces 940 F - Machine Learning(带修改莫队, 及注意事项)

F - Machine Learning

You come home and fell some unpleasant smell. Where is it coming from?

You are given an array a. You have to answer the following queries:

  1. You are given two integers l and r. Let ci be the number of occurrences of i in al: r, where al: r is the subarray of a from l-th element to r-th inclusive. Find the Mex of {c0, c1, ..., c109}
  2. You are given two integers p to x. Change ap to x.

The Mex of a multiset of numbers is the smallest non-negative integer not in the set.

Note that in this problem all elements of a are positive, which means that c0 = 0 and 0 is never the answer for the query of the second type.

Input

The first line of input contains two integers n and q (1 ≤ n, q ≤ 100 000) — the length of the array and the number of queries respectively.

The second line of input contains n integers — a1, a2, ..., an (1 ≤ ai ≤ 109).

Each of the next q lines describes a single query.

The first type of query is described by three integers ti = 1, liri, where 1 ≤ li ≤ ri ≤ n — the bounds of the subarray.

The second type of query is described by three integers ti = 2, pixi, where 1 ≤ pi ≤ n is the index of the element, which must be changed and 1 ≤ xi ≤ 109 is the new value.

Output

For each query of the first type output a single integer  — the Mex of {c0, c1, ..., c109}.

Example

input

10 4
1 2 3 1 1 2 2 2 9 9
1 1 1
1 2 8
2 7 1
1 2 8

output

2
3
2

Note

The subarray of the first query consists of the single element — 1.

The subarray of the second query consists of four 2s, one 3 and two 1s.

The subarray of the fourth query consists of three 1s, three 2s and one 3.

 

题意:给一个长度为 n 的数组,有询问和单点修改操作。每个询问给定 l 和 r,设 al 到 ar 中的数出现的次数为序列  c,设标准的序列 c 为 1,2,3,4,5,6,...,n,输出第一个不连续的数。修改 p 位置的值为 x

思路:首先是,你需要知道 带修莫队的复杂度 O(n^{\frac{5}{3}}),这里 n 为 1e5,算出来大概是 2e8 的复杂度,所以莫队的相关操作 只能是O(1)的,那么对于 数组 以及修改的值,都需要提前全部处理好。

而对于答案,则可以暴力求。把答案设为最大,则该数列至少含 ans * (ans - 1) / 2个元素,由于序列长度在1e5的范围,则ans < 448,所以暴力求是没问题的。。

故需要做的:记录每个数出现的次数,以及每个次数出现的次数即可。!注意:由于出现次数会作为下标,莫队的del 的值是可能为负数的,所以需要先扩大范围,再缩小范围。   对于出现次数,最大出现次数不止 1e5,空间最好开大一些。

Code:

#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define fi first
#define se second
#define CLR(a) while(!(a).empty()) a.pop()

using namespace std;

const int maxn = 1e5 + 10;
int n,q,Ls[maxn * 2],en;
int belong[maxn],a[maxn];
int ans[maxn],Ans = 1;
int cnt1[maxn * 2];
int cnt2[maxn * 2];
struct xx{
    int l,r,id,time;
}Q[maxn];
struct change{
    int pos,val;
}cge[maxn];

inline int read() {
    int X = 0, w = 0;
    char ch = 0;
    while(!isdigit(ch)) {
        w |= ch == '-';
        ch = getchar();
    }
    while(isdigit(ch))
        X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}

bool cmp(xx A,xx B){
    if(belong[A.l] != belong[B.l])
        return belong[A.l] < belong[B.l];
    if(belong[A.r] != belong[B.r])
        return belong[A.r] < belong[B.r];
    return A.time < B.time;
}

void add(int x){
    -- cnt2[cnt1[a[x]]];
    ++ cnt1[a[x]];
    ++ cnt2[cnt1[a[x]]];
}

void del(int x){
    -- cnt2[cnt1[a[x]]];
    -- cnt1[a[x]];
    ++ cnt2[cnt1[a[x]]];
}

void update(int i,int _time){
    if(Q[i].l <= cge[_time].pos && cge[_time].pos <= Q[i].r)
        del(cge[_time].pos);
    swap(a[cge[_time].pos],cge[_time].val);
    if(Q[i].l <= cge[_time].pos && cge[_time].pos <= Q[i].r)
        add(cge[_time].pos);
}

int main() {
//#ifndef ONLINE_JUDGE
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
//#endif
    int n = read(),m = read();
    int block = pow(n,2.0 / 3);
    for(int i = 1;i <= n;++ i){
        a[i] = read();
        Ls[++ en] = a[i];
        belong[i] = (i - 1) / block + 1;
    }
    int tmp = 0,pq = 0;
    for(int i = 1;i <= m;++ i){
        int op = read();
        if(op == 1){
            Q[++ pq].l = read(); Q[pq].r = read();
            Q[pq].id = pq;       Q[pq].time = tmp;
        }
        else {
            cge[++ tmp].pos = read();
            cge[tmp].val = read();
            Ls[++ en] = cge[tmp].val;
        }
    }
    sort(Ls + 1,Ls + 1 + en);
    en = unique(Ls + 1,Ls + 1 + en) - Ls - 1;
    for(int i = 1;i <= n;++ i)
        a[i] = lower_bound(Ls + 1,Ls + 1 + en,a[i]) - Ls;
    for(int i = 1;i <= tmp;++ i)
        cge[i].val = lower_bound(Ls + 1,Ls + 1 + en,cge[i].val) - Ls;
    
    sort(Q + 1,Q + 1 + pq,cmp);
    int L = 1,R = 0,_time = 0;
    for(int i = 1;i <= pq;++ i){
        while(L > Q[i].l) add(-- L);
        while(R < Q[i].r) add(++ R);
        while(L < Q[i].l) del(L ++);
        while(R > Q[i].r) del(R --);
        while(_time < Q[i].time) update(i,++ _time);
        while(_time > Q[i].time) update(i,_time --);
        ans[Q[i].id] = 1;
        while(cnt2[ans[Q[i].id]])
            ++ ans[Q[i].id];
    }
    for(int i = 1;i <= pq;++ i)
        printf("%d\n",ans[i]);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值