Cod sursa(job #1194511)

Utilizator WyvernFMI Stanescu Leonard Wyvern Data 3 iunie 2014 23:36:55
Problema Zeap Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Semestrul 2 Marime 2.88 kb
#include <cstdio>
#include <map>
#include <set>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
const char iname[] = "zeap.in";
const char oname[] = "zeap.out";
set<int>S;
map<int,int>M;
inline void read(char *p, int &n) {
    for (; *p < '0' || *p > '9'; ++ p) ;
    for (n = 0; '0' <= *p && *p <= '9'; ++ p)
        n = n * 10 + (*p - '0');
}
int main(void) {
    FILE *fi, *fo;
    char buffer[16];
    pair <set <int>::iterator, bool> ret;
    set <int>::iterator it, itup, itlow, last;
    int n;
    fi = fopen(iname, "r");
    fo = fopen(oname, "w");
    while (fgets(buffer, 16, fi)) {
        switch (buffer[0]) {
            case 'I':
                    read(buffer, n);
                    if ((ret = S.insert(n)).second == true) {
                        itup = itlow = ret.first;
                        itup ++, itlow --;
                        if (ret.first != S.begin() && itup != S.end()) {
                            int &tmp = M[*itup - *itlow];
                            if ((-- tmp) == 0)
                                M.erase(*itup - *itlow);
                        }
                        if (itup != S.end())
                            M[*itup - n] ++;
                        if (ret.first != S.begin())
                            M[n - *itlow] ++;
                    }
                break ;
            case 'S':
                    read(buffer, n);
                    if ((it = S.find(n)) != S.end()) {
                        itup = itlow = it;
                        itup ++, itlow --;
                        if (it != S.begin()) {
                            int &tmp = M[n - *itlow];
                            if ((-- tmp) == 0)
                                M.erase(n - *itlow);
                        }
                        if (itup != S.end()) {
                            int &tmp = M[*itup - n];
                            if ((-- tmp) == 0)
                                M.erase(*itup - n);
                        }
                        if (it != S.begin() && itup != S.end())
                            M[*itup - *itlow] ++;
                        S.erase(it);
                    }
                    else
                        fprintf(fo, "-1\n");
                break ;
            case 'C':
                    read(buffer, n);
                    fprintf(fo, "%d\n", (S.find(n) != S.end() ? 1 : 0));
                break ;
            case 'M':
                    if (S.size() > 1)
                        if (buffer[1] == 'A')
                            fprintf(fo, "%d\n", *(-- (last = S.end())) - *S.begin());
                        else
                            fprintf(fo, "%d\n", (*M.begin()).first);
                    else
                        fprintf(fo, "-1\n");
                break ;
        }
    }
    fcloseall();
    return 0;
}