Cod sursa(job #2823108)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 26 decembrie 2021 23:11:18
Problema Marbles Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.57 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

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

const int maxN = 100005;
int n, m, c, s[66][maxN], npow = 1;
struct bila {
    int coord, color;
}v[maxN];

bool cmp(bila a, bila b)
{
    return a.coord < b.coord;
}

int main()
{
    fin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        fin >> v[i].coord >> v[i].color;
        c = max(c, v[i].color);
    }
    sort(v + 1, v + n + 1, cmp);
    for(int i = 1; i <= n; i++)
        s[v[i].color][i] = 1;
    for(int i = 1; i <= c; i++)
        for(int j = 1; j <= n; j++)
            s[i][j] += s[i][j - 1];
    while(npow * 2 <= n)
        npow = npow * 2;
    for(int j = 1; j <= m; j++)
    {
        int op, a, b;
        fin >> op >> a >> b;
        if(op == 0)
        {
            int poz = 0;
            for(int i = npow; i > 0; i >>= 1)
            {
                if(poz + i <= n && v[poz + i].coord <= a)
                    poz = poz + i;
            }
            v[poz].coord = a + b;
        }
        if(op == 1)
        {
            int st = 0, dr = 0, ans = 0;
            for(int i = npow; i > 0; i >>= 1)
            {
                if(st + i <= n && v[st + i].coord < a)
                    st = st + i;
                if(dr + i <= n && v[dr + i].coord <= b)
                    dr = dr + i;
            }
            for(int i = 1; i <= c; i++)
                ans = max(ans, s[i][dr] - s[i][st]);
            fout << ans << '\n';
        }
    }
    return 0;
}