Answer the question
In order to leave comments, you need to log in
How to speed up python code?
The code speed does not meet the conditions, less than a second is needed, there are 1.089 seconds. Are there any other ways to speed up the code?
n, m = [int(i) for i in input().split()]
arr = {}
summa = 0
for i in range(m):
l, r, x = [int(i) for i in input().split()]
for j in range(l-1, r):
get = arr.get(j,0)
if get < x:
summa += x - get
arr[j] = x
print(summa)
Answer the question
In order to leave comments, you need to log in
Unfortunately, it is unlikely that it will be possible to speed up the python code. You can try collective farm methods to try to do something, but most likely this will not help. If you need to speed up this task, try using another programming language that is best not interpreted.
I assume that the condition of your task is as follows - a bunch of requests of the form <beginning of the segment, end, value> are given. All cells in this segment are filled with the given value if they are less than it. We need to find the sum of all the cells at the end.
There are two solutions here. The first - through a tree of segments with a delayed change. The tree itself turns out to be a little strange, because it does not count anything in intermediate vertices, but will only store the pending change. The physical meaning of the delayed change is "all cells in this subtree must be at least this value". Relaxation (descent down) of the deferred change occurs as follows: the deferred change in the children is replaced by the maximum of what lies there and the deferred value in the parent. The value in the parent is replaced with 0. When querying, descend recursively down the tree, relaxing the changes until the current subtree lies entirely within the query. Then overwrite the pending value at the current vertex if it is less. At the end of query processing, relax all vertices from top to bottom and sum the values in the leaves.
The second solution - through the method of scanning line can be easier to implement due to the presence of built-in data structures. I think there should be a structure in python that can quickly add a number, remove a number, and take the maximum from all numbers. C++ has std::set or std::priority_queue.
For each query segment, create two events of the form <coordinate, segment opened/closed, value> (figure out where to put +-1 there so that the length of the segment in coordinates is equal to the number of covered cells). Throw them all into one array and sort by coordinate. Then go left to right. Apply the current event - either add a number to your data structure or remove it from there. Between the current and the next event in coordinates, all cells are covered by the same set of segments, so you can quickly calculate them all, because your structure can take the maximum. Add to the answer the difference in coordinates between the current and next events, multiplied by the value of the maximum from the structure. All.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question