-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathGSS3.cs
233 lines (191 loc) · 9.45 KB
/
GSS3.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using System;
using System.Collections.Generic;
using System.Text;
// https://www.spoj.com/problems/GSS3/ #divide-and-conquer #segment-tree
// Does element updates and maximum sum subrange queries on an array.
public sealed class GSS3
{
private readonly ArrayBasedSegmentTree _segmentTree;
public GSS3(IReadOnlyList<int> sourceArray)
{
_segmentTree = new ArrayBasedSegmentTree(sourceArray);
}
public int Query(int queryStartIndex, int queryEndIndex)
=> _segmentTree.Query(queryStartIndex, queryEndIndex);
public void Update(int updateIndex, int newValue)
=> _segmentTree.Update(updateIndex, newValue);
}
// Most guides online cover this approach, but here's one good one:
// https://kartikkukreja.wordpress.com/2014/11/09/a-simple-approach-to-segment-trees/
public sealed class ArrayBasedSegmentTree
{
private readonly IReadOnlyList<int> _sourceArray;
private readonly MaximumSumQueryObject[] _treeArray;
public ArrayBasedSegmentTree(IReadOnlyList<int> sourceArray)
{
_sourceArray = sourceArray;
_treeArray = new MaximumSumQueryObject[2 * MathHelper.FirstPowerOfTwoEqualOrGreater(_sourceArray.Count) - 1];
Build(0, 0, _sourceArray.Count - 1);
}
private void Build(int treeArrayIndex, int segmentStartIndex, int segmentEndIndex)
{
if (segmentStartIndex == segmentEndIndex)
{
_treeArray[treeArrayIndex] = new MaximumSumQueryObject(segmentStartIndex, _sourceArray[segmentStartIndex]);
return;
}
int leftChildTreeArrayIndex = 2 * treeArrayIndex + 1;
int rightChildTreeArrayIndex = leftChildTreeArrayIndex + 1;
int leftChildSegmentEndIndex = (segmentStartIndex + segmentEndIndex) / 2;
Build(leftChildTreeArrayIndex, segmentStartIndex, leftChildSegmentEndIndex);
Build(rightChildTreeArrayIndex, leftChildSegmentEndIndex + 1, segmentEndIndex);
_treeArray[treeArrayIndex] = _treeArray[leftChildTreeArrayIndex].Combine(_treeArray[rightChildTreeArrayIndex]);
}
public int Query(int queryStartIndex, int queryEndIndex)
=> Query(0, queryStartIndex, queryEndIndex).MaximumSum;
private MaximumSumQueryObject Query(int treeArrayIndex, int queryStartIndex, int queryEndIndex)
{
var queryObject = _treeArray[treeArrayIndex];
if (queryObject.IsTotallyOverlappedBy(queryStartIndex, queryEndIndex))
return queryObject;
bool leftHalfOverlaps = queryObject.DoesLeftHalfOverlapWith(queryStartIndex, queryEndIndex);
bool rightHalfOverlaps = queryObject.DoesRightHalfOverlapWith(queryStartIndex, queryEndIndex);
int leftChildTreeArrayIndex = 2 * treeArrayIndex + 1;
int rightChildTreeArrayIndex = leftChildTreeArrayIndex + 1;
if (leftHalfOverlaps && rightHalfOverlaps)
return Query(leftChildTreeArrayIndex, queryStartIndex, queryEndIndex)
.Combine(Query(rightChildTreeArrayIndex, queryStartIndex, queryEndIndex));
else if (leftHalfOverlaps)
return Query(leftChildTreeArrayIndex, queryStartIndex, queryEndIndex);
else
return Query(rightChildTreeArrayIndex, queryStartIndex, queryEndIndex);
}
public void Update(int updateIndex, int newValue)
=> Update(0, updateIndex, newValue);
private void Update(int treeArrayIndex, int updateIndex, int newValue)
{
var queryObject = _treeArray[treeArrayIndex];
if (queryObject.SegmentStartIndex == queryObject.SegmentEndIndex)
{
queryObject.Reinitialize(newValue);
return;
}
int leftChildTreeArrayIndex = 2 * treeArrayIndex + 1;
int rightChildTreeArrayIndex = leftChildTreeArrayIndex + 1;
if (queryObject.DoesLeftHalfOverlapWith(updateIndex, updateIndex))
{
Update(leftChildTreeArrayIndex, updateIndex, newValue);
}
else // Some overlap must exist, so it's over the right half.
{
Update(rightChildTreeArrayIndex, updateIndex, newValue);
}
queryObject.Update(_treeArray[leftChildTreeArrayIndex], _treeArray[rightChildTreeArrayIndex]);
}
}
// Given a query range, this stores the maximum sum for any contiguous subrange.
public sealed class MaximumSumQueryObject
{
private MaximumSumQueryObject()
{ }
public MaximumSumQueryObject(int index, int value)
{
SegmentStartIndex = SegmentEndIndex = index;
Sum = MaximumSum = MaximumLeftStartingSum = MaximumRightStartingSum = value;
}
public void Reinitialize(int value)
=> Sum = MaximumSum = MaximumLeftStartingSum = MaximumRightStartingSum = value;
// 'Readonly' property for the start index of the array range this query object corresponds to.
public int SegmentStartIndex { get; private set; }
// 'Readonly' property for the end index of the array range this query object corresponds to.
public int SegmentEndIndex { get; private set; }
private int Sum { get; set; }
public int MaximumSum { get; private set; }
private int MaximumLeftStartingSum { get; set; } // [-> ... ]
private int MaximumRightStartingSum { get; set; } // [ ... <-]
public MaximumSumQueryObject Combine(MaximumSumQueryObject rightAdjacentObject)
=> new MaximumSumQueryObject
{
SegmentStartIndex = SegmentStartIndex,
SegmentEndIndex = rightAdjacentObject.SegmentEndIndex,
Sum = GetCombinedSum(this, rightAdjacentObject),
MaximumSum = GetCombinedMaximumSum(this, rightAdjacentObject),
MaximumLeftStartingSum = GetCombinedMaximumLeftStartingSum(this, rightAdjacentObject),
MaximumRightStartingSum = GetCombinedMaximumRightStartingSum(this, rightAdjacentObject)
};
public void Update(MaximumSumQueryObject updatedLeftChild, MaximumSumQueryObject updatedRightChild)
{
Sum = GetCombinedSum(updatedLeftChild, updatedRightChild);
MaximumSum = GetCombinedMaximumSum(updatedLeftChild, updatedRightChild);
MaximumLeftStartingSum = GetCombinedMaximumLeftStartingSum(updatedLeftChild, updatedRightChild);
MaximumRightStartingSum = GetCombinedMaximumRightStartingSum(updatedLeftChild, updatedRightChild);
}
private static int GetCombinedSum(MaximumSumQueryObject leftAdjacentObject, MaximumSumQueryObject rightAdjacentObject)
// The sum is just the sum of both.
=> leftAdjacentObject.Sum + rightAdjacentObject.Sum;
private static int GetCombinedMaximumSum(MaximumSumQueryObject leftAdjacentObject, MaximumSumQueryObject rightAdjacentObject)
// The maximum sum either intersects both segments, or is entirely in one.
=> Math.Max(
leftAdjacentObject.MaximumRightStartingSum + rightAdjacentObject.MaximumLeftStartingSum,
Math.Max(leftAdjacentObject.MaximumSum, rightAdjacentObject.MaximumSum));
private static int GetCombinedMaximumLeftStartingSum(MaximumSumQueryObject leftAdjacentObject, MaximumSumQueryObject rightAdjacentObject)
// The maximum left starting sum starts at the left, and may or may not cross into the right.
=> Math.Max(
leftAdjacentObject.Sum + rightAdjacentObject.MaximumLeftStartingSum,
leftAdjacentObject.MaximumLeftStartingSum);
private static int GetCombinedMaximumRightStartingSum(MaximumSumQueryObject leftAdjacentObject, MaximumSumQueryObject rightAdjacentObject)
// The maximum right starting sum starts at the right, and may or may not cross into the left.
=> Math.Max(
rightAdjacentObject.Sum + leftAdjacentObject.MaximumRightStartingSum,
rightAdjacentObject.MaximumRightStartingSum);
// The given range starts before the segment starts and ends after the segment ends.
public bool IsTotallyOverlappedBy(int startIndex, int endIndex)
=> startIndex <= SegmentStartIndex && endIndex >= SegmentEndIndex;
// Assumed that some overlap exists, just not necessarily over the left half.
public bool DoesLeftHalfOverlapWith(int startIndex, int endIndex)
=> startIndex <= (SegmentStartIndex + SegmentEndIndex) / 2;
// Assumed that some overlap exists, just not necessarily over the right half.
public bool DoesRightHalfOverlapWith(int startIndex, int endIndex)
=> endIndex > (SegmentStartIndex + SegmentEndIndex) / 2;
}
public static class MathHelper
{
public static int FirstPowerOfTwoEqualOrGreater(int value)
{
int result = 1;
while (result < value)
{
result <<= 1;
}
return result;
}
}
public static class Program
{
private static void Main()
{
int arrayLength = int.Parse(Console.ReadLine());
int[] sourceArray = Array.ConvertAll(Console.ReadLine().Trim().Split(), int.Parse);
var solver = new GSS3(sourceArray);
var output = new StringBuilder();
int operationCount = int.Parse(Console.ReadLine());
for (int o = 0; o < operationCount; ++o)
{
int[] operation = Array.ConvertAll(Console.ReadLine().Trim().Split(), int.Parse);
if (operation[0] == 0)
{
solver.Update(
updateIndex: operation[1] - 1,
newValue: operation[2]);
}
else
{
output.Append(solver.Query(
queryStartIndex: operation[1] - 1,
queryEndIndex: operation[2] - 1));
output.AppendLine();
}
}
Console.Write(output);
}
}