Cod sursa(job #2847611)

Utilizator Sebi_MafteiMaftei Sebastioan Sebi_Maftei Data 11 februarie 2022 09:08:17
Problema Marbles Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.9 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, m, col;
int a[64][100003];
int len[64];

void Citire()
{
    int i, c, x;
    fin >> n >> m;
    for (i = 1; i <= n; i++)
    {
        fin >> x >> c;
        a[c][++len[c]] = x;
        col = max(col, c);
    }
}

int CautBinMME(int color, int poz)
{
    int st, dr, p = 0, mij;
    st = 1;
    dr = len[color];
    while (st <= dr)
    {
        mij = (st + dr) / 2;
        if (a[color][mij] == poz) return mij;
        if (a[color][mij] < poz)
            st = mij + 1;
        else
        {
            dr = mij - 1;
            p = mij;
        }
    }
    return p;
}

int CautBinmmEg(int color, int poz)
{
    int st, dr, p = 0, mij;
    st = 1;
    dr = len[color];
    while (st <= dr)
    {
        mij = (st + dr) / 2;
        if (a[color][mij] == poz) return mij;
        if (a[color][mij] < poz)
        {
            p = mij;
            st = mij + 1;
        }
        else dr = mij - 1;
    }
    return p;
}

void CautBinMax_p1(int x, int y)
{
    int i, st, dr, maxs = 0;
    for (i = 1; i <= col; i++)
    {
        st = CautBinMME(i, x);
        dr = CautBinmmEg(i, y);
        maxs = max(maxs, dr - st + 1);
    }
    fout << maxs << "\n";
}

void Schimb(int x, int y)
{
    int i, poz = 0;
    for (i = 1; i <= col; i++)
    {
        poz = CautBinmmEg(i, x);
        if (a[i][poz] == x)
        {
            a[i][poz] += y;
            return;
        }
    }
}

void Rezolva()
{
    int i, j, t, x, y;
    for (i = 1; i <= col; i++)
        sort(a[i] + 1, a[i] + len[i] + 1);
    for (i = 1; i <= m; i++)
    {
        fin >> t >> x >> y;
        if (t == 1)
            CautBinMax_p1(x, y);
        else
            Schimb(x, y);
    }
}

int main()
{
    Citire();
    Rezolva();
    return 0;
}