1

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);
        }
    }
  • Is it a problem on an online judge? If yes can you please link to the original problem? – ksb Aug 13 '13 at 21:30
  • Yes, it is about the AUG13 -> seabal on CodeChef: http://www.codechef.com/problems/SEABAL – Paul Herman Aug 15 '13 at 18:37
  • But in the problem we need to return all intervals within a given range which is different from the stabbing query(which returns all intervals containing a given point). – ksb Aug 16 '13 at 22:26
  • The code I posted was correct, the mistake was in the processing of the segments. – Paul Herman Aug 24 '13 at 19:31

0 Answers0