I solved this problem with segment tree, but when I submitted my solution, it told me "Memory limit exceeded on test 4". I calculated my static memory usage, it's far from 512MiB. So why?
Just now, I tried changing my solution to be the same as official solution (linear memory usage), and I got MLE again...
I copied your solution to custom invocation, and tried to simplify as much as possible without changing the verdict. Eventually, I realised: the culprit is
Here you essentially create quadratic memory by creating entries in the
bol[i]
for all numbers, that are present in any row from1
toi
-th — remember that even access to astd::map
creates an entry. So, ifn = 1e5, m = 1
, and all numbers are distinct, than you create approximatelyn*n/2
entries in the maps.This is really hard for me to find it... especially when I felt desperate because of the verdict in the contest. :(
Anyway, thanks for your help! Appreciate ^_^