Cod sursa(job #828269)

Utilizator oana_popfmi - pop oana oana_pop Data 3 decembrie 2012 15:57:33
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.49 kb
#include <cstdio>
#include <string>
#include <vector>

using namespace std;

#define mod 666013
#define max 1<<20



vector < int > a, b;
vector < pair < int, int > > Hash[666014];
int N,L,U,viz[max];



int search(int x)
{
    int cheie=x%mod;
    for(int i=0; i<Hash[cheie].size();++i){
       if(Hash[cheie][i].first==x) 
          return Hash[cheie][i].second;
    }
    return -1;
}

long long solve_subseq(int x)
{
     memset(viz,0,sizeof(viz));
     int nr=0,j=0,i=0;
     long long rez=0;
     
     for(;i<N;++i){
        if(!viz[b[i]])  nr++;
        viz[b[i]]++;
        for(;nr>x;j++)
        {
                      viz[b[j]]--;
                      if(!viz[b[j]]) nr--;
        }
        rez+=i-j+1;
     }
     return rez;
}

void read()
{
     scanf("%d %d %d",&N,&L,&U);
     int norm=0;
     for(int i=0; i<N; ++i)
     {
             int x;
             scanf("%u",&x);
             a.push_back(x);
             int rez=search(x);
             if(rez==-1)
             {
                        int cheie=x%mod;
                        Hash[cheie].push_back(make_pair(x,norm));
                        b.push_back(norm++);
             }
             else
             {
                 b.push_back(rez);
             }
     }
     
}
void write(){
 
    printf("%lld", solve_subseq(U)-solve_subseq(L-1));
 
}
int main()
{
    freopen("secv5.in","r",stdin);
    freopen("secv5.out","w",stdout);
    read();
    write();
}