Cod sursa(job #988942)

Utilizator ctlin04UAIC.VlasCatalin ctlin04 Data 24 august 2013 12:57:19
Problema Secventa 5 Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.6 kb
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
typedef unsigned int intt;
intt i,n,l,u,a[1050000],poz[1050000],aux[1050000],h[1050000],s;
bool ok;
string st;

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);
}

intt transforma(){
    intt x=0;
    while (st[s]!='\n') { x=x*10+intt(st[s])-48; ++s; }
    ++s;
    return(x);
}

int main(void) {
    ifstream fin("secv5.in");
    ofstream fout("secv5.out");
    fin>>n>>l>>u; getline(fin,st); getline(fin,st,char(EOF)); st+='\n';
    for (i=1; i<=n; ++i) { a[i]=transforma(); 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);
}