对于i,j,如果有 (t_i<t_j,v_i<v_j,p_i>p_j)||(t_i<t_j,v_i>v_j,p_i<p_j)那么就记录贡献就好了

// luogu-judger-enable-o2
#include <bits/stdc++.h>
#define il inline
#define Max 500005
#define ll long long
#define lowbit(x) x&-x
using namespace std;
il int read()
{
    char c=getchar();
    int x=0,f=1;
    while(c>'9'||c<'0')
    {
        if(c=='-') f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
    return x*f;
}
struct nd
{
    int m,v,d,id,t;
    nd(int a=0,int b=0,int x=0,int y=0,int z=0)
    {
        m=a,v=b,d=x,id=y,t=z;
    }
}e[Max];
int n,m,q[Max],tot,t[Max],a[Max],p[Max];
ll ans[Max];
namespace lyzqs
{
    il void ins(int x,int y)
    {
        for(int i=x;i<=n;i+=lowbit(i))
            t[i]+=y;
    }
    il int qry(int x)
    {
        int res=0;
        for(int i=x;i;i-=lowbit(i))
            res+=t[i];
        return res;
    }
    il bool cmp(nd x,nd y)
    {
        return x.d<y.d;
    }
    il void cdq(int l,int r)
    {
        if(l>=r) return;
        //cout<<l<<' '<<r<<endl;
        int mid=(l+r)>>1;
        cdq(l,mid),cdq(mid+1,r);
        sort(e+l,e+mid+1,cmp);
        sort(e+mid+1,e+r+1,cmp);
        int j=l;
        for(int i=mid+1;i<=r;i++)
        {
            while(j<=mid&&e[j].d<=e[i].d) ins(e[j].v,e[j].m),j++;
            ans[e[i].id]+=e[i].m*(qry(n)-qry(e[i].v));
        }
        for(int i=l;i<j;i++) ins(e[i].v,-e[i].m);
        j=mid;
        for(int i=r;i>mid;i--)
        {
            while(j>=l&&e[j].d>=e[i].d) ins(e[j].v,e[j].m),j--;
            ans[e[i].id]+=e[i].m*qry(e[i].v-1);
        }
        for(int i=mid;i>j;i--) ins(e[i].v,-e[i].m);
    }
    il void main()
    {
        n=read();m=read();
        for(int i=1;i<=n;i++)
        {
            a[i]=read();
            p[a[i]]=i;
            e[++tot]=nd(1,a[i],i,0,tot);
        }
        for(int i=1;i<=m;i++)
        {
            int x=read();
            e[++tot]=nd(-1,x,p[x],i,tot);
        }
        cdq(1,tot);
        //ans[0]=5;
        for(int i=1;i<=m;i++) ans[i]+=ans[i-1];
        for(int i=0;i<m;i++) printf("%lld\n",ans[i]);
    }
}
int main()
{
    lyzqs::main();
}