-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMULTQ3.cs
349 lines (301 loc) · 12.9 KB
/
MULTQ3.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
using System;
using System.IO;
// https://www.spoj.com/problems/MULTQ3/ #divide-and-conquer #lazy #segment-tree
// Does range increments and range queries for numbers divisible by 3.
public sealed class MULTQ3
{
private readonly int _arrayEndIndex;
private readonly MULTQ3LazySegmentTree _segmentTree;
public MULTQ3(int arrayLength)
{
_arrayEndIndex = arrayLength - 1;
_segmentTree = new MULTQ3LazySegmentTree(arrayLength);
}
public void Increment(int incrementStartIndex, int incrementEndIndex)
=> _segmentTree.Increment(0, 0, _arrayEndIndex, incrementStartIndex, incrementEndIndex);
public int Query(int queryStartIndex, int queryEndIndex)
=> _segmentTree.Query(0, 0, _arrayEndIndex, queryStartIndex, queryEndIndex).Remainder0Count;
}
public sealed class MULTQ3LazySegmentTree
{
private readonly IncrementQueryObject[] _treeArray;
public MULTQ3LazySegmentTree(int arrayLength)
{
_treeArray = new IncrementQueryObject[2 * MathHelper.FirstPowerOfTwoEqualOrGreater(arrayLength) - 1];
Build(0, 0, arrayLength - 1);
}
private void Build(int treeArrayIndex, int segmentStartIndex, int segmentEndIndex)
{
if (segmentStartIndex == segmentEndIndex)
{
_treeArray[treeArrayIndex] = new IncrementQueryObject { Remainder0Count = 1 };
return;
}
int leftChildTreeArrayIndex = (treeArrayIndex << 1) | 1;
int rightChildTreeArrayIndex = leftChildTreeArrayIndex + 1;
int leftChildSegmentEndIndex = (segmentStartIndex + segmentEndIndex) >> 1;
Build(leftChildTreeArrayIndex, segmentStartIndex, leftChildSegmentEndIndex);
Build(rightChildTreeArrayIndex, leftChildSegmentEndIndex + 1, segmentEndIndex);
_treeArray[treeArrayIndex] = _treeArray[leftChildTreeArrayIndex].Combine(_treeArray[rightChildTreeArrayIndex]);
}
// Instead of returning the children object directly, we have to add on the parent's range addition. The
// children query object knows the subset of the parent segment it intersects with, and everything in
// there needs the additions that were applied to the parent segment as a whole. It's kind of weird, any
// pending range additions specifically for the children object gets brought out and added to the sum when
// we do .Combine or .Sum, but recursively it makes sense: the children object has a sum but still needs
// to know about the parent's range additions.
public IncrementQueryObject Query(
int treeArrayIndex, int segmentStartIndex, int segmentEndIndex,
int queryStartIndex, int queryEndIndex)
{
var parentQueryObject = _treeArray[treeArrayIndex];
if (queryStartIndex <= segmentStartIndex && queryEndIndex >= segmentEndIndex)
return parentQueryObject;
int leftChildTreeArrayIndex = (treeArrayIndex << 1) | 1;
int rightChildTreeArrayIndex = leftChildTreeArrayIndex + 1;
int leftChildSegmentEndIndex = (segmentStartIndex + segmentEndIndex) >> 1;
IncrementQueryObject childrenQueryObject;
if (queryStartIndex <= leftChildSegmentEndIndex && queryEndIndex > leftChildSegmentEndIndex)
{
childrenQueryObject = Query(
leftChildTreeArrayIndex, segmentStartIndex, leftChildSegmentEndIndex,
queryStartIndex, queryEndIndex)
.Combine(Query(
rightChildTreeArrayIndex, leftChildSegmentEndIndex + 1, segmentEndIndex,
queryStartIndex, queryEndIndex));
}
else if (queryStartIndex <= leftChildSegmentEndIndex)
{
childrenQueryObject = Query(
leftChildTreeArrayIndex, segmentStartIndex, leftChildSegmentEndIndex,
queryStartIndex, queryEndIndex);
}
else
{
childrenQueryObject = Query(
rightChildTreeArrayIndex, leftChildSegmentEndIndex + 1, segmentEndIndex,
queryStartIndex, queryEndIndex);
}
var queryObject = new IncrementQueryObject
{
RangeIncrements = parentQueryObject.RangeIncrements
};
queryObject.SubsumeRemainders(childrenQueryObject);
return queryObject;
}
public void Increment(
int treeArrayIndex, int segmentStartIndex, int segmentEndIndex,
int incrementStartIndex, int incrementEndIndex)
{
if (incrementStartIndex <= segmentStartIndex && incrementEndIndex >= segmentEndIndex)
{
++_treeArray[treeArrayIndex].RangeIncrements;
return;
}
int leftChildTreeArrayIndex = (treeArrayIndex << 1) | 1;
int rightChildTreeArrayIndex = leftChildTreeArrayIndex + 1;
int leftChildSegmentEndIndex = (segmentStartIndex + segmentEndIndex) >> 1;
if (incrementStartIndex <= leftChildSegmentEndIndex)
{
Increment(
leftChildTreeArrayIndex, segmentStartIndex, leftChildSegmentEndIndex,
incrementStartIndex, incrementEndIndex);
}
if (incrementEndIndex > leftChildSegmentEndIndex)
{
Increment(
rightChildTreeArrayIndex, leftChildSegmentEndIndex + 1, segmentEndIndex,
incrementStartIndex, incrementEndIndex);
}
_treeArray[treeArrayIndex].Update(_treeArray[leftChildTreeArrayIndex], _treeArray[rightChildTreeArrayIndex]);
}
// The idea is to store how many numbers in a range have remainders of 0, 1, and 2 when divided
// by 3. Then we can calculate how range increments to that range affect the total count of
// numbers divisible by 3. For example, if the range increment is 2, any numbers in the range
// already having a remainder of 1 will have a remainder of 0 after applying the range increment.
// The tricky part is doing the calculates fast enough to get AC.
public struct IncrementQueryObject
{
public int Remainder0Count
{
get
{
switch (RangeIncrements % 3)
{
case 0: return _remainder0CountWithoutRangeIncrements;
case 1: return _remainder2CountWithoutRangeIncrements;
case 2: return _remainder1CountWithoutRangeIncrements;
default: return 0;
}
}
set
{
_remainder0CountWithoutRangeIncrements = value;
}
}
private int _remainder0CountWithoutRangeIncrements;
private int _remainder1CountWithoutRangeIncrements;
private int _remainder2CountWithoutRangeIncrements;
public int RangeIncrements;
public IncrementQueryObject Combine(IncrementQueryObject rightAdjacentObject)
{
var combinedQueryObject = new IncrementQueryObject();
combinedQueryObject.SubsumeRemainders(this);
combinedQueryObject.SubsumeRemainders(rightAdjacentObject);
return combinedQueryObject;
}
public void Update(IncrementQueryObject updatedLeftChild, IncrementQueryObject updatedRightChild)
{
// Zero these out first as we want the total of the children's updated contributions.
_remainder0CountWithoutRangeIncrements
= _remainder1CountWithoutRangeIncrements
= _remainder2CountWithoutRangeIncrements = 0;
SubsumeRemainders(updatedLeftChild);
SubsumeRemainders(updatedRightChild);
}
public void SubsumeRemainders(IncrementQueryObject child)
{
switch (child.RangeIncrements % 3)
{
case 0:
_remainder0CountWithoutRangeIncrements += child._remainder0CountWithoutRangeIncrements;
_remainder1CountWithoutRangeIncrements += child._remainder1CountWithoutRangeIncrements;
_remainder2CountWithoutRangeIncrements += child._remainder2CountWithoutRangeIncrements;
break;
case 1:
_remainder0CountWithoutRangeIncrements += child._remainder2CountWithoutRangeIncrements;
_remainder1CountWithoutRangeIncrements += child._remainder0CountWithoutRangeIncrements;
_remainder2CountWithoutRangeIncrements += child._remainder1CountWithoutRangeIncrements;
break;
case 2:
_remainder0CountWithoutRangeIncrements += child._remainder1CountWithoutRangeIncrements;
_remainder1CountWithoutRangeIncrements += child._remainder2CountWithoutRangeIncrements;
_remainder2CountWithoutRangeIncrements += child._remainder0CountWithoutRangeIncrements;
break;
}
}
}
}
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 = FastIO.ReadNonNegativeInt();
var solver = new MULTQ3(arrayLength);
int operationCount = FastIO.ReadNonNegativeInt();
for (int o = 0; o < operationCount; ++o)
{
int operation = FastIO.ReadNonNegativeInt();
if (operation == 0)
{
solver.Increment(
incrementStartIndex: FastIO.ReadNonNegativeInt(),
incrementEndIndex: FastIO.ReadNonNegativeInt());
}
else
{
FastIO.WriteNonNegativeInt(solver.Query(
queryStartIndex: FastIO.ReadNonNegativeInt(),
queryEndIndex: FastIO.ReadNonNegativeInt()));
FastIO.WriteLine();
}
}
FastIO.Flush();
}
}
// 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 const int _outputBufferLimit = 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 readonly Stream _outputStream = Console.OpenStandardOutput();
private static readonly byte[] _outputBuffer = new byte[_outputBufferLimit];
private static readonly byte[] _digitsBuffer = new byte[11];
private static int _outputBufferSize = 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 void WriteNonNegativeInt(int value)
{
int digitCount = 0;
do
{
int digit = value % 10;
_digitsBuffer[digitCount++] = (byte)(digit + _zero);
value /= 10;
} while (value > 0);
if (_outputBufferSize + digitCount > _outputBufferLimit)
{
_outputStream.Write(_outputBuffer, 0, _outputBufferSize);
_outputBufferSize = 0;
}
while (digitCount > 0)
{
_outputBuffer[_outputBufferSize++] = _digitsBuffer[--digitCount];
}
}
public static void WriteLine()
{
if (_outputBufferSize == _outputBufferLimit) // else _outputBufferSize < _outputBufferLimit.
{
_outputStream.Write(_outputBuffer, 0, _outputBufferSize);
_outputBufferSize = 0;
}
_outputBuffer[_outputBufferSize++] = _newLine;
}
public static void Flush()
{
_outputStream.Write(_outputBuffer, 0, _outputBufferSize);
_outputBufferSize = 0;
_outputStream.Flush();
}
}