Cod sursa(job #1706045)

Utilizator PopoviciRobertPopovici Robert PopoviciRobert Data 21 mai 2016 13:45:45
Problema Secventa 5 Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.23 kb
#include <cstdio>
#include <algorithm>
#define MAXN (1<<20)
#define MAXBUF (1<<20)
FILE*fi,*fout;
using namespace std;
char buf[MAXBUF];
int vf1[MAXN+1],vf2[MAXN+1],vec[MAXN],pos=MAXBUF;
struct numar{
    int poz;
    long long val;
};
numar v[MAXN];
inline char nextch(){
   if(pos==MAXBUF){
       fread(buf,1,MAXBUF,fi);
       pos=0;
   }
   return buf[pos++];
}
inline long long getnr(){
   char a=nextch();
   while(a<'0'||a>'9')
      a=nextch();
   long long nr=0;
   while(a>='0'&&a<='9'){
      nr=nr*10+a-'0';
      a=nextch();
   }
   return nr;
}
bool cmp(numar a,numar b){
    return (a.val<b.val);
}
int main(){
    int n,l,u,i,j,nr,b1,e1,b2,e2,con1,con2;
    long long nrsub,aux;
    fi=fopen("secv5.in" ,"r");
    fout=fopen("secv5.out" ,"w");
    n=getnr();
    l=getnr();
    u=getnr();
    for(i=0;i<n;i++){
        v[i].val=getnr();
        v[i].poz=i;
    }
    sort(v,v+n,cmp);
    i=0;
    nr=1;
    while(i<n){
        j=i;
        aux=v[i].val;
        while(j<n&&v[j].val==aux){
           vec[v[j].poz]=nr;
           j++;
        }
        nr++;
        i=j;
    }
    e1=con1=0;
    while(e1<n&&con1<l){
       if(vf1[vec[e1]]==0)
          con1++;
       vf1[vec[e1]]++;
       e1++;
    }
    e2=con2=0;
    while(e2<n&&con2<=u){
       if(vf2[vec[e2]]==0)
          con2++;
       vf2[vec[e2]]++;
       e2++;
    }
    if(con2>u&&vf2[e2-1]==1){
       con2--;
       vf2[e2-1]--;
       e2--;
    }
    b1=b2=0;
    nrsub=e2-e1+1;
    while(b1<n||b2<n){
        vf1[vec[b1]]--;
        if(vf1[vec[b1]]==0)
           con1--;
        b1++;
        vf2[vec[b2]]--;
        if(vf2[vec[b2]]==0)
           con2--;
        b2++;
        while(e1<n&&con1<l){
           if(vf1[vec[e1]]==0)
             con1++;
           vf1[vec[e1]]++;
           e1++;
        }
        while(e2<n&&con2<=u){
           if(vf2[vec[e2]]==0)
             con2++;
           vf2[vec[e2]]++;
           e2++;
        }
        if(con2>u&&vf2[e2-1]==1){
          con2--;
          vf2[e2-1]--;
          e2--;
        }
        if(con1>=l)
          nrsub=nrsub+(e2-e1+1);
    }
    fprintf(fout,"%lld" ,nrsub);
    fclose(fi);
    fclose(fout);
    return 0;
}