Cod sursa(job #2823443)

Utilizator lolismekAlex Jerpelea lolismek Data 28 decembrie 2021 15:28:58
Problema Marbles Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.2 kb
#include <vector>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;

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

const int CULORI = 64;
vector <int> marbles[CULORI + 1];
unordered_map <int, int> umap;

int main(){
    int n, q;
    fin >> n >> q;
    for(int i = 0; i < n; i++){
        int x, culoare;
        fin >> x >> culoare;
        marbles[culoare].push_back(x);
        umap.insert({x, culoare});
    }
    for(int i = 1; i <= CULORI; i++) sort(marbles[i].begin(), marbles[i].end());
    while(q--){
        int cerr, x, y;
        fin >> cerr >> x >> y;
        if(cerr == 0){
            int c = umap[x];
            umap.erase(x);
            umap.insert({x + y, c});
            int st = 0, dr = marbles[c].size();
            while(dr - st > 1){
                int mij = (st + dr) / 2;
                if(marbles[c][mij] > x) dr = mij;
                else st = mij;
            }
            marbles[c][st] += y;
        }else{
            int ans = 0;
            for(int c = 1; c <= CULORI; c++){
                if(marbles[c].size() > 0){
                    int st = 0, dr = (int)marbles[c].size();
                    while(dr - st > 1){
                        int mij = (st + dr) / 2;
                        if(marbles[c][mij] > x) dr = mij;
                        else st = mij;
                    }
                    int poz1 = st;
                    st = 0, dr = (int)marbles[c].size();
                    while(dr - st > 1){
                        int mij = (st + dr) / 2;
                        if(marbles[c][mij] > y) dr = mij;
                        else st = mij; 
                    }
                    int poz2 = st;
                    while(marbles[c][poz1] < x && poz1 + 1 < (int)marbles[c].size()) poz1++;
                    //while(marbles[c][poz2] < y && poz2 + 1 < (int)marbles[c].size()) poz2++;
                    if(poz1 < (int)marbles[c].size() && poz2 < (int)marbles[c].size() && marbles[c][poz1] >= x && marbles[c][poz2] <= y) ans = max(ans, poz2 - poz1 + 1);
                }
            }
            fout << ans << '\n';
        }
    }
    return 0;
}