题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4737
2 3 6 1 3 5 2 4 5 4
Case #1: 4 Case #2: 0
题意:
求有多少对f(i, j) < m!
PS:
维护两个指针!
代码如下:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const int maxn = 100010; int a[maxn]; int c[32]; int cal(int x, int v) { int ret = 0; for(int i = 0; i <= 30; i++) { if(x&(1<<i)) { c[i] += v; } if(c[i]) { ret |= (1<<i); } } return ret; } int main () { int t; int n, m; int cas = 0; scanf("%d",&t); while(t--) { memset(c,0,sizeof(c)); scanf("%d%d", &n, &m); for(int i = 0; i < n; i++) { scanf("%d",&a[i]); } int l = 0; LL ans = 0; for(int i = 0; i < n; i++) { int tt = cal(a[i], 1); while(tt >= m) { tt = cal(a[l++], -1); } ans += (i-l+1); } printf("Case #%d: %I64d\n",++cas,ans); } return 0; }