Cod sursa(job #2549516)

Utilizator MateiAruxandeiMateiStefan MateiAruxandei Data 17 februarie 2020 19:14:52
Problema Marbles Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.89 kb
#include <fstream>
#include <algorithm>

using namespace std;

class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;
 
	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}
 
public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}
 
	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
 
	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};

ifstream cin("marbles.in");
ofstream cout("marbles.out");

int s[65][100005];

int bound(int cul, int pz, int which){
	int st = 1;
	int dr = s[cul][0];
	int bs = 0;
	while(st <= dr){
		int mij = (st + dr) / 2;
		if(s[cul][mij] <= pz){
			bs = mij;
			st = mij + 1;
		}
		else dr = mij - 1;
	}

	if(!which){
		if(s[cul][bs] < pz)
			++bs;
	}
	return bs;
}

int main(){
	int n, m;
	cin >> n >> m;

	int maxx = 0;
	for(int i = 1; i <= n; ++i){
		int x, cul;
		cin >> x >> cul;

		s[cul][++s[cul][0]] = x;
		maxx = max(maxx, cul);
	}

	for(int i = 1; i <= maxx; ++i)
		sort(s[i] + 1, s[i] + s[i][0] + 1);

	int lgMax, pz, st, fs;
	for(int i = 1; i <= m; ++i){
		int t, xi, xj;
		cin >> t >> xi >> xj;

		lgMax = 0;
		for(int cul = 1; cul <= maxx; ++cul)
			if(t == 0){
				pz = bound(cul, xi, 0);
				if(s[cul][pz] == xi)
					s[cul][pz] += xj;
			}
			else if(t == 1){
				st = bound(cul, xi, 0);
				fs = bound(cul, xj, 1);
				lgMax = max(lgMax, fs - st + 1);	
			}
		if(t)
			cout << lgMax << '\n';
	}
	return 0;
}