-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSUBAXOR.cpp
More file actions
96 lines (90 loc) · 1.44 KB
/
SUBAXOR.cpp
File metadata and controls
96 lines (90 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include<iostream>
#include<vector>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxBITS = 20;
struct node{
int lc, rc;
node *left, *right;
node()
{
lc = rc = 0;
left = right = NULL;
}
};
inline node* insert(node* curr , int level, int N)
{
if(level == -1)
return curr;
int b = N & (1 << level);
if(b)
{
curr->rc++;
if(curr->right == NULL)
curr->right = new node;
curr->right = insert(curr->right,level-1 , N);
}
else
{
curr->lc++;
if(curr->left == NULL)
curr->left = new node;
curr->left = insert(curr->left,level-1,N);
}
return curr;
}
inline int query(node* curr, int level ,int N, int K)
{
if(level == -1 || curr == NULL)
return 0;
int bit_N = N & (1 << level);
int bit_K = K & (1 << level);
if(bit_K)
{
if(bit_N)
{
return curr->rc + query(curr->left,level-1,N,K);
}
else
{
return curr->lc + query(curr->right,level-1,N,K);
}
}
else
{
if(bit_N)
{
return query(curr->right,level-1,N,K);
}
else
{
return query(curr->left,level-1,N,K);
}
}
}
int main()
{
int t; scanf("%d",&t);
while(t--)
{
node *root = new node;
int n , k;
scanf("%d%d",&n,&k);
int p = 0;
int q = 0;
long long ans = 0;
root = insert(root, maxBITS,0);
for(int i = 0; i < n; i++)
{
int x; scanf("%d",&x);
q = p ^ x;
ans += query(root,maxBITS, q , k );
root = insert(root, maxBITS,q);
p = q;
}
printf("%lld\n",ans);
}
return 0;
}