int tot; structNode { Node* nxt[2]; static Node* newNode(); voidinsert(int v, int b){ int x = (v >> b) & 1; if (!nxt[x]) nxt[x] = Node::newNode(); if (b) nxt[x]->insert(v, b - 1); } intask(int v, int b){ if (b < 0) return0; int x = (v >> b) & 1; if (nxt[!x]) { return (1 << b) | nxt[!x]->ask(v, b - 1); } elseif (nxt[x]) { return nxt[x]->ask(v, b - 1); } else { return0; } } } nodes[N];
Node* Node::newNode(){ return nodes + (++tot); }
classSolution { public: intfindMaximumXOR(vector<int>& nums){ Node* root = nodes; int ans = 0; for (int num : nums) { ans = max(ans, root->ask(num, 30)); root->insert(num, 30); } for (int i = 0; i <= tot; i++) { nodes[i].nxt[0] = nodes[i].nxt[1] = nullptr; } tot = 0; return ans; } };