Cod sursa(job #2823097)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 26 decembrie 2021 22:19:56
Problema Marbles Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int maxN = 100005;
int n, m, c, s[66][maxN], v[maxN], npow = 1;

int main()
{
    fin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        int a, b;
        fin >> a >> b;
        v[i] = a;
        s[b][i] = 1;
        c = max(c, b);
    }
    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] <= a)
                    poz = poz + i;
            }
            v[poz] = 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] < a)
                    st = st + i;
                if(dr + i <= n && v[dr + i] <= 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;
}