Segment tree challenge
Bounds checking simplification

Your bounds checking code is overly complicated.  This total overlap check:


/* Total overlap */
if(node-&gt;start &gt;= start &amp;&amp; node-&gt;start &lt;= end)
{
    if(node-&gt;end &lt;= end &amp;&amp; node-&gt;end &gt;= start)
        return node-&gt;sum;
}



could be reduced to:

/* Total overlap */
if(node-&gt;start &gt;= start &amp;&amp; node-&gt;end &lt;= end)
{
    return node-&gt;sum;
}


In this partial overlap check:


/* Partial overlap */
if((start &gt;= node-&gt;start &amp;&amp; start &lt;= node-&gt;end) ||
       (end &gt;= node-&gt;end &amp;&amp; end &lt;= node-&gt;start) ||
       (start &lt;= node-&gt;start &amp;&amp; end &gt;= node-&gt;start))



I noticed that the second of the three conditions always evaluates to false.  Some of the other parts are also unnecessary.  In fact the whole thing could be rewritten as:

/* Partial overlap */
if (start &lt;= node-&gt;end &amp;&amp; end &gt;= node-&gt;start)


Use of globals

The function build_tree() references the array ra directly.  It would be better for that array to be passed in as an argument.  Otherwise that function can't be used for any other segment tree.