Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use a range tree. A range tree is n log(n), just like the sorted begin/end points, but it has the additional advantage that overlapping ranges will reduce the number of elements (but maybe increase the cost of insertion) Snippet (untested)</p> <pre><code>struct segment { struct segment *ll, *rr; float lo, hi; }; struct segment * newsegment(float lo, float hi) { struct segment * ret; ret = malloc (sizeof *ret); ret-&gt;lo = lo; ret-&gt;hi = hi; ret-&gt;ll= ret-&gt;rr = NULL; return ret; } struct segment * insert_range(struct segment *root, float lo, float hi) { if (!root) return newsegment(lo, hi); /* non-overlapping(or touching) ranges can be put into the {l,r} subtrees} */ if (hi &lt; root-&gt;lo) { root-&gt;ll = insert_range(root-&gt;ll, lo, hi); return root; } if (lo &gt; root-&gt;hi) { root-&gt;rr = insert_range(root-&gt;rr, lo, hi); return root; } /* when we get here, we must have overlap; we can extend the current node ** we also need to check if the broader range overlaps the child nodes */ if (lo &lt; root-&gt;lo ) { root-&gt;lo = lo; while (root-&gt;ll &amp;&amp; root-&gt;ll-&gt;hi &gt;= root-&gt;lo) { struct segment *tmp; tmp = root-&gt;ll; root-&gt;lo = tmp-&gt;lo; root-&gt;ll = tmp-&gt;ll; tmp-&gt;ll = NULL; // freetree(tmp); } } if (hi &gt; root-&gt;hi ) { root-&gt;hi = hi; while (root-&gt;rr &amp;&amp; root-&gt;rr-&gt;lo &lt;= root-&gt;hi) { struct segment *tmp; tmp = root-&gt;rr; root-&gt;hi = tmp-&gt;hi; root-&gt;rr = tmp-&gt;rr; tmp-&gt;rr = NULL; // freetree(tmp); } } return root; } float total_width(struct segment *ptr) { float ret; if (!ptr) return 0.0; ret = ptr-&gt;hi - ptr-&gt;lo; ret += total_width(ptr-&gt;ll); ret += total_width(ptr-&gt;rr); return ret; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload