Cod sursa(job #791463)

Utilizator scipianusFMI Ciprian Olariu scipianus Data 24 septembrie 2012 11:56:32
Problema Secventa 5 Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.26 kb
#include<fstream>
#include<algorithm>
#include<cstring>
using namespace std;
int n,L,U;
struct Element{unsigned int val;int poz;};
Element A[1050000];
int v[1050000],viz[1050000];

inline void Citire()
{
	int i,ns,poz;
	unsigned int aux;
	char s[20];
	ifstream fin("secv5.in");
	fin>>n>>L>>U;
	fin.getline(s,20);
	for(i=1;i<=n;i++)
	{
		fin.getline(s,20);
		ns=strlen(s);
		poz=0;
		aux=0;
		while(poz<ns)
			aux=aux*10+s[poz++]-'0';
		A[i].val=aux;
		A[i].poz=i;
	}
	fin.close();
}

inline bool Sortare(Element A,Element B)
{
	return A.val<B.val;
}

inline void Normalizare()
{
	int i,norm=0;
	sort(A+1,A+n+1,Sortare);
	for(i=1;i<=n;i++)
	{
		if(A[i].val==A[i-1].val)
			v[A[i].poz]=norm;
		else
			v[A[i].poz]=++norm;
	}
}

inline long long Nr(int size)
{
	long long sol=0;
	int i,st=1,dr=1,now=0;
	while(dr<=n)
	{
		if(viz[v[dr]]==0)
			now++;
		viz[v[dr]]++;
		while(now>size)
		{
			viz[v[st]]--;
			if(viz[v[st]]==0)
				now--;
			st++;
		}
		sol+=(long long)(dr-st+1);
		dr++;
	}
	for(i=1;i<=n;i++)
		viz[i]=0;
	return sol;
}

inline void Afisare()
{
	ofstream fout("secv5.out");
	fout<<(Nr(U)-Nr(L-1))<<"\n";
	fout.close();
}

int main()
{
	Citire();
	Normalizare();
	Afisare();
	return 0;
}