Cod sursa(job #2573092)

Utilizator Coroian_DavidCoroian David Coroian_David Data 5 martie 2020 15:47:38
Problema Marbles Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <fstream>
#include <algorithm>

#define MAX_N 100000
#define MAX_NR 64

#define xx first
#define yy second

using namespace std;

ifstream f("marbles.in");

int n, m;
int ap[MAX_N + 1][MAX_NR + 1];

pair<int, int> v[MAX_N + 1];

void readFile()
{
    f >> n >> m;

    for(int i = 1; i <= n; i ++)
        f >> v[i].xx >> v[i].yy;
}

void preCalc()
{
    sort(v + 1, v + n + 1);

    for(int i = 1; i <= n; i ++)
    {
        for(int j = 1; j <= 64; j ++)
            ap[i][j] = ap[i - 1][j];

        ap[i][v[i].yy] ++;
    }
}

int cautBin(int nr)
{
    int i = 0;
    int pas = 1 << 16;
    while(pas != 0)
    {
        if(i + pas <= n && v[i + pas].xx < nr)
            i += pas;

        pas >>= 1;
    }

    return i + 1;
}

void ansQues()
{
    ofstream g("marbles.out");

    while(m > 0)
    {
        m --;

        int tip, i, j;
        f >> tip >> i >> j;
        if(tip == 0)
        {
            int p = cautBin(i);
            v[p].xx += j;
        }

        else
        {
            int pi = cautBin(i);
            int pj = cautBin(j);

            if(v[pj].xx > j)
                pj --;

            int rez = 0;
            for(int c = 1; c <= 64; c ++)
                rez = max(rez, ap[pj][c] - ap[pi - 1][c]);

            g << rez << "\n";
        }
    }

    g.close();
}

int main()
{
    readFile();

    preCalc();

    ansQues();

    return 0;
}