import std.stdio : writeln;
import std.algorithm.comparison: min;
import std.algorithm.iteration: sum;
import core.time: MonoTime, Duration;
T[] sum_subranges(T)(T[] input, uint range)
{
T[] result = new T[(input.length + 1)/range +1];
if (range == 0)
{
return result;
}
size_t it;
for (uint i; i < input.length; i=min(i+range, input.length))
{
result[it++] = sum(input[i..min(i+range, input.length)]);
}
return result;
}
unittest
{
assert(sum_subranges([1,1,1], 2) == [2, 1]);
assert(sum_subranges([1,1,1,2,3,3], 2) == [2, 3, 6]);
assert(sum_subranges([], 2) == []);
assert(sum_subranges([1], 2) == [1]);
assert(sum_subranges([1], 0) == []);
}
int main()
{
int[1000000] v;
for (int i=0; i < 1000000; ++i)
v[i] = i;
int sum;
MonoTime beg = MonoTime.currTime;
for (int i=0; i < 100; i++)
sum += cast(int)sum_subranges(v,2).length;
MonoTime end = MonoTime.currTime;
writeln(end-beg);
writeln(sum);
return sum;
}
aW1wb3J0IHN0ZC5zdGRpbyA6IHdyaXRlbG47CmltcG9ydCBzdGQuYWxnb3JpdGhtLmNvbXBhcmlzb246IG1pbjsKaW1wb3J0IHN0ZC5hbGdvcml0aG0uaXRlcmF0aW9uOiBzdW07CmltcG9ydCBjb3JlLnRpbWU6IE1vbm9UaW1lLCBEdXJhdGlvbjsKCgpUW10gc3VtX3N1YnJhbmdlcyhUKShUW10gaW5wdXQsIHVpbnQgcmFuZ2UpCnsKICAgIFRbXSByZXN1bHQgPSBuZXcgVFsoaW5wdXQubGVuZ3RoICsgMSkvcmFuZ2UgKzFdOwogICAgaWYgKHJhbmdlID09IDApCiAgICB7CiAgICAgICAgcmV0dXJuIHJlc3VsdDsKICAgIH0KICAgIHNpemVfdCBpdDsKICAgIGZvciAodWludCBpOyBpIDwgaW5wdXQubGVuZ3RoOyBpPW1pbihpK3JhbmdlLCBpbnB1dC5sZW5ndGgpKQogICAgewogICAgICAgIHJlc3VsdFtpdCsrXSA9IHN1bShpbnB1dFtpLi5taW4oaStyYW5nZSwgaW5wdXQubGVuZ3Ro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