Cod sursa(job #2533288)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 28 ianuarie 2020 21:19:45
Problema Marbles Scor 90
Compilator cpp-32 Status done
Runda Arhiva de probleme Marime 2.4 kb
/*
                `-/oo+/-   ``
              .oyhhhhhhyo.`od
             +hhhhyyoooos. h/
            +hhyso++oosy- /s
           .yoooossyyo:``-y`
            ..----.` ``.-/+:.`
                   `````..-::/.
                  `..```.-::///`
                 `-.....--::::/:
                `.......--::////:
               `...`....---:::://:
             `......``..--:::::///:`
            `---.......--:::::////+/`
            ----------::::::/::///++:
            ----:---:::::///////////:`
            .----::::::////////////:-`
            `----::::::::::/::::::::-
             `.-----:::::::::::::::-
               ...----:::::::::/:-`
                 `.---::/+osss+:`
                   ``.:://///-.
*/
#include <fstream>
#include <algorithm>

std::ifstream in ("marbles.in");
std::ofstream out ("marbles.out");

int a[5 + 64][5 + 100000];
int v[5 + 64];

int cautst(char color, int elem) {
    int st, dr, mid, ans;
    st = 1;
    dr = v[color];
    ans = dr + 1;

    while(st <= dr) {
        mid = (st + dr) >> 1;
        if(a[color][mid] >= elem) {
            ans = mid;
            dr = mid - 1;
        } else st = mid + 1;
    }

    return ans;
}

int cautdr(char color, int elem) {
    int st, dr, mid, ans(0);
    st = 1;
    dr = v[color];

    while(st <= dr) {
        mid = (st + dr) >> 1;
        if(a[color][mid] <= elem) {
            ans = mid;
            st = mid + 1;
        } else dr = mid - 1;
    }

    return ans;
}

int main() {
    int n, m, culmax(0), i, j, x, c, y, tip, Max(0);

    in >> n >> m;

    for(i = 1; i <= n; i++) {
        int x, c;
        in >> x >> c;
        a[c][++v[c]] = x;

        if(c > culmax) culmax = c;
    }

    for(i = 1; i <= culmax; i++) if(v[i] > 0)
            std::sort(a[i] + 1, a[i] + v[i] + 1);

    for(i = 1; i <= m; i++) {
        in >> tip >> x >> y;

        for(j = 1; j <= culmax; j++) {
            if(tip == 0) {
                int pos = cautst(j, x);
                if(a[j][pos] == x)
                    a[j][pos] += y;
            } else {
                int posst = cautst(j, x);
                int posdr = cautdr(j, y);
                if(posdr - posst + 1 > Max)
                    Max = posdr - posst + 1;
            }
        }

        if(tip == 1) {
            out << Max << '\n';
            Max = 0;
        }
    }

    return 0;
}