Cod sursa(job #1762297)

Utilizator BarbumateiBarbu Matei Barbumatei Data 23 septembrie 2016 14:16:04
Problema Secventa 5 Scor 10
Compilator c Status done
Runda Arhiva de probleme Marime 1.84 kb
#include <stdio.h>
#include <stdlib.h>
#define MOD 2093570
#define MAXN (1<<20)
#define h(x) (x%MOD)
unsigned int v[MAXN+1], val[2*MAXN+1];
int lista[2][MOD+1], next[2*MAXN+1], f[2*MAXN+1], m;
long long rasp;

void insert(unsigned int x, int ok){
  int hash=h(x);
  val[++m]=x;
  f[m]=1;
  next[m]=lista[ok][hash];
  lista[ok][hash]=m;
}

int cauta(int x, int ok){
  int poz, hash;
  hash=h(x);
  poz=lista[ok][hash];
  while(poz && val[poz]!=x)
    poz=next[poz];
  return poz;
}

void deleteme(unsigned int x, int ok, int *point){
  int poz, hash;
  hash=h(x);
  poz=lista[ok][hash];
  if(val[poz]==x){
    f[poz]--;
    if(!f[poz]){
      lista[ok][hash]=next[poz];
      (*point)--;
    }
  }
  else{
    while(next[poz] && val[next[poz]]!=x)
      poz=next[poz];
    if(val[next[poz]]==x){
      f[next[poz]]--;
      if(!f[next[poz]]){
        (*point)--;
        next[poz]=next[next[poz]];
      }
    }
  }
}

int main(){
  int n, l, u, difl, difu, poz1, poz2, i, p;
  FILE *fin, *fout;
  fin=fopen("secv5.in", "r");
  fout=fopen("secv5.out", "w");
  fscanf(fin, "%d%d%d", &n, &l, &u);
  for(i=1; i<=n; i++)
    fscanf(fin, "%u", &v[i]);
  difl=difu=poz1=poz2=0;
  for(i=1; i<=n; i++){
    while(difl<l && poz1<n){
      poz1++;
      p=cauta(v[poz1], 0);
      if(!p){
        insert(v[poz1], 0);
        difl++;
      }
      else f[p]++;
    }
    while(difu<u && poz2<n){
      poz2++;
      p=cauta(v[poz2], 1);
      if(!p){
        insert(v[poz2], 1);
        difu++;
      }
      else f[p]++;
    }
    while( difu==u && (p=cauta(v[poz2+1], 1))>0 && poz2<n){
      poz2++;
      f[p]++;
    }
    if(difl==l)
      rasp+=poz2-poz1+1;
    deleteme(v[i], 0, &difl);
    deleteme(v[i], 1, &difu);
  }
  fprintf(fout, "%lld\n", rasp);
  fclose(fin);
  fclose(fout);
    return 0;
}