Luo Gu P3157 [CQOI2011] dynamic reverse order

Subject to the effect:

Given \ (1 \) to the \ (n-\) is arranged in a sequentially deleted in a given sequence \ (m \) elements, each element is calculated before the reverse delete the entire sequence number

Basic routine: deletion variant edge bordered

So that we do not seek to satisfy \ (pos_i <pos_j, tim_i < tim_j, num_i> num_j \) the number of Well

Press \ (tim \) sorted and then merge \ (pos_i \) , Fenwick tree \ (num_i \)

But this question we need to run positive and negative two \ (cdq \) , because we need to separate statistics \ (pos_i <pos_j, num_i> num_j \) and \ (pos_i> pos_j, num_i < num_j \) contributions

But I compressed to a \ (cdq \) in the \ (emmmm \)

Little-noticed point is that we need to answer accumulated to make a \ (ret_i \) array, which \ (ret_i \) indicates \ (i \) time newly generated number reverse order, the last also need to output the prefix and

Non-stick code is not too short

#include<bits/stdc++.h>
using namespace std;
namespace red{
#define int long long
#define mid ((l+r)>>1)
#define lowbit(x) ((x)&(-x))
    inline int read()
    {
        int x=0;char ch,f=1;
        for(ch=getchar();(ch<'0'||ch>'9')&&ch!='-';ch=getchar());
        if(ch=='-') f=0,ch=getchar();
        while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
        return f?x:-x;
    }
    const int N=1e5+10;
    int n,m,idx,tot;
    int pos[N];
    int st[N];
    int ret[N];
    struct point
    {
        int x,id,tim;
        int val;
        inline bool operator < (const point &t) const
        {
            if(tim^t.tim) return tim<t.tim;
            return id<t.id;
        }
    }a[N<<2],t[N<<2];
    int tr[N<<1];
    inline void update(int x,int k)
    {
        for(int i=x;i<=n;i+=lowbit(i)) tr[i]+=k;
    }
    inline int query(int y)
    {
        int ret=0;
        for(int i=y;i;i-=lowbit(i))
            ret+=tr[i];
        return ret;
    }
    inline void cdq(int l,int r)
    {
        if(l==r) return;
        cdq(l,mid);
        cdq(mid+1,r);
        int tl=l,tr=mid+1,tot=l;
        while(tl<=mid&&tr<=r)
        {
            if(a[tl].id<=a[tr].id) update(a[tl].x,1),t[tot++]=a[tl++];
            else ret[a[tr].tim]+=query(n)-query(a[tr].x),t[tot++]=a[tr++];
        }
        while(tl<=mid) update(a[tl].x,1),t[tot++]=a[tl++];
        while(tr<=r) ret[a[tr].tim]+=query(n)-query(a[tr].x),t[tot++]=a[tr++];
        for(int i=l;i<=mid;++i) update(a[i].x,-1);
        tl=mid,tr=r;
        while(tl>=l&&tr>=mid+1)
        {
            if(a[tl].id>=a[tr].id) update(a[tl].x,1),--tl;
            else ret[a[tr].tim]+=query(a[tr].x-1),--tr;
        }
        while(tl>=l) update(a[tl].x,1),--tl;
        while(tr>=mid+1) ret[a[tr].tim]+=query(a[tr].x-1),--tr;
        for(int i=l;i<=mid;++i) update(a[i].x,-1);
        for(int i=l;i<=r;++i) a[i]=t[i];
    }
    inline void main()
    {
        n=read(),m=read();
        for(int x,i=1;i<=n;++i)
        {
            x=read();
            pos[x]=i;
            a[i].x=x;
            a[i].id=i;
            a[i].tim=1;
        }
        for(int x,tmp,i=1;i<=m;++i)
        {
            x=read();
            tmp=pos[x];
            a[tmp].tim=m-i+2;
        }
        sort(a+1,a+n+1);
        cdq(1,n);
        for(int i=1;i<=m+1;++i) ret[i]+=ret[i-1];
        for(int i=m+1;i>=2;--i) printf("%lld\n",ret[i]);
    }
}
signed main()
{
    red::main();
    return 0;
}

Guess you like

Origin www.cnblogs.com/knife-rose/p/12045586.html