#include <bits/stdc++.h> using namespace std; #define int long long typedef long long ll; inline ll read() { ll s=0; bool f=0; char ch=‘ ‘; while(!isdigit(ch)) {f|=(ch==‘-‘); ch=getchar();} while(isdigit(ch)) {s=(s<<3)+(s<<1)+(ch^48); ch=getchar();} return (f)?(-s):(s); } #define R(x) x=read() inline void write(ll x) { if(x<0) {putchar(‘-‘); x=-x;} if(x<10) {putchar(x+‘0‘); return;} write(x/10); putchar((x%10)+‘0‘); } #define W(x) write(x),putchar(‘ ‘) #define Wl(x) write(x),putchar(‘\n‘) const ll N=300005; ll n,m; struct Node { ll id,num,val; }a[N]; inline bool cmpnum(Node a,Node b){return a.num<b.num;} inline bool cmpid(Node a,Node b){return a.id<b.id;} struct BIT { ll S[N<<1]; #define lowbit(x) ((x)&(-x)) inline void Ins(int x,int v) { while(x<=n) { S[x]+=v; x+=lowbit(x); } } inline int Que(int x) { ll ans=0; while(x>0) { ans+=S[x]; x-=lowbit(x); } return ans; } }T,BT; signed main() { freopen("sort.in","r",stdin); freopen("sort.out","w",stdout); ll i,cv=0,ans=0,now=0,oo; R(n); R(m); for(i=1;i<=n;i++) R(a[a[i].id=i].num); sort(a+1,a+n+1,cmpnum); a[1].val=++cv; for(i=2;i<=n;i++) { if(a[i].num==a[i-1].num) a[i].val=cv; else a[i].val=++cv; } sort(a+1,a+n+1,cmpid); for(i=n;i>=1;i--) { ans+=(oo=T.Que(a[i].val-1)); BT.Ins(a[i].val,oo); T.Ins(a[i].val,1); } Wl(ans); for(i=1;i<=m;i++) { ll pos; R(pos); if(now<a[pos].val) { ans-=(BT.Que(a[pos].val)-BT.Que(now)); now=a[pos].val; } Wl(ans); } return 0; } /* input 6 2 160 163 164 161 167 160 2 3 output 6 3 1 */
原文:https://www.cnblogs.com/gaojunonly1/p/11222279.html