Pagini recente » Cod sursa (job #446677) | Cod sursa (job #1639769) | Cod sursa (job #963544) | Cod sursa (job #2884576) | Cod sursa (job #988941)
Cod sursa(job #988941)
#include<fstream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef unsigned int intt;
intt i,n,l,u,a[1050000],poz[1050000],aux[1050000],h[1050000];
bool ok;
intt newvalue(intt x){
intt l,r,mid;
l=1; r=n;
while (l<=r) {
mid=(l+r)/2;
if (aux[mid]==x) return(poz[mid]);
else if (aux[mid]>x) r=mid-1;
else l=mid+1;
}
}
long long solve(intt lim) {
long long sol=0; intt nr=0,p=0;
for (intt i=1; i<=n; ++i){
while (nr<=lim&&p<=n) {
++p;
++h[a[p]];
if (h[a[p]]==1) ++nr;
}
h[a[p]]=0; --p; --nr; sol+=p-i+1;
--h[a[i]]; if (h[a[i]]==0) --nr;
}
return(sol);
}
int main(void) {
ifstream fin("secv5.in");
ofstream fout("secv5.out");
fin>>n>>l>>u;
for (i=1; i<=n; ++i) { fin>>a[i]; aux[i]=a[i]; }
//normalizare
sort(aux+1,aux+n+1);
intt nr=0;
for (i=1; i<=n; ++i)
if (aux[i]!=aux[i-1]) { ++nr; poz[i]=nr; }
else poz[i]=nr;
for (i=1; i<=n; ++i) a[i]=newvalue(a[i]);
//calculez cite segvente au maxim u elemente distincte si cite au l-1 elemente distincte
long long sol1, sol2;
sol1=solve(u); memset(h,0,sizeof(h));
sol2=solve(l-1);
fout<<sol1-sol2;
return(0);
}