dtoi4375 "BJOI2019" delete number

Meaning of the questions:

     For any number of columns, if they can be deleted after following several operations in a limited number of its delete empty columns, claimed that the series can delete empty. A number of puncturing operations is defined as follows:

  • Note of the current number of columns of length  k , the number of columns is equal to delete all the  k  number.

     A length of the existing  n the number of columns  a, there are  m times modify operation, the  i  after the revision to answer you: After  i  number of times after the modified column  a, at least a few numbers need to modify the space available delete?

     Each modification operation to modify or add a whole number of columns or the number of columns as a whole minus a single point.

answer:

     I omitted if a size of a few, the sequence length becomes ah [a] (h [a] is the value of the number of times a line). So we sense about this situation, we can imagine to have h [a] boxes piled up a top, then pour the left, one by one falling position. Of course, greater than the length of the sequence position of the box can not be taken into account.

     So what is the answer? The answer is not the number of positions the box. Because I can put the box on a pile of boxes moved to the position where there is no more box, that is, the number of mobile answer.

     So if there is only a single point of modification, we use a tree line maintenance just fine.

     However, several rows of plus or minus 1 overall how to do it. It does not matter, we just did not change as a digital, ask the interval shifted on it. Of course, after the addition of the digital to follow displacement.

     So we can use the tree line maintained!

#include<cstdio>
#include<algorithm>
#include<cstdlib>
using namespace std;
const int INF=300000;
int n,m,a[300002],h[900002],t,cnt=1,minn,ans;
typedef struct{
    int ls,rs,Min,sum,f;
}P;
P p[4000002];
void pushdown(int root){
    if (p[root].f)
    {
        if (p[root].ls)
        {
            p[p[root].ls].Min+=p[root].f;p[p[root].ls].f+=p[root].f;
        }
        if (p[root].rs)
        {
            p[p[root].rs].Min+=p[root].f;p[p[root].rs].f+=p[root].f;
        }
        p[root].f=0;
    }
}
void build(int root,int begin,int end){
    if (begin==end)
    {
        p[root].Min=0;p[root].sum=1;
        return;
    }
    int mid=begin+(end-begin)/2;
    p[root].ls=++cnt;p[root].rs=++cnt;
    build(p[root].ls,begin,mid);build(p[root].rs,mid+1,end);
    int ls=p[root].ls,rs=p[root].rs;
    if (p[ls].Min<p[rs].Min){p[root].Min=p[ls].Min;p[root].sum=p[ls].sum;}
    else if (p[ls].Min>p[rs].Min){p[root].Min=p[rs].Min;p[root].sum=p[rs].sum;}
    else if (p[ls].Min==p[rs].Min){p[root].Min=p[ls].Min;p[root].sum=p[ls].sum+p[rs].sum;}
}
void gengxin(int root,int begin,int end,int begin2,int end2,int x){
    if (begin>end2 || end<begin2)return;
    if (begin>=begin2 && end<=end2)
    {
        p[root].Min+=x;p[root].f+=x;
        return;
    }
    int mid=begin+(end-begin)/2;pushdown(root);
    gengxin(p[root].ls,begin,mid,begin2,end2,x);gengxin(p[root].rs,mid+1,end,begin2,end2,x);
    int ls=p[root].ls,rs=p[root].rs;
    if (p[ls].Min<p[rs].Min){p[root].Min=p[ls].Min;p[root].sum=p[ls].sum;}
    else if (p[ls].Min>p[rs].Min){p[root].Min=p[rs].Min;p[root].sum=p[rs].sum;}
    else if (p[ls].Min==p[rs].Min){p[root].Min=p[ls].Min;p[root].sum=p[ls].sum+p[rs].sum;}
}
void chaxun(int root,int begin,int end,int begin2,int end2){
    if (begin>end2 || end<begin2)return;
    if (begin>=begin2 && end<=end2)
    {
        ow (p [root] .Min < a)
        {
            minn = p [root] .min; ans = p [root] .sum;
        }
        else if (p[root].Min==minn)ans+=p[root].sum;
        return;
    }
    int mid=begin+(end-begin)/2;pushdown(root);
    chaxun(p[root].ls,begin,mid,begin2,end2);chaxun(p[root].rs,mid+1,end,begin2,end2);
}
int main ()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++){scanf("%d",&a[i]);h[a[i]+INF]++;}
    t=n;build(1,-INF,INF);
    for (int i=1;i<=n;i++)
    if (h[i+INF])gengxin(1,-INF,INF,i-h[i+INF]+1,i,1);
    for (int i=1;i<=m;i++)
    {
        int on, x;
        scanf("%d%d",&op,&x);
        if (op)
        {
            if (h [a [on] + INF] && a [on] <= t) Gengxin ( 1 , -INF, INF, a [on] -h [a [on] + INF] + 1 , a [on] -h [a [on] + INF] + 1 , - 1 );
            h [a [op] + INF] -; x = x + n t, h [x + INF] ++ ;
            ow (h [x + INF]) Gengxin ( 1 , -INF, INF, xh [x + INF] + 1 , xh [x + INF] + 1 , 1 );
            a [on] = x;
        }
        else
        {
            if (x==-1)
            {
                t++;
                if (h[t+INF])gengxin(1,-INF,INF,t-h[t+INF]+1,t,1);
            }
            else
            {
                if (h[t+INF])gengxin(1,-INF,INF,t-h[t+INF]+1,t,-1);
                t--;
            }
        }
        a = INF; ans = 0 ;
        chaxun(1,-INF,INF,t-n+1,t);
        if (minn)puts("0");else printf("%d\n",ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/1124828077ccj/p/12241880.html