Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int MAXN = 1e6 + 2;
- struct trie{
- int cnt = 0;
- int links[2];
- } b[30 * MAXN];
- int pr[MAXN];
- //vector <trie> b;
- int cntV = 0;
- void add(int x){
- int v = 0;
- b[v].cnt++;
- int bit;
- for (int i = 29; i >= 0; i--){
- bit = (x >> i) & 1;
- if (b[v].links[bit] != -1){
- v = b[v].links[bit];
- b[v].cnt++;
- }
- else{
- cntV++;
- b[v].links[bit] = cntV;
- v = cntV;
- b[v].cnt++;
- b[v].links[0] = -1;
- b[v].links[1] = -1;
- }
- }
- }
- int get_cnt(int x, int k){
- int ans = 0;
- int v = 0;
- int bitX, bitK;
- for (int i = 29; i >= 0; i--){
- bitX = (x >> i) & 1;
- bitK = (k >> i) & 1;
- if (bitK == 0 && bitX == 0){
- if (b[v].links[1] != -1){
- ans += b[b[v].links[1]].cnt;
- }
- if (b[v].links[0] != -1){
- v = b[v].links[0];
- }
- else{
- return ans;
- }
- }
- if (bitK == 0 && bitX == 1){
- if (b[v].links[0] != -1){
- ans += b[b[v].links[0]].cnt;
- }
- if (b[v].links[1] != -1){
- v = b[v].links[1];
- }
- else{
- return ans;
- }
- }
- if (bitK == 1){
- if (b[v].links[1 ^ bitX] != -1){
- v = b[v].links[1 ^ bitX];
- }
- else{
- return ans;
- }
- }
- }
- ans += b[v].cnt;
- return ans;
- }
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- b[0].links[0] = -1;
- b[0].links[1] = -1;
- int n, k;
- cin >> n >> k;
- int a[n + 1];
- for (int i = 1; i <= n; i++){
- cin >> a[i];
- }
- add(0);
- long long answer = 0;
- for (int i = 1; i <= n; i++){
- pr[i] = pr[i - 1] ^ a[i];
- answer += get_cnt(pr[i], k);
- add(pr[i]);
- }
- cout << answer;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment