-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathRPLN.cs
204 lines (172 loc) · 6.76 KB
/
RPLN.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
using System;
using System.IO;
using System.Text;
// https://www.spoj.com/problems/RPLN/ #divide-and-conquer #segment-tree
// Finds the minimum score in a range.
public sealed class RPLN
{
private readonly ArrayBasedSegmentTree _segmentTree;
public RPLN(int[] scores)
{
_segmentTree = new ArrayBasedSegmentTree(scores);
}
public int Solve(int queryStartIndex, int queryEndIndex)
=> _segmentTree.Query(queryStartIndex, queryEndIndex);
}
// 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/
// This segment tree has some optimizations (compared to say, GSS3) to avoid TLE.
public sealed class ArrayBasedSegmentTree
{
private readonly int[] _sourceArray;
private readonly int[] _treeArray;
public ArrayBasedSegmentTree(int[] sourceArray)
{
_sourceArray = sourceArray;
_treeArray = new int[2 * MathHelper.FirstPowerOfTwoEqualOrGreater(_sourceArray.Length) - 1];
Build(0, 0, _sourceArray.Length - 1);
}
private void Build(int treeArrayIndex, int segmentStartIndex, int segmentEndIndex)
{
if (segmentStartIndex == segmentEndIndex)
{
_treeArray[treeArrayIndex] = _sourceArray[segmentStartIndex];
return;
}
int leftChildTreeArrayIndex = 2 * treeArrayIndex + 1;
int rightChildTreeArrayIndex = leftChildTreeArrayIndex + 1;
int leftChildSegmentEndIndex = (segmentStartIndex + segmentEndIndex) >> 1;
Build(leftChildTreeArrayIndex, segmentStartIndex, leftChildSegmentEndIndex);
Build(rightChildTreeArrayIndex, leftChildSegmentEndIndex + 1, segmentEndIndex);
_treeArray[treeArrayIndex] = Math.Min(_treeArray[leftChildTreeArrayIndex], _treeArray[rightChildTreeArrayIndex]);
}
public int Query(int queryStartIndex, int queryEndIndex)
=> Query(0, 0, _sourceArray.Length - 1, queryStartIndex, queryEndIndex);
private int Query(
int treeArrayIndex, int segmentStartIndex, int segmentEndIndex,
int queryStartIndex, int queryEndIndex)
{
if (queryStartIndex <= segmentStartIndex && queryEndIndex >= segmentEndIndex)
return _treeArray[treeArrayIndex];
int leftChildTreeArrayIndex = (treeArrayIndex << 1) + 1;
int rightChildTreeArrayIndex = leftChildTreeArrayIndex + 1;
int leftChildSegmentEndIndex = (segmentStartIndex + segmentEndIndex) >> 1;
if (queryStartIndex <= leftChildSegmentEndIndex && queryEndIndex > leftChildSegmentEndIndex)
return Math.Min(
Query(leftChildTreeArrayIndex, segmentStartIndex, leftChildSegmentEndIndex, queryStartIndex, queryEndIndex),
Query(rightChildTreeArrayIndex, leftChildSegmentEndIndex + 1, segmentEndIndex, queryStartIndex, queryEndIndex));
else if (queryStartIndex <= leftChildSegmentEndIndex)
return Query(leftChildTreeArrayIndex, segmentStartIndex, leftChildSegmentEndIndex, queryStartIndex, queryEndIndex);
else
return Query(rightChildTreeArrayIndex, leftChildSegmentEndIndex + 1, segmentEndIndex, queryStartIndex, queryEndIndex);
}
}
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()
{
var output = new StringBuilder();
int testCount = FastIO.ReadNonNegativeInt();
for (int t = 1; t <= testCount; ++t)
{
int scoreCount = FastIO.ReadNonNegativeInt();
int queryCount = FastIO.ReadNonNegativeInt();
int[] scores = new int[scoreCount];
for (int s = 0; s < scoreCount; ++s)
{
scores[s] = FastIO.ReadInt();
}
var solver = new RPLN(scores);
output.AppendLine($"Scenario #{t}:");
for (int q = 0; q < queryCount; ++q)
{
output.Append(solver.Solve(
queryStartIndex: FastIO.ReadNonNegativeInt() - 1,
queryEndIndex: FastIO.ReadNonNegativeInt() - 1));
output.AppendLine();
}
}
Console.Write(output);
}
}
// This is based in part on submissions from https://www.codechef.com/status/INTEST.
// It's assumed the input is well-formed, so if you try to read an integer when no
// integers remain in the input, there's undefined behavior (infinite loop).
public static class FastIO
{
private const byte _null = (byte)'\0';
private const byte _newLine = (byte)'\n';
private const byte _minusSign = (byte)'-';
private const byte _zero = (byte)'0';
private const int _inputBufferLimit = 8192;
private static readonly Stream _inputStream = Console.OpenStandardInput();
private static readonly byte[] _inputBuffer = new byte[_inputBufferLimit];
private static int _inputBufferSize = 0;
private static int _inputBufferIndex = 0;
private static byte ReadByte()
{
if (_inputBufferIndex == _inputBufferSize)
{
_inputBufferIndex = 0;
_inputBufferSize = _inputStream.Read(_inputBuffer, 0, _inputBufferLimit);
if (_inputBufferSize == 0)
return _null; // All input has been read.
}
return _inputBuffer[_inputBufferIndex++];
}
public static int ReadNonNegativeInt()
{
byte digit;
// Consume and discard whitespace characters (their ASCII codes are all < _minusSign).
do
{
digit = ReadByte();
}
while (digit < _minusSign);
// Build up the integer from its digits, until we run into whitespace or the null byte.
int result = digit - _zero;
while (true)
{
digit = ReadByte();
if (digit < _zero) break;
result = result * 10 + (digit - _zero);
}
return result;
}
public static int ReadInt()
{
// Consume and discard whitespace characters (their ASCII codes are all < _minusSign).
byte digit;
do
{
digit = ReadByte();
}
while (digit < _minusSign);
bool isNegative = digit == _minusSign;
if (isNegative)
{
digit = ReadByte();
}
// Build up the integer from its digits, until we run into whitespace or the null byte.
int result = isNegative ? -(digit - _zero) : (digit - _zero);
while (true)
{
digit = ReadByte();
if (digit < _zero) break;
result = result * 10 + (isNegative ? -(digit - _zero) : (digit - _zero));
}
return result;
}
}