UPD:附上了链式前向星代码
题意很简短,就是维护可删的逆序对数量,注意到 , 的规模都在 及以内,我们考虑用多 级别算法或者单根号级别算法。
由于 的贡献与两个二元组 , 相关,因此我们考虑使用维护二维信息的常见算法,有离线算法和在线主席树,树套树等。
由于本题要支持带修,因此离线算法可能不太适用(事实上,离线的 cdp 分治和分块可以解决,但是我不会),于是很自然的想到树套树。先预处理逆序对数,这个很好做,不细讲了。考虑删除。我们将 序列抽象为 个二元组 ,则所有与一个二元组相关的贡献为满足 或 的点对数量。我们直接用树状数组套树状数组将每个 插入,然后删除时查询 和 ,并且将点 删去即可。
具体的,见下面代码。代码中使用了容斥原理将 转换为 的元素数量减去 的元素数量,以及 转换为 的元素数量减去 的元素数量。由于 , 相等的情况不可能存在,因此无需在意。
ans-=query(i,n)+query(n,a[i])-2*query(i,a[i]);
update(i,a[i],-1);由于树套树的空间复杂度是 ,因此直接开出来肯定不行。需要使用哈希表,并且要手写,否则会被卡常。
仍然会 TLE。注意到 query(i,n)与query(n,a[i]) 完全可以直接使用一维树状数组维护,因此此处无需二维树状数组。
然后应该就可以通过了。时间复杂度 。AC记录: https://www.luogu.com.cn/record/236750260
事实上,写链式前行星代替 vector 可以跑得更快,但是没有必要。
这种使用树状数组的方法听说叫二维树状数组。但是用手写哈希实现它,这种方法我好像没有在任何地方看过,可能算是自创的。
#include<bits/stdc++.h>
using namespace std;
int n,m,a[100010];
const int N=9234353;
vector<pair<pair<int,int>,int> > b[N];
map<int,int> mp;
int id;
void upd(int x,int y,int z)
{
id=(x*((long long)n)+y)%N;
for(int i=0;i<b[id].size();i++)
{
if(b[id][i].first.first==x&&b[id][i].first.second==y)
{
b[id][i].second+=z;
return;
}
}
b[id].push_back({{x,y},z});
return;
}
int que(int x,int y)
{
id=(x*((long long)n)+y)%N;
for(int i=0;i<b[id].size();i++)
{
if(b[id][i].first.first==x&&b[id][i].first.second==y)
{
return b[id][i].second;
}
}
return 0;
}
struct DS
{
//unordered_map<int,int> tr[100010];
void update(int x,int y,int date)
{
while(y<=n)
{
//tr[x][y]+=date;
upd(x,y,date);
y+=y&(-y);
}
}
void Update(int x,int y,int date)
{
while(x<=n)
{
update(x,y,date);
x+=x&(-x);
}
return;
}
int query(int x,int y)
{
int ans=0;
while(y)
{
//ans+=tr[x][y];
ans+=que(x,y);
y-=y&(-y);
}
return ans;
}
int Query(int x,int y)
{
int ans=0;
while(x)
{
ans+=query(x,y);
x-=x&(-x);
}
return ans;
}
};
DS tr;
struct tree
{
int tr[100010];
void update(int x,int date)
{
while(x<=n)
{
tr[x]+=date;
x+=x&(-x);
}
return;
}
int query(int x)
{
int ans=0;
while(x)
{
ans+=tr[x];
x-=x&(-x);
}
return ans;
}
};
tree tr1,tr2;
long long ans=0;
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
mp[a[i]]=i;
tr.Update(i,a[i],1);
tr1.update(a[i],1);
tr2.update(i,1);
ans+=i-tr1.query(a[i]);
}
int x,y;
for(int i=1;i<=m;i++)
{
cout<<ans<<endl;
cin>>y;
x=mp[y];
ans-=(tr1.query(y)+tr2.query(x)-2*tr.Query(x,y));
tr1.update(y,-1);
tr2.update(x,-1);
tr.Update(x,y,-1);
}
return 0;
}
/*
5 4
1 5 3 4 2
5
1
4
2
*/链式前向星实现,并且卡了一下常:
#include<bits/stdc++.h>
using namespace std;
int n,m,a[100010];
const int N=9234353;
int h[10000010],ne[10000010],e[10000010][3],idx;
unordered_map<int,int> mp;
int id;
void upd(int x,int y,int z)
{
id=(x*((long long)n)+y)%N;
for(int i=h[id];i!=-1;i=ne[i])
{
if(e[i][0]==x&&e[i][1]==y)
{
e[i][2]+=z;
return;
}
}
e[++idx][0]=x,e[idx][1]=y,e[idx][2]=z,ne[idx]=h[id],h[id]=idx;
return;
}
int que(int x,int y)
{
id=(x*((long long)n)+y)%N;
for(int i=h[id];i!=-1;i=ne[i])
{
if(e[i][0]==x&&e[i][1]==y)
{
return e[i][2];
}
}
return 0;
}
struct DS
{
void update(int x,int y,int date)
{
while(y<=n)
{
upd(x,y,date);
y+=y&(-y);
}
}
void Update(int x,int y,int date)
{
while(x<=n)
{
update(x,y,date);
x+=x&(-x);
}
return;
}
int query(int x,int y)
{
int ans=0;
while(y)
{
ans+=que(x,y);
y-=y&(-y);
}
return ans;
}
int Query(int x,int y)
{
int ans=0;
while(x)
{
ans+=query(x,y);
x-=x&(-x);
}
return ans;
}
};
DS tr;
struct tree
{
int tr[100010];
void update(int x,int date)
{
while(x<=n)
{
tr[x]+=date;
x+=x&(-x);
}
return;
}
int query(int x)
{
int ans=0;
while(x)
{
ans+=tr[x];
x-=x&(-x);
}
return ans;
}
};
tree tr1,tr2;
long long ans=0;
int main()
{
memset(h,-1,sizeof h);
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
mp[a[i]]=i;
tr.Update(i,a[i],1);
tr1.update(a[i],1);
tr2.update(i,1);
ans+=i-tr1.query(a[i]);
}
int x,y;
for(int i=1;i<=m;i++)
{
cout<<ans<<endl;
cin>>y;
x=mp[y];
ans-=(tr1.query(y)+tr2.query(x)-2*tr.Query(x,y));
tr1.update(y,-1);
tr2.update(x,-1);
tr.Update(x,y,-1);
}
return 0;
}
/*
5 4
1 5 3 4 2
5
1
4
2
*/