HDU 4737 A Bit Fun (2013成都网络赛)

A Bit Fun

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 423    Accepted Submission(s): 270

Problem Description There are n numbers in a array, as a
0, a
1 … , a
n-1, and another number m. We define a function f(i, j) = a
i|a
i+1|a
i+2| … | a
j . 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 <= 2
30) Then n numbers come in the second line which is the array a, where 1 <= a
i <= 2
30.  

 

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  

 

Recommend liuyiding  

 用位数num维护l,r两个指针。

扫描一遍。

复杂度31*n

 

 1 /* ***********************************************
 2 Author        :kuangbin
 3 Created Time  :2013/9/14 星期六 11:59:04
 4 File Name     :2013成都网络赛\1010.cpp
 5 ************************************************ */
 6 
 7 #pragma comment(linker, "/STACK:1024000000,1024000000")
 8 #include <stdio.h>
 9 #include <string.h>
10 #include <iostream>
11 #include <algorithm>
12 #include <vector>
13 #include <queue>
14 #include <set>
15 #include <map>
16 #include <string>
17 #include <math.h>
18 #include <stdlib.h>
19 #include <time.h>
20 using namespace std;
21 const int MAXN = 100010;
22 int a[MAXN];
23 int num[40];
24 int bit[40];
25 
26 int main()
27 {
28     //freopen("in.txt","r",stdin);
29     //freopen("out.txt","w",stdout);
30     
31     int T;
32     int n,m;
33     bit[0] = 1;
34     for(int i = 1;i <= 31;i++)
35         bit[i] = 2*bit[i-1];
36     scanf("%d",&T);
37     int iCase = 0;
38     while(T--)
39     {
40         iCase++;
41         scanf("%d%d",&n,&m);
42         for(int i = 0;i < n;i++)
43             scanf("%d",&a[i]);
44         long long tot = (long long)n*(n+1)/2;
45         int last = 0;
46         int i = 0,j = 0;
47         memset(num,0,sizeof(num));
48         long long sum = 0;
49         while(j < n)
50         {
51             for(int k = 0;k <=31;k++)
52                 if(a[j] & bit[k])
53                     num[k]++;
54             int s = 0;
55             for(int k = 0;k <= 31;k++)
56                 if(num[k])
57                     s += bit[k];
58             if(s >= m)
59             {
60                 while(s >=m)
61                 {
62                     for(int k = 0;k <= 31;k++)
63                         if(a[i] & bit[k])
64                             num[k]--;
65                     s = 0;
66                     for(int k = 0;k <= 31;k++)
67                         if(num[k])
68                             s += bit[k];
69                     i++;
70                     //cout<<i<<endl;
71                 }
72                 sum += (long long)(n-j)*(i-last);
73                 last = i;
74             }
75             j++;
76         }
77         printf("Case #%d: ",iCase);
78         //cout<<tot<<" "<<sum<<endl;
79         cout<<tot-sum<<endl;
80     }
81     return 0;
82 }

 

 

 

 

 

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/p/3322002.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