Editorial for COCI '08 Regional #6 Cvjetici
                Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
        Submitting an official solution before solving the problem yourself is a bannable offence.
Simulating the growth of plants by maintaining a two-dimensional table is of time and space complexity , where 
 is the maximum coordinate. This solution scores 30 points.
Let  be the number of plants for which a flower may grow at coordinate 
 (but hasn't yet). The sequence 
 initially contains all zeros and changes when plant 
 grows:
- Flowers will grow at coordinates 
and
, a total of
of them, so we output that number. After these flowers have grown, there are no more available plants at coordinates
and
, so we reset
and
to zero.
 - The horizontal segment 
is now available to grow flowers, so we increase
by one for each of those coordinates.
 
Directly implementing the above algorithm yields a solution of complexity , scoring 45 points.
For full score, we need a data structure that supports the following operations:
- Set 
;
 - Increase 
by one for each
in
.
 
Some of the data structures that do this are interval trees and Fenwick trees for  per query, or splitting the sequence 
 into blocks of size about 
, for 
 per query. See task Jagoda for details on this structure.
Comments