Skip to main content
added 1302 characters in body
Source Link
Kelly Bundy
  • 3.2k
  • 7
  • 21

If you don't mind the asymmetry of solve reading the input itself but not printing the output itself, you could also make it a generator:

from itertools import accumulate

def input_ints():
    return map(int, input().split())

def solve():
    _, q = input_ints()
    cumulative = [0, *accumulate(input_ints())]

    for _ in range(q):
        a, b = input_ints()
        yield cumulative[b] - cumulative[a - 1]

print('\n'.join(map(str, solve())))

I do mind that asymmetry, though. Another symmetric way, instead of solve doing both input and output, would be to solve doing neither of them:

from itertools import accumulate

def input_ints():
    return map(int, input().split())

def solve(x, queries):
    cumulative = [0, *accumulate(x)]

    for a, b in queries:
        yield cumulative[b] - cumulative[a - 1]

_, q = input_ints()
x = input_ints()
queries = (input_ints() for _ in range(q))
results = solve(x, queries)
print('\n'.join(map(str, results)))

Granted, now the "main block" doesn't look nice. But solve is now very nice, and it can also be tested nicely by just calling it with data, for example:

>>> list(solve([3, 2, 4, 5, 1, 1, 5, 3],
               [[2, 4], [5, 6], [1, 8], [3, 3]]))
[11, 2, 24, 4]

If you don't mind the asymmetry of solve reading the input itself but not printing the output itself, you could also make it a generator:

from itertools import accumulate

def input_ints():
    return map(int, input().split())

def solve():
    _, q = input_ints()
    cumulative = [0, *accumulate(input_ints())]

    for _ in range(q):
        a, b = input_ints()
        yield cumulative[b] - cumulative[a - 1]

print('\n'.join(map(str, solve())))

I do mind that asymmetry, though. Another symmetric way, instead of solve doing both input and output, would be to solve doing neither of them:

from itertools import accumulate

def input_ints():
    return map(int, input().split())

def solve(x, queries):
    cumulative = [0, *accumulate(x)]

    for a, b in queries:
        yield cumulative[b] - cumulative[a - 1]

_, q = input_ints()
x = input_ints()
queries = (input_ints() for _ in range(q))
results = solve(x, queries)
print('\n'.join(map(str, results)))

Granted, now the "main block" doesn't look nice. But solve is now very nice, and it can also be tested nicely by just calling it with data, for example:

>>> list(solve([3, 2, 4, 5, 1, 1, 5, 3],
               [[2, 4], [5, 6], [1, 8], [3, 3]]))
[11, 2, 24, 4]
added 1 character in body
Source Link
Kelly Bundy
  • 3.2k
  • 7
  • 21

Your solution is alright and I got it accepted as-is. Just had to choose "PyPy3" instead of "CPython3".

Anyway...

I'd use accumulate and a helper function to reduce code duplication and make the interesting code clearer:

from itertools import accumulate

def input_ints():
    return map(int, input().split())

def solve():
    _, q = input_ints()
    cumulative = [0, *accumulate(input_ints())]
    
    for _ in range(q):
        a, b = input_ints()
        print(cumulative[b] - cumulative[a - 1])

solve()

This still gets TLE with CPython3, though, and gets accepted in PyPy3 in the same 0.81 seconds as yours.

As @setris pointed out you can get it accepted by using a single print. Here's my version of that, not mixing the calculations with the printing, which I find clearer. Got accepted with CPythonCPython3 in 0.69 seconds.

from itertools import accumulate

def input_ints():
    return map(int, input().split())

def solve():
    _, q = input_ints()
    cumulative = [0, *accumulate(input_ints())]

    results = []
    for _ in range(q):
        a, b = input_ints()
        results.append(cumulative[b] - cumulative[a - 1])

    print('\n'.join(map(str, results)))

solve()

Your solution is alright and I got it accepted as-is. Just had to choose "PyPy3" instead of "CPython3".

Anyway...

I'd use accumulate and a helper function to reduce code duplication and make the interesting code clearer:

from itertools import accumulate

def input_ints():
    return map(int, input().split())

def solve():
    _, q = input_ints()
    cumulative = [0, *accumulate(input_ints())]
    
    for _ in range(q):
        a, b = input_ints()
        print(cumulative[b] - cumulative[a - 1])

solve()

This still gets TLE with CPython3, though, and gets accepted in PyPy3 in the same 0.81 seconds as yours.

As @setris pointed out you can get it accepted by using a single print. Here's my version of that, not mixing the calculations with the printing, which I find clearer. Got accepted with CPython in 0.69 seconds.

from itertools import accumulate

def input_ints():
    return map(int, input().split())

def solve():
    _, q = input_ints()
    cumulative = [0, *accumulate(input_ints())]

    results = []
    for _ in range(q):
        a, b = input_ints()
        results.append(cumulative[b] - cumulative[a - 1])

    print('\n'.join(map(str, results)))

