Pagini recente » Cod sursa (job #861495) | Cod sursa (job #1231363) | Cod sursa (job #1237796) | Cod sursa (job #2364691) | Cod sursa (job #9567)
Cod sursa(job #9567)
#include <stdio.h>
#include <stdlib.h>
struct arb
{
int x,ap;
struct arb *st,*dr;
};
long int n,l,u,st1,st2,nrst1,nrst2,a[1050000];
long long int nr_secv;
struct arb *a1, *a2;
void add(struct arb *p, long int x)
{
struct arb *q,*tata;
q=p; tata=NULL;
while (q!=NULL && q->x!=x)
if (x<q->x) tata=q,q=q->st;
else tata=q,q=q->dr;
if (q!=NULL) { q->ap=1; return; }
q=(struct arb*) malloc(sizeof(struct arb));
q->x=x;
q->ap=1;
q->dr=NULL;
q->st=NULL;
if (x<tata->x) tata->st=q;
else tata->dr=q;
}
int find(struct arb *p, long int x)
{
struct arb *q;
q=p;
while (q!=NULL && q->x!=x)
if (x<q->x) q=q->st;
else q=q->dr;
if (q==NULL || q->ap==0) return 0;
(q->ap)++;
return 1;
}
int elim(struct arb *p, long int x)
{
struct arb *q;
q=p;
while (q->x!=x)
if (x<q->x) q=q->st;
else q=q->dr;
(q->ap)--;
if (q->ap==0) return 1;
return 0;
}
int elim2(struct arb *p, long int x)
{
struct arb *q;
q=p;
while (q->x!=x)
if (x<q->x) q=q->st;
else q=q->dr;
if (q->ap==1) return 1;
(q->ap)--;
return 0;
}
void elib(struct arb *p)
{
if (p->st!=NULL) {
elib(p->st);
free(p->st);
}
if (p->dr!=NULL) {
elib(p->dr);
free(p->dr);
}
}
int main()
{
freopen("secv5.in","r",stdin);
scanf("%d %d %d",&n,&l,&u);
scanf("%d",&a[0]);
st1=0; st2=0;
nrst1=1; nrst2=1;
a1=(struct arb*) malloc(sizeof(struct arb));
a1->x=a[0];
a1->ap=1;
a1->dr=NULL;
a1->st=NULL;
a2=(struct arb*) malloc(sizeof(struct arb));
a2->x=a[0];
a2->ap=1;
a2->dr=NULL;
a2->st=NULL;
nr_secv=0;
for (int i=1;i<n;i++)
{
scanf("%d",&a[i]);
if (!find(a1,a[i]))
{
add(a1,a[i]);
if (nrst1<u) nrst1++;
else {
while (!elim(a1,a[st1])) st1++;
st1++;
}
}
if (!find(a2,a[i]))
{
add(a2,a[i]);
if (nrst2<l) nrst2++;
else {
while (!elim(a2,a[st2])) st2++;
st2++;
}
}
while (!elim2(a2,a[st2])) st2++;
if (nrst2==l) nr_secv+=st2-st1+1;
}
elib(a1); free(a1);
elib(a2); free(a2);
freopen("secv5.out","w",stdout);
printf("%I64d\n",nr_secv);
fclose(stdout);
return 0;
}