hdu 4737 A Bit Fun 尺取法

A Bit Fun

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


Problem Description
There are n numbers in a array, as a0, a1 ... , an-1, and another number m. We define a function f(i, j) = ai|ai+1|ai+2| ... | aj . Where "|" is the bit-OR operation. (i <= j)
The problem is really simple: please count the number of different pairs of (i, j) where f(i, j) < m.
 
Input
The first line has a number T (T <= 50) , indicating the number of test cases.
For each test case, first line contains two numbers n and m.(1 <= n <= 100000, 1 <= m <= 230) Then n numbers come in the second line which is the array a, where 1 <= ai <= 230.
 
Output
For every case, you should output "Case #t: " at first, without quotes. The t is the case number starting from 1.
Then follows the answer.
 
Sample Input
2 3 6 1 3 5 2 4 5 4
 
Sample Output
Case #1: 4 Case #2: 0
 
Source

2013 ACM/ICPC Asia Regional Chengdu Online

#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define esp 0.00000000001
const int N=1e5+10,M=1e6+10,inf=1e9+10,mod=1000000007;
int a[N];
int flag[40];
void init()
{
    memset(flag,0,sizeof(flag));
}
void update(int x,int hh)
{
    int sum=0;
    while(x)
    {
        flag[sum++]+=x%2*hh;
        x>>=1;
    }
}
int getnum()
{
    int ans=0;
    for(int i=0;i<=35;i++)
    {
        if(flag[i])
        ans+=1<<i;
    }
    return ans;
}
int main()
{
    int x,y,z,i,t;
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d%d",&x,&y);
        for(i=1;i<=x;i++)
        scanf("%d",&a[i]);
        int st=1;
        int en=1;
        int cnt=1;
        ll ans=0;
        int qu=0;
        while(1)
        {
            while(getnum()<y&&en<=x)
            update(a[en++],1);
            if(getnum()<y)
            {
                ans+=(ll)(en-st)*(en-st+1)/2;
                break;
            }
            ans+=max(0,en-st-1);
            update(a[st++],-1);
        }
        printf("Case #%d: %I64d
",cas++,ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jhz033/p/5747475.html