zoukankan      html  css  js  c++  java
  • F. Machine Learning 带修端点莫队

    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:

    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, 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
    #include<cstdio>
    #include<cstring>
    #include<map>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    const int N=2e5+88;
    map<int,int>M;
    int vis[N],num[N],a[N],b[N],now[N],ans[N],unit,l,r,t;
    struct Query{
        int l,r,tim,id;
        bool operator < (const Query &A)const{
          return l/unit==A.l/unit?(r/unit==A.r/unit?tim<A.tim:r<A.r):l<A.l;
        }
    }Q[N];
    struct Change{
        int pos,x,y;
    }C[N];
    void modify(int x,int d){
        --vis[num[x]];num[x]+=d;++vis[num[x]];
    }
    void going(int T,int d){
        if(C[T].pos>=l&&C[T].pos<=r) modify(C[T].x,d),modify(C[T].y,-d);
        if(d==1) a[C[T].pos]=C[T].x;else a[C[T].pos]=C[T].y;
    }
    int calc(){
        for(int i=1;;++i) if(!vis[i]) return i;
    }
    int main(){
        int n,q,tot,op,cc=0,pp=0;
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;++i) scanf("%d",&a[i]),now[i]=b[i]=a[i];
        tot=n,unit=(int)pow(n,0.6666666);
        for(int i=1;i<=q;++i) {
            scanf("%d",&op);
            if(op==2){
            ++cc,scanf("%d%d",&C[cc].pos,&C[cc].x);
            C[cc].y=now[C[cc].pos],b[++tot]=now[C[cc].pos]=C[cc].x;
            }
            else {
            ++pp,scanf("%d%d",&Q[pp].l,&Q[pp].r);
            Q[pp].tim=cc,Q[pp].id=pp;
            }
        }
        sort(b+1,b+tot+1);
        tot=unique(b+1,b+tot+1)-b-1;
        for(int i=1;i<=tot;++i) M[b[i]]=i;
        for(int i=1;i<=n;++i) a[i]=M[a[i]];
        for(int i=1;i<=cc;++i) C[i].x=M[C[i].x],C[i].y=M[C[i].y];
        sort(Q+1,Q+pp+1);
        for(int i=1;i<=pp;++i) {
            while(t<Q[i].tim) going(t+1,1),++t;
            while(t>Q[i].tim) going(t,-1),--t;
            
            while(l<Q[i].l) modify(a[l],-1),++l;
            while(l>Q[i].l) modify(a[l-1],1),--l;
            while(r<Q[i].r) modify(a[r+1],1),++r;
            while(r>Q[i].r) modify(a[r],-1),--r;
            ans[Q[i].id]=calc();
            
        }
        for(int i=1;i<=pp;++i) printf("%d
    ",ans[i]);
    }
    2
    3
    2


    莫队学习博客


  • 相关阅读:
    程序界真正的高帅富团体:Valve
    How Unreal Engine 4 Will Change The Next Games You Play【纯搬运】
    互联网“百年老店”是彻头彻尾的扯淡!
    如何关闭VS10中的IntelliSense
    发人深省周鸿祎:少功利多学习 做力所能及的事情
    FlashCS4 快捷键大全
    《1万小时成功定律——解构成功》
    通过AutoExpand调试Unreal内置数据类型
    14 Ways to Motivate Yourself
    关于C++ 动态定义数组
  • 原文地址:https://www.cnblogs.com/mfys/p/8519628.html
Copyright © 2011-2022 走看看