This commit is contained in:
2026-07-06 13:48:26 +02:00
parent 241dc2b987
commit 2ada187ae0
11 changed files with 2216 additions and 551 deletions
+89
View File
@@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace AnotherReplayReader.Utils
{
public record TimeIndexedPrefixSums(List<TimeSpan> Times, List<int> PrefixSums)
{
public void Add(TimeSpan time, int value)
{
if (Times.Count > 0 && time < Times.Last())
{
throw new ArgumentException("Time must be added in non-decreasing order.");
}
Times.Add(time);
PrefixSums.Add((PrefixSums.LastOrDefault()) + value);
}
public int Query(TimeSpan start, TimeSpan end)
{
var times = Times;
var prefix = PrefixSums;
int startIndex = LowerBound(times, start);
int endIndex = UpperBound(times, end);
if (startIndex >= times.Count || endIndex < 0 || startIndex > endIndex)
{
return 0;
}
int result = prefix[endIndex];
if (startIndex > 0)
{
result -= prefix[startIndex - 1];
}
return result;
}
public int GetTotal()
{
return PrefixSums.LastOrDefault();
}
public static int LowerBound(List<TimeSpan> arr, TimeSpan target)
{
int left = 0, right = arr.Count;
while (left < right)
{
int mid = left + (right - left) / 2;
if (arr[mid] < target)
{
left = mid + 1;
}
else
{
right = mid;
}
}
return left;
}
public static int UpperBound(List<TimeSpan> arr, TimeSpan target)
{
int left = 0, right = arr.Count;
while (left < right)
{
int mid = left + (right - left) / 2;
if (arr[mid] <= target)
{
left = mid + 1;
}
else
{
right = mid;
}
}
return left - 1;
}
}
}