Cod sursa(job #3277054)

Utilizator Andrei2454Andrei Hulubei Andrei2454 Data 15 februarie 2025 11:48:38
Problema Marbles Scor 100
Compilator cpp-64 Status done
Runda vs11_12_vine_oji_2025 Marime 2.22 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fin("marbles.in");
ofstream fout("marbles.out");

int n , m;
vector<int> L[64];
unordered_map<int , int> M;

int CautBin(int lun , int x)
{
    int st = 0 , dr , mij;
    dr = L[lun].size() - 1;
    while(st <= dr)
    {
        mij = (st + dr) / 2;
        if(L[lun][mij] == x) return mij;
        if(L[lun][mij] < x) st = mij + 1;
        else dr = mij - 1;
    }
    return -1;
}

int CautBin1(int lun , int x)
{
    int st = 0 , dr , mij , p;
    dr = L[lun].size() - 1;
    p = L[lun].size();
    while(st <= dr)
    {
        mij = (st + dr) / 2;
        if(L[lun][mij] >= x)
        {
            p = mij;
            dr = mij - 1;
        }
        else st = mij + 1;
    }
    return p;
}

int CautBin2(int lun, int x)
{
    int st = 0 , dr , mij , p = -1;
    dr = L[lun].size() - 1;
    while(st <= dr)
    {
        mij = (st + dr) / 2;
        if(L[lun][mij] <= x)
        {
            p = mij;
            st = mij + 1;
        }
        else dr = mij - 1;
    }
    return p;
}

int main()
{
    int i , poz , cul , task , x , y , mx , poz1 , poz2 , k , cnt;
    fin >> n >> m;
    for (i = 1; i <= n; i++)
    {
        fin >> poz >> cul;
        cul--;
        L[cul].push_back(poz);
        M[poz] = cul;
    }
    for(i = 0; i < 64; i++)
        sort(L[i].begin(), L[i].end());
    for(i = 1; i <= m; i++)
    {
        fin >> task >> x >> y;
        if(task == 0)
        {
            if(M.find(x) != M.end())
            {
                cul = M[x];
                k = CautBin(cul, x);
                if(k != -1)
                    L[cul][k] = x + y;
                M.erase(x);
                M[x + y] = cul;
            }
        }
        else if(task == 1)
        {
            mx = 0;
            for(cul = 0; cul < 64; cul++)
            {
                poz1 = CautBin1(cul, x);
                poz2 = CautBin2(cul, y);
                cnt = 0;
                if (poz1 < L[cul].size() and poz2 >= 0 and poz2 >= poz1)
                    cnt = poz2 - poz1 + 1;
                mx = max(mx, cnt);
            }
            fout << mx << "\n";
        }
    }
    return 0;
}