Pagini recente » Cod sursa (job #1743521) | Cod sursa (job #103825) | Cod sursa (job #49840) | Cod sursa (job #1305020) | Cod sursa (job #210)
Cod sursa(job #210)
#include <cstdio>
#include <set>
#include <map>
using namespace std;
const int BMAX = 16;
map <int, int> S;
multiset <int> Min;
inline int ABS(int x) { return x < 0 ? -x : x; }
inline int toint(char s[]) {
int rez = 0, i;
for (i = 0; isdigit(s[i]); ++i)
rez = rez * 10 + s[i] - '0';
return rez;
}
inline void sterg(map <int, int> :: iterator it) {
int a, b, c = 0;
a = it->first;
if (it->second == 1) ++c, it->second = 0;
++it;
if (it->second == -1) ++c, it->second = 0;
b = it->first;
while (c)
Min.erase( Min.find( ABS(a - b) ) ),
--c;
}
inline void leaga(map <int, int> :: iterator it) {
map <int, int> :: iterator j;
int a, b, c;
b = c = -1;
a = it->first;
if (it != S.begin()) {
j = it;
--j;
b = j->first;
}
j = it; ++j;
if (j != S.end())
c = j->first;
if (c == -1 && b == -1) {
it->second = 0;
return;
}
if (c == -1) {
it->second = -1;
Min.insert( ABS(a - b) );
return;
}
if (b == -1) {
it->second = 1;
Min.insert( ABS(a - c) );
return;
}
it->second = ABS(a - b) < ABS(a - c) ? -1 : 1;
Min.insert(it->second == -1 ? ABS(a - b) : ABS(a - c));
}
int main() {
FILE *fin = fopen("zeap.in", "rt");
FILE *fout = fopen("zeap.out", "wt");
char buf[BMAX];
map <int, int> :: iterator it;
int aux;
while (fgets(buf, BMAX, fin)) {
switch (buf[0]) {
case 'I':
aux = toint(buf + 2);
if (S.find(aux) == S.end()) {
it = S.lower_bound(aux);
if (it != S.end() && it != S.begin())
sterg(--it);
S[aux] = 0;
it = S.find(aux);
leaga(it);
}
break;
case 'S':
aux = toint(buf + 2);
if ((it = S.find(aux)) != S.end()) {
sterg(it);
if (it != S.begin())
sterg(--it),
++it;
S.erase(it);
if (S.size() >= 2) {
it = S.lower_bound(aux);
if (it != S.end() && it->second == 0)
leaga(it);
if (it != S.begin())
if (--it->second == 0)
leaga(it);
}
} else
fprintf(fout, "-1\n");
break;
case 'C':
aux = toint(buf + 2);
if (S.find(aux) != S.end())
fprintf(fout, "1\n");
else
fprintf(fout, "0\n");
break;
case 'M':
if (S.size() < 2)
fprintf(fout, "-1\n");
else if (buf[1] == 'A')
fprintf(fout, "%d\n", S.rbegin()->first - S.begin()->first);
else
fprintf(fout, "%d\n", *Min.begin());
break;
}
}
fclose(fin);
fclose(fout);
return 0;
}