Cod sursa(job #770090)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 21 iulie 2012 22:19:05
Problema Zeap Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.84 kb
#include <cstdio>
#include <cstring>
#include <algorithm>

#define NMax 300005
#define Infinity 100000000

using namespace std;

struct Int
{
    int M, m, D;
};

struct Operation
{
    int Type, Time, X, NX;
};

Int IT[3*NMax];
Operation Op[NMax];
int N;

inline void Update (int K, int L, int R, int X, int V)
{
    int Mid=(L+R)/2;
    if (L==R)
    {
        IT[K].M=IT[K].m=X, IT[K].D=Infinity;
        if (V==0) IT[K].M=-Infinity, IT[K].m=Infinity;
        return;
    }
    if (X<=Mid) Update (2*K, L, Mid, X, V);
    else Update (2*K+1, Mid+1, R, X, V);
    IT[K].m=min (IT[2*K].m, IT[2*K+1].m);
    IT[K].M=max (IT[2*K].M, IT[2*K+1].M);
    IT[K].D=min (min (IT[2*K].D, IT[2*K+1].D), IT[2*K+1].m-IT[2*K].M);
}

inline int Query (int K, int L, int R, int X)
{
    int Mid=(L+R)/2;
    if (L==R) return (IT[K].M==IT[K].m);
    if (X<=Mid) return Query (2*K, L, Mid, X);
    else return Query (2*K+1, Mid+1, R, X);
}

inline void Build (int K, int L, int R)
{
    int Mid=(L+R)/2;
    IT[K].M=-Infinity, IT[K].m=Infinity, IT[K].D=Infinity;
    if (L==R) return;
    Build (2*K, L, Mid); Build (2*K+1, Mid+1, R);
}

inline bool CompareX (Operation A, Operation B)
{
    if (A.Type>2) return false;
    if (B.Type>2) return true;
    return A.X<B.X;
}

inline bool CompareT (Operation A, Operation B)
{
    return A.Time<B.Time;
}

void Normalize ()
{
    sort (Op+1, Op+N+1, CompareX);
    Op[1].NX=1;
    for (int i=2; i<=N and Op[i].Type<=2; ++i)
    {
        if (Op[i].X>Op[i-1].X) Op[i].NX=Op[i-1].NX+1;
        else Op[i].NX=Op[i-1].NX;
    }
    sort (Op+1, Op+N+1, CompareT);
}

inline bool ReadType (char Type[5])
{
    memset (Type, 0, sizeof (Type));
    if (scanf ("%s", Type)!=1) return false;
    return true;
}

void Read ()
{
    freopen ("zeap.in", "r", stdin);
    char Type[5];
    while (ReadType (Type))
    {
        ++N; Op[N].Time=N;
        if (Type[0]=='I') Op[N].Type=0, scanf ("%d\n", &Op[N].X);
        if (Type[0]=='S') Op[N].Type=1, scanf ("%d\n", &Op[N].X);
        if (Type[0]=='C') Op[N].Type=2, scanf ("%d\n", &Op[N].X);
        if (Type[0]=='M' and Type[1]=='A') Op[N].Type=3;
        if (Type[0]=='M' and Type[1]=='I') Op[N].Type=4;
    }
}

void Solve ()
{
    freopen ("zeap.out", "w", stdout);
    Build (1, 1, N);
    Normalize ();
    for (int i=1; i<=N; ++i)
    {
        if (Op[i].Type==0) Update (1, 1, N, Op[i].NX, Op[i].X);
        if (Op[i].Type==1)
        {
            if (Query (1, 1, N, Op[i].NX)==0) printf ("-1\n");
            else Update (1, 1, N, Op[i].NX, 0);
        }
        if (Op[i].Type==2) printf ("%d\n", Query (1, 1, N, Op[i].NX));
        if (Op[i].Type==3) printf ("%d\n", IT[1].M-IT[1].m);
        if (Op[i].Type==4) printf ("%d\n", IT[1].D);
    }
}

int mainn()
{
    Read ();
    Solve ();
    return 0;
}