Cod sursa(job #1438147)

Utilizator theprdvtheprdv theprdv Data 19 mai 2015 03:15:43
Problema Zeap Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.64 kb
#include <fstream>
#include <algorithm>

using namespace std;

fstream fout("zeap.out", ios::out);
#define MAXN 300001
#define oo 0x3f3f3f3f
#define left  (n << 1)
#define right (left + 1)
#define mid   ((l + r) >> 1)

struct node{
	int max, min, maxD, minD = min = oo;
	int ct;
} T[MAXN * 4];
char *buf, *p;
int val, pos, A[MAXN], B[MAXN], LOG;
bool found, del, ins, X[MAXN];

inline int read_num(){
	int val = 0;
	while (!(*p >= '0' && *p <= '9') && *p != -3) ++p; // <--
	for (; *p >= '0' && *p <= '9'; ++p)
		val = val * 10 + *p - '0';
	return val;
}

inline string read_char(){
	string val;
	while (!(*p >= 'A' && *p <= 'Z') && *p != -3) ++p; // <--
	for (; *p >= 'A' && *p <= 'Z'; ++p)
		val += *p;
	return val;
}

void read(){
	fstream file("zeap.in", ios::in | ios::binary | ios::ate);
	streampos size = file.tellg();
	buf = new char[size];
	file.seekg(0, ios::beg);
	file.read(buf, size);
	file.close();
}

int search(int val){
	int step = LOG, k;

	for (k = 0; step > 0; step >>= 1)
		if (step + k <= A[0] && A[step + k] < val)
			k += step;
	return ++k;
}

void update(int n, int l, int r){
	if (l == r){
		if (T[n].ct) found = 1;
		if (del) T[n].max = T[n].maxD = T[n].ct = X[pos] = 0, T[n].min = T[n].minD = oo;
		if (ins) T[n].max = T[n].min = val, T[n].ct = 1, X[pos] = 1;
		return;
	}
	if (pos <= mid) update(left, l, mid);
	else update(right, mid + 1, r);

	T[n].ct = T[left].ct + T[right].ct;
	if (T[left].ct && T[right].ct)
		T[n].max = max(T[right].max, T[left].max),
		T[n].min = min(T[left].min, T[right].min),
		T[n].maxD = max(max(T[left].maxD, T[right].maxD), T[n].max - T[n].min),
		T[n].minD = min(min(T[left].minD, T[right].minD), T[right].min - T[left].max);
	else if (!T[left].ct)
		T[n] = T[right];
	else T[n] = T[left];
}

int main()
{
	read();
	p = buf;

	while (*p != -3) B[++B[0]] = read_num(); // <--
	sort(B + 1, B + B[0]);

	for (int i = 1; i < B[0]; ++i)
		if (B[i] != B[i - 1] || i == 1)
			A[++A[0]] = B[i];
	for (LOG = 1; LOG <= A[0]; LOG <<= 1); // <--
	
	p = buf;
	while (*p != -3){
		string act = read_char();
		found = ins = del = 0;
		if (act == "I" || act == "S" || act == "C"){
			if (act == "I") ins = 1;
			if (act == "S") del = 1;
			val = read_num();
			pos = search(val);
			if ((act == "I" && !X[pos]) || (act == "S" && X[pos]))
				update(1, 1, A[0]);
			if (act == "S" && !found) fout << "-1\n";
			if (act == "C") found ? fout << "1\n" : fout << "0\n";
		}
		else if (act == "MAX") T[1].ct > 1 ? fout << T[1].maxD << "\n" : fout << "-1\n";
		else T[1].ct > 1 ? fout << T[1].minD << "\n" : fout << "-1\n";
	}

	fout.close();
	return 0;
}