Repository navigation
Expand file tree
/
Copy patharraymanip.py
More file actions
60 lines (48 loc) · 1.51 KB
/
Copy patharraymanip.py
File metadata and controls
60 lines (48 loc) · 1.51 KB
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
# from array import array
from time import perf_counter
# import numpy as np
# ctypes - appears to be for interfacing with C
# Convert to C++
def arrmanip(n, queries):
# beginfunc = perf_counter()
# Could also use itertools.repeat(0, n)
# This is too slow:
arr = list([0] * n)
# This is even slower!
# arr = array('L', [0] * n)
# Quite a bit slower!!!
# arr = np.zeros(n, dtype=np.int32)
maxval = 0
for start, stop, inc in queries:
# Subtract 1 from start because arrays in Python are 0-indexed, and the
# queries are designed for 1-indexed arrays:
'''
# Too slow, O(n**2)
for i in range(start - 1, stop):
arr[i] += inc
# Better to take hit here or do max(arr)?
# if arr[i] > maxval:
# maxval = arr[i]
print('.', end='', flush=True)
'''
arr[start - 1] += inc
arr[stop] -= inc
# res = max(arr)
total = 0
for i in arr:
total += i
if total > maxval:
maxval = total
# endfunc = perf_counter()
# print(f'\nCompleted array manipulation in {endfunc - beginfunc:,.6f} seconds')
return maxval
def main():
beginfunc = perf_counter()
n, m = map(int, input().split())
queries = [list(map(int, input().split())) for _ in range(m)]
endfunc = perf_counter()
print(f'Read in queries in {endfunc - beginfunc:,.6f} seconds:')
res = arrmanip(n, queries)
print(f'{res:,}')
if __name__ == '__main__':
main()