Pagini recente » 08022000 | Cod sursa (job #449009) | Rating Rusu Daniela (Donut007) | Cod sursa (job #470731) | Cod sursa (job #2097688)
#include <bits/stdc++.h>
#define BUF_SIZE 1 << 14
int end = 0, cnt;
char buf[BUF_SIZE];
int pbuf=BUF_SIZE;
FILE*fi,*fo;
inline char nextch(){
if(pbuf == cnt && cnt < BUF_SIZE) return EOF;
if(pbuf == BUF_SIZE){
cnt = fread(buf, 1, BUF_SIZE, fi);
pbuf = 0;
}
return buf[pbuf++];
}
inline int nextnum(){
int a = 0;
char c = nextch();
while(!isdigit(c))
c = nextch();
while(isdigit(c)){
a = a * 10 + c - '0';
c = nextch();
}
return a;
}
std::set <int> mySet;
int count;
std::multiset <int> diff;
int main(){
fi = fopen("zeap.in","r");
fo = fopen("zeap.out","w");
char c = nextch();
while(c == 'I' || c == 'S' || c == 'C' || c == 'M'){
if(c == 'I'){
int x = nextnum();
auto it = mySet.find(x);
if(it == mySet.end()){
if(count >= 1){
if(x < *mySet.begin())
diff.insert(*mySet.begin() - x);
else if(x > *mySet.rbegin())
diff.insert(x - *mySet.rbegin());
else{
auto it1 = mySet.lower_bound(x), it2 = mySet.upper_bound(x);
it1--;
diff.erase(diff.find(*it2 - *it1));
diff.insert(x - *it1);
diff.insert(*it2 - x);
}
}
count++;
mySet.insert(x);
}
}
else if(c == 'S'){
int x = nextnum();
auto it = mySet.find(x);
if(it != mySet.end()){
if(count >= 2){
if(x == *mySet.begin()){
auto it = mySet.begin(); it++;
diff.erase(diff.find(*it - x));
}
else if(x == *mySet.rbegin()){
auto it = mySet.rbegin(); it--;
diff.erase(diff.find(x - *it));
}
else{
auto it1 = mySet.lower_bound(x), it2 = mySet.upper_bound(x);
it1--;
diff.insert(*it2 - *it1);
diff.erase(diff.find(x - *it1));
diff.erase(diff.find(*it2 - x));
}
}
count--;
mySet.erase(it);
}
else
fprintf(fo,"-1\n");
}
else if(c == 'C'){
int x = nextnum();
auto it = mySet.find(x);
if(it != mySet.end())
fprintf(fo,"1\n");
else
fprintf(fo,"0\n");
}
else{
c = nextch();
if(count < 2)
fprintf(fo,"-1\n");
else if(c == 'A')
fprintf(fo,"%d\n", *mySet.rbegin() - *mySet.begin());
else
fprintf(fo,"%d\n", *diff.begin());
nextch();
nextch();
}
c = nextch();
}
fclose(fi);
fclose(fo);
return 0;
}