TPOI-2B
官方题解。
考虑拆位,对于每一个二进制位,每次操作相当于若区间内全部为
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+2;
int a[N];
int st[N][40];
int main(){
int t;cin>>t;
while(t--){
int n;cin>>n;
for(int i=1; i<=n; i++) cin>>a[i];
for(int b=0; b<31; b++)
for(int i=1,l=0; i<=n; i++)
if(a[i]&(1ll<<b)){
if(!l) l=i;
if(i==n) st[l][b]=i;
}
else if(a[i-1]&(1ll<<b)) st[l][b]=i-1,l=0;
int ans=0;
for(int i=1; i<=n; i++){
int mn=1e9;
for(int b=0; b<31; b++) if(st[i][b]) mn=min(mn,st[i][b]);
if(mn==1e9) continue;
for(int b=0; b<31; b++){
if(st[i][b] && st[i][b]!=mn) st[mn+1][b]=st[i][b];
st[i][b]=0;
}
ans++;
}
cout<<ans<<"\n";
}
return 0;
}