duel 上分题。
题意
给一个长度为 的数列 ,包含 中的所有奇数,再给一个长度为 的数列 ,包含 中的所有整数,下标均从 开始。生成数列 ,求 的的逆序对数量模 。
分析
不同的贡献分开来算。
不妨设每 个分一段,段内 的值固定,逆序对数即为 的逆序对数,简单 BIT 优化计数。
块间考虑同一个 之间的贡献,即为 的逆序对,与上类似。
然后是 不同。容易发现的是 与 对数值的贡献完全不对等, 作为指数,相差过大时会直接决定数值大小关系。所以对于 差值大于 的逆序对直接统计即可,不用管 的贡献,不想动脑算式子直接 暴力。
最后考虑 的贡献差值小于 的情况,直接暴力枚举差值,扫描线的时候乘上差值带来的影响插进 BIT 里查贡献就可以了,时间复杂度 ,BIT 小常数配合 CF 神机还是很轻松跑过的。
代码
duel 时的代码所以有点凌乱。注意多测清空。
//From: ifffer_2137
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define inf 0x7fffffff
#define eb emplace_back
#define pii pair<int,int>
#define mkpr make_pair
#define fir first
#define sec second
inline int read(){
char ch=getchar();int x=0,w=1;
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+ch-48,ch=getchar();return w==1?x:-x;
}
const int maxn=2e5+5;
const int LG=20;
const int mod=998244353;
int T;
int n,m,ans;
int p[maxn],q[maxn];
class Fenwick_Tree{
public:
int c[maxn*2];
void clear(){for(int i=1;i<=max(m,n*2);i++) c[i]=0;}
int lowbit(int x){return x&(-x);}
void update(int x,int k){
while(x<=max(m,n*2)){
c[x]+=k;
x+=lowbit(x);
}
}
int query(int x){
int res=0;
while(x){
res+=c[x];
x-=lowbit(x);
}
return res;
}
}tr;
signed main(){
#ifndef ONLINE_JUDGE
freopen("data.in","r",stdin);
freopen("test.out","w",stdout);
#endif
cin.tie(0),cout.tie(0);
T=read();
while(T--){
n=read(),m=read(),ans=0;
for(int i=1;i<=n;i++) p[i]=read();
for(int i=1;i<=m;i++) q[i]=read();
tr.clear();
for(int i=m;i>=1;i--){
ans=(ans+tr.query(q[i]))%mod;
tr.update(q[i]+1,1);
}
tr.clear();
ans=ans*n%mod;
for(int i=n;i>=1;i--){
ans=(ans+tr.query(p[i]-1)*m%mod)%mod;
tr.update(p[i],1);
}
for(int t=1;t<min(m,LG);t++){
int res=0;
tr.clear();
for(int i=n;i>=1;i--){
res=(res+tr.query(p[i]-1))%mod;
if(p[i]*(1ll<<t)<=n*2) tr.update(p[i]*(1ll<<t),1);
}
tr.clear();
for(int i=n;i>=1;i--){
res=(res+tr.query(min(n*2,p[i]*(1ll<<t)-1)))%mod;
tr.update(p[i],1);
}
ans+=res*(m-t);
}
for(int i=0;i<m;i++){
int k=max(0ll,i-LG+1);
ans=(ans+(n*(n-1)/2)%mod*k%mod)%mod;
}
cout<<ans<<'\n';
tr.clear();
}
return 0;
}