Where is it possible to find reference for an implementation of segment trees in solving the stabbing problem? The stabbing problem asks to return all intervals (from a predefined set of m intervals of the form [a, b], 1 <= a <= b <= n where n is given) containing a given point q.
Corner case: intervals of only one point [a, a].
Until now I managed to get the following code, but it surely does not work and I can't really find the bug:
int n, m, x[100005], y[100005], q;
list< int > segm[200005];
//Search for q in the segment tree
void segmQuery(int node, int left, int right)
{
for (list<int>::iterator i = segm[node].begin(); i != segm[node].end(); i++)
{
//Process the segment
}
if (left < right)
{
int middle = left + (right - left) / 2;
if (q <= middle)
segmQuery(2 * node, left, middle);
if (q > middle)
segmQuery(2 * node + 1, middle + 1, right);
}
}
//Add the q-th segment into the tree
//Segment is denoted by x[q], y[q]
void segmAdd(int node, int left, int right)
{
if ((x[q] <= left) && (right <= y[q]))
segm[node].push_back(q);
else
{
int middle = left + (right - left) / 2;
if (x[q] <= middle)
segmAdd(2 * node, left, middle);
if (y[q] > middle)
segmAdd(2 * node + 1, middle + 1, right);
}
}