|
1 | 1 | struct T { |
2 | | - long sum; |
3 | | - long maxSubarraySumPrefix; |
4 | | - long maxSubarraySumSuffix; |
5 | | - long maxSubarraySum; |
6 | | - T() = default; |
7 | | - T(int num) |
8 | | - : sum(num), |
9 | | - maxSubarraySumPrefix(num), |
10 | | - maxSubarraySumSuffix(num), |
11 | | - maxSubarraySum(num) {} |
12 | | - T(long sum, long prefix, long suffix, long maxSum) |
13 | | - : sum(sum), |
14 | | - maxSubarraySumPrefix(prefix), |
15 | | - maxSubarraySumSuffix(suffix), |
16 | | - maxSubarraySum(maxSum) {} |
| 2 | + long sum; |
| 3 | + long maxSubarraySumPrefix; |
| 4 | + long maxSubarraySumSuffix; |
| 5 | + long maxSubarraySum; |
| 6 | + T() = default; |
| 7 | + T(int num) |
| 8 | + : sum(num) |
| 9 | + , maxSubarraySumPrefix(num) |
| 10 | + , maxSubarraySumSuffix(num) |
| 11 | + , maxSubarraySum(num) {} |
| 12 | + T(long sum, long prefix, long suffix, long maxSum) |
| 13 | + : sum(sum) |
| 14 | + , maxSubarraySumPrefix(prefix) |
| 15 | + , maxSubarraySumSuffix(suffix) |
| 16 | + , maxSubarraySum(maxSum) {} |
17 | 17 | }; |
18 | 18 |
|
19 | 19 | class SegmentTree { |
20 | | - public: |
21 | | - SegmentTree(const vector<int>& nums) : n(nums.size()), tree(nums.size() * 4) { |
22 | | - build(nums, 0, 0, n - 1); |
23 | | - } |
| 20 | +public: |
| 21 | + SegmentTree(const vector<int>& nums) |
| 22 | + : n(nums.size()) |
| 23 | + , tree(nums.size() * 4) { |
| 24 | + build(nums, 0, 0, n - 1); |
| 25 | + } |
24 | 26 |
|
25 | | - // Updates nums[i] to val. |
26 | | - void update(int i, int val) { |
27 | | - update(0, 0, n - 1, i, val); |
28 | | - } |
| 27 | + // Updates nums[i] to val. |
| 28 | + void update(int i, int val) { |
| 29 | + update(0, 0, n - 1, i, val); |
| 30 | + } |
29 | 31 |
|
30 | | - long getMaxSubarraySum() const { |
31 | | - return tree[0].maxSubarraySum; |
32 | | - } |
| 32 | + long getMaxSubarraySum() const { |
| 33 | + return tree[0].maxSubarraySum; |
| 34 | + } |
33 | 35 |
|
34 | | - private: |
35 | | - const int n; // the size of the input array |
36 | | - vector<T> tree; // the segment tree |
| 36 | +private: |
| 37 | + const int n; // the size of the input array |
| 38 | + vector<T> tree; // the segment tree |
37 | 39 |
|
38 | | - void build(const vector<int>& nums, int treeIndex, int lo, int hi) { |
39 | | - if (lo == hi) { |
40 | | - tree[treeIndex] = T(nums[lo]); |
41 | | - return; |
| 40 | + void build(const vector<int>& nums, int treeIndex, int lo, int hi) { |
| 41 | + if (lo == hi) { |
| 42 | + tree[treeIndex] = T(nums[lo]); |
| 43 | + return; |
| 44 | + } |
| 45 | + const int mid = (lo + hi) / 2; |
| 46 | + build(nums, 2 * treeIndex + 1, lo, mid); |
| 47 | + build(nums, 2 * treeIndex + 2, mid + 1, hi); |
| 48 | + tree[treeIndex] = merge(tree[2 * treeIndex + 1], tree[2 * treeIndex + 2]); |
42 | 49 | } |
43 | | - const int mid = (lo + hi) / 2; |
44 | | - build(nums, 2 * treeIndex + 1, lo, mid); |
45 | | - build(nums, 2 * treeIndex + 2, mid + 1, hi); |
46 | | - tree[treeIndex] = merge(tree[2 * treeIndex + 1], tree[2 * treeIndex + 2]); |
47 | | - } |
48 | 50 |
|
49 | | - void update(int treeIndex, int lo, int hi, int i, int val) { |
50 | | - if (lo == hi) { |
51 | | - tree[treeIndex] = T(val); |
52 | | - return; |
| 51 | + void update(int treeIndex, int lo, int hi, int i, int val) { |
| 52 | + if (lo == hi) { |
| 53 | + tree[treeIndex] = T(val); |
| 54 | + return; |
| 55 | + } |
| 56 | + const int mid = (lo + hi) / 2; |
| 57 | + if (i <= mid) |
| 58 | + update(2 * treeIndex + 1, lo, mid, i, val); |
| 59 | + else |
| 60 | + update(2 * treeIndex + 2, mid + 1, hi, i, val); |
| 61 | + tree[treeIndex] = merge(tree[2 * treeIndex + 1], tree[2 * treeIndex + 2]); |
53 | 62 | } |
54 | | - const int mid = (lo + hi) / 2; |
55 | | - if (i <= mid) |
56 | | - update(2 * treeIndex + 1, lo, mid, i, val); |
57 | | - else |
58 | | - update(2 * treeIndex + 2, mid + 1, hi, i, val); |
59 | | - tree[treeIndex] = merge(tree[2 * treeIndex + 1], tree[2 * treeIndex + 2]); |
60 | | - } |
61 | 63 |
|
62 | | - T merge(const T& left, const T& right) const { |
63 | | - return T( |
64 | | - left.sum + right.sum, |
65 | | - max(left.maxSubarraySumPrefix, left.sum + right.maxSubarraySumPrefix), |
66 | | - max(right.maxSubarraySumSuffix, right.sum + left.maxSubarraySumSuffix), |
67 | | - max({left.maxSubarraySum, right.maxSubarraySum, |
68 | | - left.maxSubarraySumSuffix + right.maxSubarraySumPrefix})); |
69 | | - } |
| 64 | + T merge(const T& left, const T& right) const { |
| 65 | + return T( |
| 66 | + left.sum + right.sum, |
| 67 | + max(left.maxSubarraySumPrefix, left.sum + right.maxSubarraySumPrefix), |
| 68 | + max(right.maxSubarraySumSuffix, right.sum + left.maxSubarraySumSuffix), |
| 69 | + max({left.maxSubarraySum, right.maxSubarraySum, |
| 70 | + left.maxSubarraySumSuffix + right.maxSubarraySumPrefix})); |
| 71 | + } |
70 | 72 | }; |
71 | 73 |
|
72 | 74 | class Solution { |
73 | | - public: |
74 | | - long long maxSubarraySum(vector<int>& nums) { |
75 | | - const bool allPositives = |
76 | | - ranges::all_of(nums, [](int num) { return num >= 0; }); |
77 | | - const long sum = accumulate(nums.begin(), nums.end(), 0L); |
78 | | - if (allPositives) |
79 | | - return sum; |
80 | | - const int maxNum = ranges::max(nums); |
81 | | - if (maxNum < 0) |
82 | | - return maxNum; |
| 75 | +public: |
| 76 | + long long maxSubarraySum(vector<int>& nums) { |
| 77 | + const bool allPositives = ranges::all_of(nums, [](int num) { return num >= 0; }); |
| 78 | + const long sum = accumulate(nums.begin(), nums.end(), 0L); |
| 79 | + if (allPositives) |
| 80 | + return sum; |
| 81 | + const int maxNum = ranges::max(nums); |
| 82 | + if (maxNum < 0) |
| 83 | + return maxNum; |
83 | 84 |
|
84 | | - long ans = LONG_MIN; |
85 | | - unordered_map<int, vector<int>> numToIndices; |
86 | | - SegmentTree tree(nums); |
| 85 | + long ans = LONG_MIN; |
| 86 | + unordered_map<int, vector<int>> numToIndices; |
| 87 | + SegmentTree tree(nums); |
87 | 88 |
|
88 | | - for (int i = 0; i < nums.size(); ++i) |
89 | | - numToIndices[nums[i]].push_back(i); |
| 89 | + for (int i = 0; i < nums.size(); ++i) |
| 90 | + numToIndices[nums[i]].push_back(i); |
90 | 91 |
|
91 | | - for (const auto& [num, indices] : numToIndices) { |
92 | | - for (const int index : indices) |
93 | | - tree.update(index, 0); |
94 | | - ans = max(ans, tree.getMaxSubarraySum()); |
95 | | - for (const int index : indices) |
96 | | - tree.update(index, num); |
97 | | - } |
| 92 | + for (const auto& [num, indices] : numToIndices) { |
| 93 | + for (const int index : indices) |
| 94 | + tree.update(index, 0); |
| 95 | + ans = max(ans, tree.getMaxSubarraySum()); |
| 96 | + for (const int index : indices) |
| 97 | + tree.update(index, num); |
| 98 | + } |
98 | 99 |
|
99 | | - return ans; |
100 | | - } |
| 100 | + return ans; |
| 101 | + } |
101 | 102 | }; |
0 commit comments