Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.2 kB
1
Indexable
Never
#include <map>

using namespace std;

struct Comparator
{
    inline bool operator()(pair<int,int>a1,pair<int,int>a2)
    {
        return a1.first < a2.first;
    }
};

map<int, int> Line[105];

void init()
{
    for (int i = 0; i <= 100; i++)
    {
        Line[i].clear();
    }
}

void add(int mX, int mY)
{
    Line[mX].insert(make_pair(mY,mX+1));
    Line[mX+1].insert(make_pair(mY,mX));
}

void remove(int mX, int mY)
{
    Line[mX].erase(mY);
    Line[mX+1].erase(mY);
}

int numberOfCross(int mID)
{
    int count = 0;

    if(!Line[mID].empty())
    {
        int i = mID;
        int r = 0;
        auto it = Line[i].upper_bound(r);
        while ( it != Line[i].end())
        {
            count++;
            i = it->second;
            r = it->first;
            it = Line[i].upper_bound(r);
        }
    }
    return count;
}

int participant(int mX, int mY)
{
    int r = mY;
    int c = mX;
    auto it = Line[c].lower_bound(r);

    while (it != Line[c].begin() && Line[c].empty() == false)
    {
        it--;
        c = it->second;
        r = it->first;
        it = Line[c].lower_bound(r);
    }

    return c;
}