B--qsgg and Subarray
题意:就是给你一个数组让你找连续的子数组,与运算为0;
与运算有0为0,全1为1
思路:题目数组的数据为1e9级别的,大概为2的30次方,所以我们对每个数的2进制的前30位与1进行与操作,从小到大枚举右端点的下标,用lst[j]=i数组记录第j位为0的最大坐标,连续子序列的左端点的最大值为lst的最小值,故只需加上左端点即找到了,多个连续子序列。
#include <bits/stdc++.h>
using namespace std;
#define Ysanqian ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
typedef pair<int,int>PII;
const int N=1e6+10,M=1010,inf=1e9;
int lst[N];
int a[N];
//从小到大枚举右端点的下标,记录 lst[i] 表示第 i 位为 0 的最右边的下标,
//即该位 从这开始向左都为 0。那么左端点的最大取值 为所有位 lst 的最小值。
void solve()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
int ans=0;
for(int i=1;i<=n;i++)
{
for(int j=0;j<31;j++)
{
if(!(a[i]>>j&1))
lst[j]=i;
}
int mn=inf;
for(int j=0;j<31;j++)
{
cout<<lst[j]<<' ';
mn=min(mn,lst[j]);
}
cout<<endl;
ans+=mn;
}
cout<<ans<<endl;
}
signed main()
{
Ysanqian;
int T;
T=1;
//cin>>T;
while(T--)solve();
}