P1393
上篇博客说的心心念念的CDQ分治版本来辣
我们已经知道逆序数等于原来逆序数减去每次删掉一个数的贡献
那么既然一个数的贡献等于 下标在我左边的比我大的 -> cdq分治搞之
下标在我右边的比我小的 -> cdq分治搞之
不修改的数他修改时间就是 n+1
修改的数他修改时间是第几次修改
我们分治 修改时间 归并下标 树状数组维护值
就可以AC sort注意是左闭右开 谢谢Dillonh告诉我排序这个问题
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
//#pragma comment(linker, "/STACK:10240000,10240000")
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
typedef unsigned long long ull;
const ull hash1 = 201326611;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){ll ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
//ull ha[MAX_N],pp[MAX_N];
inline int read()
{
int date = 0,m = 1; char ch = 0;
while(ch!='-'&&(ch<'0'|ch>'9'))ch = getchar();
if(ch=='-'){m = -1; ch = getchar();}
while(ch>='0' && ch<='9')
{
date = date*10+ch-'0';
ch = getchar();
}return date*m;
}
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
/*int root[MAX_N],cnt,sz;
namespace hjt
{
#define mid ((l+r)>>1)
struct node{int l,r,maxx;}T[MAX_N*40];
#undef mid
}*/
const int MAX_N = 40025;
int n,m;
int C[MAX_N<<1],arr[MAX_N],b[MAX_N],Q[MAX_N],ans[MAX_N];
void add(int x,int v)
{
for(;x<MAX_N;x+=x&(-x))
C[x]+=v;
}
int get(int x)
{
int res = 0;
for(;x;x-=x&(-x))
res+=C[x];
return res;
}
struct query{
int a,time,id,s;
bool operator < (const query other) const
{
return time < other.time;
}
}T[MAX_N];
bool cmp(query a,query b)
{
return a.id < b.id;
}
void cdq(int l,int r)
{
if(l==r) return;
int mid = (l+r)>>1;
cdq(l,mid);cdq(mid+1,r);
sort(T+l,T+mid+1,cmp);sort(T+mid+1,T+r+1,cmp);
int j = mid;
for(int i = l;i<=mid;++i)
{
while(j<r&&T[j+1].id<T[i].id) j++,add(T[j].a,1);
T[i].s += get(n) - get(T[i].a);
}
for(int i = mid+1;i<=j;++i)
add(T[i].a,-1);
j = r + 1;
for(int i = mid;i>=l;--i)
{
while(j>mid+1&&T[j-1].id>T[i].id) j--,add(T[j].a,1);
T[i].s += get(T[i].a-1);
}
for(int i = r;i>=j;--i)
add(T[i].a,-1);
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
n = read();m = read();
int ANS = 0;
for(int i = 1;i<=n;++i)
{
arr[i] = read();b[i] = arr[i];
}
sort(b+1,b+1+n);
for(int i = 1;i<=n;++i)
{
arr[i] = lower_bound(b+1,b+1+n,arr[i])-b;
ANS += i - 1 - get(arr[i]);
add(arr[i],1);
T[i].a = arr[i];T[i].time = n+1;T[i].id = i;
}
for(int i = 1;i<=m;++i)
{
Q[i] = read();
T[Q[i]].time = i;
}
sort(T+1,T+1+n);
memset(C,0,sizeof(C));
cdq(1,n);
for(int i = 1;i<=n;++i) ans[T[i].id]=T[i].s;
printf("%d ",ANS);
for(int i = 1;i<=m;++i) i==n?printf("%d\n",ANS = ANS-ans[Q[i]]):printf("%d ",ANS = ANS-ans[Q[i]]);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}