题解
1.如果 \(x \oplus y \gt x\) ,则 \(y\) 的最高位对应的 \(x\) 一定是 \(0\)
2.$f(x,y)\oplus f(y,z) \gt f(x,z) $ 等价于 \(f(x,z) \oplus a_y \gt f(x,z)\)
3.\(x \in [1,y]\) 则 \(x-1 \in [0,y-1]\)
code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll a[100005]={0}, pre[100005]={0};
ll cntleft[100005][35][2]={0}, cntright[100005][35][2]={0};
inline void read(ll &x) {
x = 0;
ll flag = 1;
char c = getchar();
while(c < '0' || c > '9'){
if(c == '-')flag = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
x *= flag;
}
inline void write(ll x)
{
if(x < 0){
putchar('-');
x = -x;
}
if(x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
int main()
{
ll t;
read(t);
while(t--)
{
ll n;
read(n);
for(ll i=1; i<=n; i++)
{
read(a[i]);
pre[i]=pre[i-1]^a[i];
}
for(ll k=0; k<=30; k++) cntleft[0][k][0]=1;
for(ll i=1; i<=n; i++)
{
for(ll k=0; k<=30; k++)
{
ll add=((pre[i]>>k)&1);
cntleft[i][k][add]=cntleft[i-1][k][add]+1;
cntleft[i][k][1-add]=cntleft[i-1][k][1-add];
}
}
for(ll k=0; k<=30; k++) cntright[n+1][k][1]=cntright[n+1][k][0]=0;
for(ll i=n; i>=1; i--)
{
for(ll k=0; k<=30; k++)
{
ll add=((pre[i]>>k)&1);
cntright[i][k][add]=cntright[i+1][k][add]+1;
cntright[i][k][1-add]=cntright[i+1][k][1-add];
}
}
ll ans=0;
for(ll i=1; i<=n; i++)
{
ll height=log2(a[i]);
ans+=cntleft[i-1][height][0]*cntright[i][height][0];
ans+=cntleft[i-1][height][1]*cntright[i][height][1];
}
write(ans);
putchar('\n');
}
return 0;
}
标签:gt,cntright,cntleft,ll,100005,add,Inequality,BIT
From: https://www.cnblogs.com/pure4knowledge/p/18172696