Cod sursa(job #3191043)

Utilizator TheodosiusOancea Teodor Stefan Theodosius Data 8 ianuarie 2024 18:31:10
Problema Secventa 5 Scor 0
Compilator c-64 Status done
Runda Arhiva de probleme Marime 2.8 kb
#include <stdio.h>
#include <stdlib.h>
struct Pair{
    int first,second;
};
struct Pair *make_pair(int a,int b){
    struct Pair *x=(struct Pair *)malloc(sizeof(struct Pair));
    x->first=a;
    x->second=b;
    return x;
};
struct Nod{
    struct Pair *val;
    struct Nod *dr,*st;
};
struct Tree{
    struct Nod *top;
};
void addtotree(struct Tree *t,int a,int val){
    if(!t->top){
        t->top=(struct Nod *)malloc(sizeof(struct Nod));
        t->top->val=make_pair(a,val);
        t->top->st=NULL;
        t->top->dr=NULL;
        return;
    }
    struct Nod *nod=t->top;
    while(nod){
        if(a>nod->val->first){
            if(nod->dr){
                nod=nod->dr;
            }
            else{
                nod->dr=(struct Nod *)malloc(sizeof(struct Nod));
                nod->dr->val=make_pair(a,val);
                nod->dr->dr=NULL;
                nod->dr->st=NULL;
                return;
            }
        }
        else if(a<nod->val->first){
            if(nod->st){
                nod=nod->st;
            }
            else{
                nod->st=(struct Nod *)malloc(sizeof(struct Nod));
                nod->st->val=make_pair(a,val);
                nod->st->dr=NULL;
                nod->st->st=NULL;
                return;
            }
        }
        else{
            nod->val->second=val;
            return;
        }
    }
}
struct Nod *findtotree(struct Tree *t,int a){
    struct Nod *nod=t->top;
    while(nod){
        if(a>nod->val->first) nod=nod->dr;
        else if(a<nod->val->first) nod=nod->st;
        else return nod;
    }
    return NULL;
};
struct Map{
    struct Tree **t;
    int size;
};
struct Map *createMap(int size){
    struct Map *m=(struct Map *)malloc(sizeof(struct Map));
    m->size=size;
    m->t=(struct Tree **)calloc(size,sizeof(struct Tree *));
    return m;
};
void add(struct Map *m,int a,int val){
    int hash=a%m->size;
    if(!m->t[hash]) m->t[hash]=(struct Tree *)malloc(sizeof(struct Tree)),m->t[hash]->top=NULL;
    addtotree(m->t[hash],a,val);
};
int n,*v;
int cnt(int x){
    struct Map *m=createMap(79);
    int dr=0,st=0,c=0,rasp=0;
    while(dr<n){
        struct Nod *nod;
        if(m->t[v[dr]%m->size])
        nod=findtotree(m->t[v[dr]%m->size],v[dr]);
        else nod=NULL;
        if(!nod) add(m,v[dr],1),c++;
        else nod->val->second++;
        while(c>x){
            struct Nod *s;
            if(m->t[v[st]%m->size])
            s=findtotree(m->t[v[st]%m->size],v[st]);
            else s=NULL;
            if(s->val->second==1) c--;
            s->val->second--;
            st++;
        }
        rasp+=(dr-st);
        dr++;
    }
    return rasp;
}
int main()
{
    int l,u;
    scanf("%d %d %d",&n,&l,&u);
    v=(int *)malloc(sizeof(int)*n);
    for(int i=0;i<n;i++) scanf("%d",&v[i]);
    printf("%d",cnt(u)-cnt(l-1));

    return 0;
}