Cod sursa(job #773016)

Utilizator danalex97Dan H Alexandru danalex97 Data 31 iulie 2012 18:33:08
Problema Zeap Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.13 kb
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;

const int Size=5000000;
const int N=300000;

int A[N*3+11];
int B[N*3+11];
bitset<N*3+11> O;
int Act;

vector< pair<int,int> > V[Size+11];

#define poz(a) ( a<0 ? -a : a )

ifstream F("zeap.in");
ofstream G("zeap.out");

void Ocupate(int Nod,int St,int Dr,int Pos,int Val) // Updateaza pozitiile ocupate
{
	if ( St==Dr )
	{
		O[Nod]=Val;
		return;
	}
	
	int Mid= (St+Dr) /2;
	if ( Pos<=Mid )
		Ocupate(2*Nod,St,Mid,Pos,Val);
	else
		Ocupate(2*Nod+1,Mid+1,Dr,Pos,Val);
	
	O[Nod]=min(O[2*Nod],O[2*Nod+1]);
}

int Query(int Nod,int St,int Dr) // Cauta cea mai de la stanga pozitie libera
{
	if ( St==Dr )
		return St;
	
	int Mid= (St+Dr) /2; 
	if ( !O[2*Nod] )
		return Query(2*Nod,St,Mid);
	return Query(2*Nod+1,Mid+1,Dr);
}

void Update(int Nod,int St,int Dr,int Pos,int Val)
{
	if ( St==Dr )
	{
		A[Nod]=Val;
		return;
	}
	
	int Mid= (St+Dr) /2;
	if ( Pos<=Mid )
		Update(2*Nod,St,Mid,Pos,Val);
	else
		Update(2*Nod+1,Mid+1,Dr,Pos,Val);
	
	A[Nod]=max(A[2*Nod],A[2*Nod+1]);
	B[Nod]=min( min(B[2*Nod],B[2*Nod+1]) , poz(A[2*Nod]-A[2*Nod+1]) );
}

int Seek(int x) // marecheaza elementul folosit
{
	for (int it=0;it<int(V[x%Size].size());++it )
		if ( V[x%Size][it].first ==x )
			return 1;
	return 0;
}

void Push(int x,int pos)
{
	V[x%Size].push_back( make_pair(x,pos) );
}

int Pop(int x)
{
	for (int it=0;it<int(V[x%Size].size());++it )
		if ( V[x%Size][it].first ==x )
		{
			swap(V[x%Size][it],V[x%Size].back());
			int Ret=V[x%Size].back().second;
			V[x%Size].pop_back();
			return Ret;
		}
	return -1;
}

void Insert(int x)
{
	if ( Seek(x) ) G<<"-1\n";
	
	int Pos=Query(1,1,N);
	Push( x,Pos );
	
	Ocupate( 1,1,N,Pos,1 );
	Update( 1,1,N,Pos,x );
}

void Erase(int x)
{
	if ( Seek(x) ) G<<"-1\n";
	
	int Pos=Pop( x );
	
	Ocupate( 1,1,N,Pos,0 );
	Update( 1,1,N,Pos,0 );
}

void Max()
{
	if ( Act>1 )
		G<<A[1]<<'\n';
	else
		G<<"-1\n";
}

void Min()
{
	if ( Act>1 )
		G<<B[1]<<'\n';
	else
		G<<"-1\n";
}

void Search(int x)
{
	G<<Seek(x)<<'\n';
}


int main()
{
}