F. Machine Learning(Codeforces Round #466 (Div. 2)(带修改莫队)

F. Machine Learning
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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:

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}
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, li, ri, where 1 ≤ li ≤ ri ≤ n — the bounds of the subarray.

The second type of query is described by three integers ti = 2, pi, xi, 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
Copy

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次,带修改莫队只适合单点更新,修改的更新代码上有模板,差不多就是这个套路,用过自己写的 莫名wa 原因待补,注意修改莫队块的大小为N^(2/3) 复杂度为O(N^{5/3)
ac code

#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1e5+7;
int cnt[maxn*2];
int a[maxn];
int n;
int q;
int pos[maxn];
int block = 2000;
int cntcnt[maxn];
int b[maxn*2];
int ans[maxn];
struct node
{
    int l,r;
    int md;
    int id;
    int ans;
    bool operator <(const node&p)const{
        if(pos[this->l]==pos[p.l]){
            if(pos[this->r]==pos[p.r]){
                return this->md<p.md;
            }
            return this->r<p.r;
        }
        return pos[this->l]<pos[p.l];
    }
}Q[maxn];
struct node2
{
    int p;
    int to;
    int pre;
}modity[maxn];
void add(int va)
{
    cntcnt[cnt[va]]--;
    cnt[va]++;
    cntcnt[cnt[va]]++;
}
void del(int va)
{
     cntcnt[cnt[va]]--;
     cnt[va]--;
     cntcnt[cnt[va]]++;
}
int L,R;
void mf(node2 &the)
{
    if(the.p>=L&&the.p<=R){
        del(the.pre);
        add(the.to);
    }
    a[the.p] = the.to;
}
void mb(node2 &the)
{
    if(the.p>=L&&the.p<=R){
        del(the.to);
        add(the.pre);
    }
    a[the.p] = the.pre;
}
void getpos()
{
    for(int i = 1;i<=n;i++){
        pos[i] = i/block;
    }
}
int main()
{
    cntcnt[0] = 1e8;
    int p = 0;
    scanf("%d%d",&n,&q);
    for(int i = 1;i<=n;i++){
        scanf("%lld",&a[i]);
        b[p++] = a[i];
    }
    block = max((int)pow(n,2*1.0/3),1);
    getpos();
    int now = 0;
    int q1 = 0;
    int q2 = 1;
    for(int i = 0;i<q;i++){
        int op;
        scanf("%d",&op);
        if(op==1){
            scanf("%d%d",&Q[q1].l,&Q[q1].r);
            Q[q1].ans = 1;
            Q[q1].md = now;
            Q[q1].id = q1;
            q1++;
        }
        else{
            int pp;
            int va;
            scanf("%d%d",&pp,&va);
            now++;
            modity[q2].p = pp;
            modity[q2].pre = a[pp];
            modity[q2++].to = va;
            a[pp] = va;
            b[p++] = va;
        }
    }
    sort(b,b+p);
    sort(Q,Q+q1);
    int sz = unique(b,b+p)-b;
    for(int i = 1;i<=n;i++){
        a[i] = lower_bound(b,b+sz,a[i])-b;
    }
    for(int i = 1;i<=q2;i++){
        modity[i].pre = lower_bound(b,b+sz,modity[i].pre)-b;
        modity[i].to = lower_bound(b,b+sz,modity[i].to)-b;
    }
    int curt = now;
R = 0;
L = 1;
    for(int i = 0;i<q1;i++){
        while(R<Q[i].r) add(a[++R]);
        while(L<Q[i].l) del(a[L++]);
        while(R>Q[i].r) del(a[R--]);
        while(L>Q[i].l) add(a[--L]);z  
        while(curt<Q[i].md) mf(modity[++curt]);
        while(curt>Q[i].md) mb(modity[curt--]);
        while(cntcnt[Q[i].ans]>0) Q[i].ans++;
        ans[Q[i].id] = Q[i].ans;
    }
    for(int i = 0;i<q1;i++){
        printf("%d\n",ans[i]);
    }
 //   cout<<(int)sqrt(100000*1.0)<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值