1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| #include<bits/stdc++.h> using namespace std; #define ll long long const int maxn=5e5+10; const int inf=0x7fffffff; #define in read() inline int read(){ int x=0,f=1;char c=getchar(); while(c<'0'||c>'9'){ if(c=='-') f=-1; c=getchar(); } while(c>='0'&&c<='9'){ x=(x<<1)+(x<<3)+c-'0'; c=getchar(); } return x*f; } int n,m,h[maxn],hsh[maxn],tot=0,x; ll val[maxn]; struct node{ int pos,val; }a[maxn]; struct BIT{ ll t[maxn]; inline int lowbit(int x){ return x&-x; } inline ll query(int x){ ll res=0; while(x){ res+=t[x]; x-=lowbit(x); } return res; } inline void update(int x,ll val){ while(x<=tot){ t[x]+=val; x+=lowbit(x); } } }bit; bool cmp(node a,node b){ return a.val<b.val; }
struct tree{ int l,r,pos; }t[maxn<<2]; void pushup(int p){ if(hsh[t[p<<1|1].pos]<=hsh[t[p<<1].pos]) t[p].pos=t[p<<1|1].pos; else t[p].pos=t[p<<1].pos; } void build(int l,int r,int p){ t[p].l=l;t[p].r=r; if(l==r){ t[p].pos=l; return ; } int mid=l+r>>1; build(l,mid,p<<1); build(mid+1,r,p<<1|1); pushup(p); } void update(int l,int r,int p,int t){ if(l==r){ hsh[l]=inf; val[l]=0; return ; } int mid=l+r>>1; if(t<=mid) update(l,mid,p<<1,t); else update(mid+1,r,p<<1|1,t); pushup(p); } int pd(int x,int y){ if(hsh[y]<=hsh[x]) return y; return x; } int query(int l,int r,int p){ if(l<=t[p].l&&t[p].r<=r) return t[p].pos; int ans=0; int mid=t[p].l+t[p].r>>1; if(l<=mid) ans=query(l,r,p<<1); if(r>mid){ int t=query(l,r,p<<1|1); if(!ans) ans=t; else ans=pd(ans,t); } return ans; } int main(){ n=in;m=in; for(int i=1;i<=n;i++){ a[i].pos=i;a[i].val=in; } sort(a+1,a+1+n,cmp); for(int i=1;i<=n;i++){ if(a[i].val!=a[i-1].val) hsh[a[i].pos]=++tot; else hsh[a[i].pos]=tot; } ll ans=0; for(int i=n;i>=1;i--){ bit.update(hsh[i],1); val[i]=bit.query(hsh[i]-1); ans+=val[i]; } printf("%lld\n",ans); build(1,n,1); for(int i=1;i<=m;i++){ x=in; while(hsh[x]!=inf){ int t=query(x,n,1); ans-=val[t]; update(1,n,1,t); } printf("%lld\n",ans); } return 0; }
|