solve()

Your solution is alright and I got it accepted as-is. Just had to choose "PyPy3" instead of "CPython3".

Anyway...

I'd use accumulate and a helper function to reduce code duplication and make the interesting code clearer:

from itertools import accumulate

def input_ints():
    return map(int, input().split())

def solve():
    _, q = input_ints()
    cumulative = [0, *accumulate(input_ints())]
    
    for _ in range(q):
        a, b = input_ints()
        print(cumulative[b] - cumulative[a - 1])

solve()

This still gets TLE with CPython3, though, and gets accepted in PyPy3 in the same 0.81 seconds as yours.

As @setris pointed out you can get it accepted by using a single print. Here's my version of that, not mixing the calculations with the printing, which I find clearer. Got accepted with CPython3 in 0.69 seconds.

from itertools import accumulate

def input_ints():
    return map(int, input().split())

def solve():
    _, q = input_ints()
    cumulative = [0, *accumulate(input_ints())]

    results = []
    for _ in range(q):
        a, b = input_ints()
        results.append(cumulative[b] - cumulative[a - 1])

    print('\n'.join(map(str, results)))

solve()
added 119 characters in body
Source Link
Kelly Bundy
  • 3.2k
  • 7
  • 21

Your solution is alright and I got it accepted as-is. Just had to choose "PyPy3" instead of "CPython3".

Anyway...

I'd use accumulate and a helper function to reduce code duplication and make the interesting code clearer:

from itertools import accumulate

def solveinput_ints():
    _, q =return map(int, input().split())

def solve():
    _, q = input_ints()
    cumulative = [0, *accumulate(map(int, input().splitinput_ints()))]
    
    for _ in range(q):
        a, b = map(int, input().splitinput_ints())
        print(cumulative[b] - cumulative[a - 1])

solve()

This still gets TLE with CPython3, though, and gets accepted in PyPy3 in the same 0.81 seconds as yours.

As @setris pointed out you can get it accepted by using a single print. Here's my version of that, not mixing the calculations with the printing, which I find clearer. Got accepted with CPython in 0.6569 seconds.

from itertools import accumulate

def solveinput_ints():
    _, q =return map(int, input().split())

def solve():
    _, q = input_ints()
    cumulative = [0, *accumulate(map(int, input().splitinput_ints()))]

    results = []
    for _ in range(q):
        a, b = map(int, input().splitinput_ints())
        results.append(cumulative[b] - cumulative[a - 1])

    print('\n'.join(map(str, results)))

solve()

Your solution is alright and I got it accepted as-is. Just had to choose "PyPy3" instead of "CPython3".

Anyway...

I'd use accumulate:

from itertools import accumulate

def solve():
    _, q = map(int, input().split())    
    cumulative = [0, *accumulate(map(int, input().split()))]
    
    for _ in range(q):
        a, b = map(int, input().split())
        print(cumulative[b] - cumulative[a - 1])

solve()

This still gets TLE with CPython3, though, and gets accepted in PyPy3 in the same 0.81 seconds as yours.

As @setris pointed out you can get it accepted by using a single print. Here's my version of that, not mixing the calculations with the printing, which I find clearer. Got accepted with CPython in 0.65 seconds.

from itertools import accumulate

def solve():
    _, q = map(int, input().split())    
    cumulative = [0, *accumulate(map(int, input().split()))]

    results = []
    for _ in range(q):
        a, b = map(int, input().split())
        results.append(cumulative[b] - cumulative[a - 1])

    print('\n'.join(map(str, results)))

solve()

Your solution is alright and I got it accepted as-is. Just had to choose "PyPy3" instead of "CPython3".

Anyway...

I'd use accumulate and a helper function to reduce code duplication and make the interesting code clearer:

from itertools import accumulate

def input_ints():
    return map(int, input().split())

def solve():
    _, q = input_ints()
    cumulative = [0, *accumulate(input_ints())]
    
    for _ in range(q):
        a, b = input_ints()
        print(cumulative[b] - cumulative[a - 1])

solve()

This still gets TLE with CPython3, though, and gets accepted in PyPy3 in the same 0.81 seconds as yours.

As @setris pointed out you can get it accepted by using a single print. Here's my version of that, not mixing the calculations with the printing, which I find clearer. Got accepted with CPython in 0.69 seconds.

from itertools import accumulate

def input_ints():
    return map(int, input().split())

def solve():
    _, q = input_ints()
    cumulative = [0, *accumulate(input_ints())]

    results = []
    for _ in range(q):
        a, b = input_ints()
        results.append(cumulative[b] - cumulative[a - 1])

    print('\n'.join(map(str, results)))

solve()
Source Link
Kelly Bundy
  • 3.2k
  • 7
  • 21
Loading