-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathKGSS.cs
216 lines (176 loc) · 8.07 KB
/
KGSS.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
using System;
using System.Collections.Generic;
using System.Text;
// https://www.spoj.com/problems/KGSS/ #divide-and-conquer #segment-tree
// Does element updates and second maximum (sum) subrange queries on an array.
public sealed class KGSS
{
private readonly ArrayBasedSegmentTree _segmentTree;
public KGSS(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 SecondMaximumQueryObject[] _treeArray;
public ArrayBasedSegmentTree(IReadOnlyList<int> sourceArray)
{
_sourceArray = sourceArray;
_treeArray = new SecondMaximumQueryObject[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 SecondMaximumQueryObject(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)
{
var result = Query(0, queryStartIndex, queryEndIndex);
return result.Maximum + (result.SecondMaximum ?? 0);
}
private SecondMaximumQueryObject 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 and second maximum.
public sealed class SecondMaximumQueryObject
{
private SecondMaximumQueryObject()
{ }
public SecondMaximumQueryObject(int index, int value)
{
SegmentStartIndex = SegmentEndIndex = index;
Maximum = value;
}
public void Reinitialize(int value)
=> Maximum = 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; }
public int Maximum { get; private set; }
public int? SecondMaximum { get; private set; }
public SecondMaximumQueryObject Combine(SecondMaximumQueryObject rightAdjacentObject)
=> new SecondMaximumQueryObject
{
SegmentStartIndex = SegmentStartIndex,
SegmentEndIndex = rightAdjacentObject.SegmentEndIndex,
Maximum = GetCombinedMaximum(this, rightAdjacentObject),
SecondMaximum = GetCombinedSecondMaximum(this, rightAdjacentObject)
};
public void Update(SecondMaximumQueryObject updatedLeftChild, SecondMaximumQueryObject updatedRightChild)
{
Maximum = GetCombinedMaximum(updatedLeftChild, updatedRightChild);
SecondMaximum = GetCombinedSecondMaximum(updatedLeftChild, updatedRightChild);
}
private static int GetCombinedMaximum(SecondMaximumQueryObject leftAdjacentObject, SecondMaximumQueryObject rightAdjacentObject)
=> Math.Max(leftAdjacentObject.Maximum, rightAdjacentObject.Maximum);
private static int GetCombinedSecondMaximum(SecondMaximumQueryObject leftAdjacentObject, SecondMaximumQueryObject rightAdjacentObject)
=> Math.Max(
Math.Min(leftAdjacentObject.Maximum, rightAdjacentObject.Maximum),
Math.Max(leftAdjacentObject.SecondMaximum ?? 0, rightAdjacentObject.SecondMaximum ?? 0));
// 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().Split(), int.Parse);
var solver = new KGSS(sourceArray);
var output = new StringBuilder();
int operationCount = int.Parse(Console.ReadLine());
for (int o = 0; o < operationCount; ++o)
{
string[] operation = Console.ReadLine().Split();
if (operation[0] == "Q")
{
output.Append(solver.Query(
queryStartIndex: int.Parse(operation[1]) - 1,
queryEndIndex: int.Parse(operation[2]) - 1));
output.AppendLine();
}
else
{
solver.Update(
updateIndex: int.Parse(operation[1]) - 1,
newValue: int.Parse(operation[2]));
}
}
Console.Write(output);
}
}