- Choose the aggregate stored for each interval or prefix.
- Build or initialize the structure from the input.
- Apply updates and combine the affected nodes to answer each query.
Code notes
- 133 lines of C++ from the credited upstream file longest-balanced-subarray-i.cpp.
- The implementation visibly relies on sequence storage, hash lookup.
- 7 loop blocks detected.
Complexity
Count the build once, then multiply the logarithmic update or query path by the number of operations.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
123 45class Solution {6public:7 int longestBalanced(vector<int>& nums) {8 const int n = size(nums) + 1;9 SegmentTree st(n);10 int result = 0;11 unordered_map<int, int> lookup;12 for (int i = 1, curr = 0; i <= size(nums); ++i) {13 const auto& d = nums[i - 1] & 1 ? +1 : -1;14 if (lookup.count(nums[i - 1])) {15 st.update(lookup[nums[i - 1]], n - 1, -d);16 curr -= d;17 }18 curr += d;19 lookup[nums[i - 1]] = i;20 st.update(lookup[nums[i - 1]], n - 1, +d);21 const auto& l = i - st.binary_search(curr);22 if (l > result) {23 result = l;24 }25 }26 return result;27 }28 29private:30 class SegmentTree {31 public:32 explicit SegmentTree(int N)33 : base_(N > 1 ? 1 << (__lg(N - 1) + 1) : 1),34 lazy_(base_),35 min(N > 1 ? 1 << (__lg(N - 1) + 2) : 2),36 max(N > 1 ? 1 << (__lg(N - 1) + 2) : 2) {37 38 }39 40 void update(int L, int R, const int val) {41 L += base_;42 R += base_;43 int L0 = L, R0 = R;44 for (; L <= R; L >>= 1, R >>= 1) {45 if ((L & 1) == 1) {46 apply(L++, val);47 }48 if ((R & 1) == 0) {49 apply(R--, val);50 }51 }52 pull(L0);53 pull(R0);54 }55 56 int binary_search(int x) {57 int i = 1;58 while (!(i >= base_)) {59 if (lazy_[i]) {60 apply(i << 1, lazy_[i]);61 apply((i << 1) | 1, lazy_[i]);62 lazy_[i] = 0;63 }64 i <<= 1;65 if (!(min[i] <= x && x <= max[i])) {66 i |= 1;67 }68 }69 return i - base_;70 }71 72 vector<int> min, max;73 74 private:75 void apply(int x, const int val) {76 min[x] += val;77 max[x] += val;78 if (x < base_) {79 lazy_[x] += val;80 }81 }82 83 void pull(int x) {84 while (x > 1) {85 x >>= 1;86 min[x] = min[x << 1] < min[(x << 1) | 1] ? min[x << 1] : min[(x << 1) | 1];87 max[x] = max[x << 1] > max[(x << 1) | 1] ? max[x << 1] : max[(x << 1) | 1];88 if (lazy_[x]) {89 min[x] += lazy_[x];90 max[x] += lazy_[x];91 }92 }93 }94 95 void push(int x) {96 for (int h = __lg(x) - 1; h > 0; --h) {97 int y = x >> h;98 if (lazy_[y]) {99 apply(y << 1, lazy_[y]);100 apply((y << 1) | 1, lazy_[y]);101 lazy_[y] = 0;102 }103 }104 }105 106 int base_;107 vector<int> lazy_;108 };109};110 111112113114class Solution2 {115public:116 int longestBalanced(vector<int>& nums) {117 int result = 0;118 for (int left = 0; left < size(nums); ++left) {119 unordered_set<int> lookup;120 for (int right = left, curr = 0; right < size(nums); ++right) {121 if (!lookup.count(nums[right])) {122 lookup.emplace(nums[right]);123 curr += nums[right] & 1 ? +1 : -1;124 }125 if (curr == 0) {126 result = max(result, right - left + 1);127 }128 }129 }130 return result;131 }132};133