Cod sursa(job #1135972)

Utilizator narcis_vsGemene Narcis - Gabriel narcis_vs Data 8 martie 2014 17:11:02
Problema Zeap Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.36 kb
//rezolvare offline
#include <fstream>
#include <string>
#include <bitset>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <iostream>

using namespace std ;
const int NMAX = 300004;
const int INF = 2000000000;
int pos[NMAX];
struct Zeap{
    struct Node{
        int maxxdif, minndif, maxx, minn;
        Node(){
            maxx = maxxdif = 0;
            minn = minndif = INF;
        }
    };
    Node aint[4*NMAX];
    bitset < NMAX > is;
    int n;
    inline void Update(const int node,const int Left,const int Right,const int pos,const bool ok,const int value){
        if(Left == Right){
            aint[node].maxx = aint[node].maxxdif = 0;
            aint[node].minn = aint[node].minndif = INF;          
            if(ok)
                aint[node].maxx = aint[node].minn = value;
            return ;
        }
        int Middle = (Left + Right)/2;
        if(pos <= Middle)
            Update(2*node,Left,Middle,pos,ok,value);
        else
            Update(2*node+1,Middle+1,Right,pos,ok,value);
        aint[node].maxx = max(aint[2*node].maxx,aint[2*node+1].maxx);
        aint[node].minn = min(aint[2*node].minn,aint[2*node+1].minn);
        aint[node].minndif = min(aint[2*node].minndif,aint[2*node+1].minndif);
        if(aint[2*node+1].minn != INF && aint[2*node].maxx !=0)
            aint[node].minndif = min(aint[node].minndif,aint[2*node+1].minn - aint[2*node].maxx);
        if(aint[2*node+1].maxx != 0 && aint[2*node].minn != INF)
            aint[node].maxxdif = aint[2*node+1].maxx - aint[2*node].minn;
    }
    inline void Insert(const int x,const int value){
        if(is[pos[x]])
            return ;
        is[pos[x]] = 1;
        Update(1,1,n,pos[x],1,value);
    }
    inline bool Delete(const int x){
        if(!is[pos[x]])
            return 0;
        is[pos[x]] = 0;
        Update(1,1,n,pos[x],0,0);
        return 1;
    }
    inline bool Search(const int x){
        return is[pos[x]];
    }
    inline int MAX(){
        return (aint[1].maxxdif?aint[1].maxxdif:-1);
    }
    inline int MIN(){
        return (aint[1].minndif != INF?aint[1].minndif:-1);
    }
};
Zeap Z;
int m, n;
pair < int , int > op[NMAX], v[NMAX];
inline void Read(){
    freopen("zeap.in","r",stdin);
    freopen("zeap.out","w",stdout);
    char s[200];
    while(cin >> s){
        ++m;
        if(s[0]=='M' && s[1] =='I')
            op[m].first = 5; 
        else
            if(s[0] =='M' && s[1] == 'A')
                op[m].first = 4;
                 else
                 {
                     int x = 0;
                     if(s[0] == 'I')
                         op[m].first = 1;
                     else
                         if(s[0] == 'S')
                            op[m].first = 2;
                     else
                            op[m].first = 3;
                     cin >> s;
                     for(unsigned i = 0; s[i];++i)
                         x = x*10+s[i]-'0';
                     op[m].second = x;
                     ++n;
                     v[n] = make_pair(x,n);
                 }
    }
}

inline void Normalization(){
    sort(v+1,v+n+1);
    for(int i = 1;i <= n; ++i)
        if(v[i].first == v[i-1].first)
            pos[v[i].second] = pos[v[i-1].second];
        else
            pos[v[i].second] = pos[v[i-1].second] + 1;
}

inline bool cmp(const pair < int , int > a,const pair < int , int > b){
    return a.second < b.second;
}

inline void Solve(){ 
   int i,j;
   Z.n = pos[v[n].second]; 
   sort(v+1,v+n+1,cmp);
   for(i = 1,j = 0;i <= m; ++i){
       if(op[i].first == 1){
           ++j;
           Z.Insert(j,op[i].second);
       }
       else{
           if(op[i].first == 2){
               ++j;
               if(!Z.Delete(j))
                   printf("-1\n");
           }
           else{
               if(op[i].first == 3){
                   ++j;
                   printf("%d\n",Z.Search(j));
               }
               else
                   if(op[i].first == 4)
                       printf("%d\n",Z.MAX());
                   else
                       printf("%d\n",Z.MIN());
           }
       }
   }/*
   for(i = 1;i <= m; ++i)
        printf("%d %d\n",op[i].first,op[i].second);
   printf("\n");
   for(i = 1;i <= n; ++i)
       printf("%d ",v[i].first);
   printf("\n");
   for(i = 1;i <= n ;++i)
       printf("%d ",pos[i]);
   printf("\n%d\n",Z.n);*/
}
int main(){
    Read();
    Normalization();
    Solve();
    return 0;
}