in reply to Re: Code efficiency / algorithm
in thread Code efficiency / algorithm

IIRC, you can build a segment tree in O (n log2 n) time, while queries take O (log2 n + k) time.

I wonder if there isn't a way to get O(1) query time by exploiting the nature of the keys and building a different data structure.

The keys here are dates. There aren't that many distinct dates in a year. By converting all dates to an offset from some given starting date, you can build an array indexible by date offset. Each element of this array, if present, holds an array of references to records that are "effective" on that date.

Replies are listed 'Best First'.
Re^3: Code efficiency / algorithm
by tall_man (Parson) on Jan 15, 2003 at 01:37 UTC
    Great idea, dws. One modification, though. Since the date information is sparse, a hash of the distinct dates (or date offsets) would be better than an array.