Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
2.1 kB
2
Indexable
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

vector<vector<pair<int,int>>> user(101);

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

void add(int mX, int mY)
{
    user[mX].push_back(make_pair(mX+1, mY));
    user[mX+1].push_back(make_pair(mX, mY));
    
    // Sort the vectors to maintain order
    sort(user[mX].begin(), user[mX].end(), [](const pair<int,int>& a, const pair<int,int>& b) {
        return a.second < b.second;
    });
    sort(user[mX+1].begin(), user[mX+1].end(), [](const pair<int,int>& a, const pair<int,int>& b) {
        return a.second < b.second;
    });
}

void remove(int mX, int mY)
{
    user[mX].erase(remove(user[mX].begin(), user[mX].end(), make_pair(mX, mY)), user[mX].end());
    user[mX+1].erase(remove(user[mX+1].begin(), user[mX+1].end(), make_pair(mX+1, mY)), user[mX+1].end());
}

int numberOfCross(int mID)
{
    int id = mID;
    int res = 0;
    auto it = upper_bound(user[id].begin(), user[id].end(), make_pair(mID,0), 
        [](const pair<int,int>& a, const pair<int,int>& b) {
            return a.second < b.second;
        });
    
    while(it != user[id].end())
    {
        res++;
        auto s = *it;
        id = s.first;
        it = upper_bound(user[id].begin(), user[id].end(), make_pair(s.first,s.second),
            [](const pair<int,int>& a, const pair<int,int>& b) {
                return a.second < b.second;
            });
    }
    return res;
}

int participant(int mX, int mY)
{
    int id = mX;
    auto it = lower_bound(user[id].begin(), user[id].end(), make_pair(mX,mY),
        [](const pair<int,int>& a, const pair<int,int>& b) {
            return a.second < b.second;
        });
    
    while(it != user[id].begin())
    {
        --it;
        auto s = *it;
        id = s.first;
        it = lower_bound(user[id].begin(), user[id].end(), make_pair(s.first,s.second),
            [](const pair<int,int>& a, const pair<int,int>& b) {
                return a.second < b.second;
            });
    }
    return id;
}
Leave a Comment