Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * author: compounding
- * created: 2024-11-13 01:43:23
- **/
- #include <bits/stdc++.h>
- using namespace std;
- mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
- #define NeedForSpeed \
- ios_base::sync_with_stdio(false); \
- cin.tie(NULL); \
- cout.tie(NULL);
- #define int long long
- #define all(x) (x).begin(), (x).end()
- typedef vector<int> vi;
- typedef vector<bool> vb;
- typedef vector<vi> vvi;
- typedef vector<pair<int, int>> vpi;
- #define f first
- #define s second
- #define yes cout << "YES" << endl
- #define no cout << "NO" << endl
- #define endl "\n"
- const int mod = 1000000007;
- int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
- struct trie
- {
- int leftcnt, rightcnt; // count of left and right children which have 0, 1 in the bit
- trie *left, *right; // reference to left and right child
- trie()
- {
- leftcnt = 0;
- rightcnt = 0;
- left = NULL;
- right = NULL;
- }
- };
- void insert(trie *root, int value)
- {
- for (int i = 31; i >= 0; i--)
- {
- int biton = (value >> i) & 1; // get the ith bit
- // if ON
- if (biton)
- {
- root->rightcnt++;
- if (root->right == NULL)
- {
- // go to it's right child
- root->right = new trie();
- }
- root = root->right;
- }
- else
- {
- root->leftcnt++;
- if (root->left == NULL)
- {
- // go to it's left child
- root->left = new trie();
- }
- root = root->left;
- }
- }
- }
- int query(trie *root, int value, int k)
- {
- int ans = 0;
- if (root == NULL)
- {
- return ans;
- }
- for (int i = 31; i >= 0; i--)
- {
- int biton = (value >> i) & 1; // get the ith bit
- int bitk = (k >> i) & 1; // get the ith bit
- // TODO: if ith bit of k is ON
- if (bitk)
- {
- // TODO : if ith bit of value is ON
- if (biton)
- {
- // add the right cnt, as we have to make the XOR < k so we have to switch this bit off
- ans += root->rightcnt;
- if (root->left == NULL)
- {
- return ans;
- }
- root = root->left;
- }
- else
- {
- ans += root->leftcnt;
- if (root->right == NULL)
- {
- return ans;
- }
- root = root->right;
- }
- }
- // TODO: if ith bit of k is OFF
- else
- {
- // if ith bit of value is ON
- if (biton)
- {
- if (root->right == NULL)
- {
- return ans;
- }
- root = root->right;
- }
- else
- {
- if (root->left == NULL)
- {
- return ans;
- }
- root = root->left;
- }
- }
- }
- return ans;
- }
- void solve()
- {
- int n, k;
- cin >> n >> k;
- // number of subarrays with XOR less than k
- vi a(n);
- int prexor = 0, cnt = 0;
- trie *root = new trie();
- insert(root, 0);
- for (int i = 0; i < n; i++)
- {
- cin >> a[i];
- prexor ^= a[i];
- cnt += query(root, prexor, k);
- insert(root, prexor);
- }
- cout << cnt << endl;
- return;
- }
- signed main()
- {
- NeedForSpeed;
- int t = 1;
- cin >> t;
- while (t--)
- {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment