题意:求[a,b]区间内的数字中正序对的个数。

具体思路参考:

https://blog.csdn.net/weixin_43135318/article/details/88061396

https://www.cnblogs.com/asdfsag/p/11278519.html

在此基础上维护一下每个状态中大于每个数字的数字出现的次数即可。

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=;
ll bit[N],nbit,f[N][][],g[N][][][N],G[N][][],vis[N][][],ka,a,b,ka2;
void dfs(ll u,ll ze,ll lim) {
if(vis[u][ze][lim]==ka)return;
vis[u][ze][lim]=ka;
G[u][ze][lim]=;
for(ll i=; i<=; ++i)g[u][ze][lim][i]=;
if(u==) {f[u][ze][lim]=; return;}
f[u][ze][lim]=;
for(ll i=; i<=(lim?bit[u]:); ++i) {
ll v=u-,ze2=ze&&i==,lim2=lim&&i==bit[u];
dfs(v,ze2,lim2);
f[u][ze][lim]+=f[v][ze2][lim2];
for(ll j=; j<=; ++j)g[u][ze][lim][j]+=g[v][ze2][lim2][j];
G[u][ze][lim]+=G[v][ze2][lim2];
if(!ze2) {
for(ll j=; j<=i; ++j)g[u][ze][lim][j]+=f[v][ze2][lim2];
G[u][ze][lim]+=g[v][ze2][lim2][i+];
}
}
}
ll solve(ll x) {
++ka;
for(nbit=; x; x/=)bit[++nbit]=x%;
dfs(nbit,,);
return G[nbit][][];
}
int main() {
ll T;
for(scanf("%lld",&T); T--;) {
scanf("%lld%lld",&a,&b);
printf("Case %lld: %lld\n",++ka2,solve(b)-solve(a-));
}
}
05-11 17:53