classSolution { public: intmaxAscendingSum(vector<int>& nums){ int ans = 1; int last = 0; int current = 0; for (int i : nums) { if (i > last) { current += i; } else { current = i; } ans = max(ans, current); last = i; } return ans; } };
classSolution { structTrie { array<Trie*, 2> children; int count = 0; }; constint MAX_BIT = 16; public: intcountPairs(vector<int>& nums, int low, int high){ constint n = nums.size(); Trie *root = newTrie(); for (int i : nums) { auto current = root; for (int k = MAX_BIT; k >= 0; --k) { if (current->children[(i >> k) & 1] == nullptr) { current->children[(i >> k) & 1] = newTrie(); } current = current->children[(i >> k) & 1]; current->count += 1; } } auto countLessThan = [&](constint target) -> int { int ans = 0; for (int i : nums) { auto current = root; for (int k = MAX_BIT; k >= 0 && current; --k) { constint x = (i >> k) & 1; constint y = (target >> k) & 1; if (y == 1) { if (current->children[x]) { ans += current->children[x]->count; // take all xor = zero } current = current->children[1 - x]; } else { current = current->children[x]; } } } return ans; }; return (countLessThan(high + 1) - countLessThan(low)) / 2; } };