Cod sursa(job #944312)

Utilizator claudiumihailClaudiu Mihail claudiumihail Data 28 aprilie 2013 04:23:11
Problema Secventa 5 Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.51 kb
#include <fstream>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <cstring>
#include <unordered_map>
     
#define MAXN ((1<<20) + 1)
#define HASH_SIZE 38993
     
using namespace std;
     
unsigned int vec[MAXN];
int minimumSpans[MAXN];
unordered_map<unsigned int, int> mapDistincts;
   
int main()
{
    int n, L, U;
    long long numSecv = 0;
    fstream fin("secv5.in" , fstream::in);
    fstream fout("secv5.out", fstream::out);
        
    fin.seekg(0, ios::end);
    int end = fin.tellg();
        
    char* buffer = static_cast<char*>(malloc(end));
    
    fin.seekg (0, ios::beg);
    fin >> n >> L >> U;
    //cout << n << " " << L << " " << U << endl;
        
    fin.read(buffer, end);
    //cout << buffer << endl;
    
    char* pCurrent = buffer;
    for (int i=1; i<=n; ++i)
    {
        //fin >> vec[i];
        pCurrent++;
        while (*pCurrent >= '0' && *pCurrent <= '9')
        {
            vec[i] = 10*vec[i] + (*pCurrent - 48);
            pCurrent++;
        }
          
        //cout << vec[i] << " ";
    }
    //cout << endl;
        
    //free(buffer);
    unsigned int distincts = 0;
    int span = 1;
           
    for (int i=1; i<=n; ++i)
    {
        mapDistincts[vec[i]]++;
        if (mapDistincts[vec[i]] == 1)
        {
            distincts++;
             
            while (distincts > L-1)
            {
                mapDistincts[vec[span]]--;
                if (mapDistincts[vec[span]] == 0)
                {
                    mapDistincts.erase(vec[span]);
                    distincts--;
                }
                       
                span++;
            }
        }
   
        minimumSpans[i] = span;
    }
         
    mapDistincts.clear();
      
    distincts = 0;
    span = 1;
           
    for (int i=1; i<=n; ++i)
    {
        mapDistincts[vec[i]]++;
        if (mapDistincts[vec[i]] == 1)
        {
            distincts++;
             
            while (distincts > U)
            {
                mapDistincts[vec[span]]--;
                if (mapDistincts[vec[span]] == 0)
                {
                    mapDistincts.erase(vec[span]);
                    distincts--;
                }
                       
                span++;
            }
        }
 
        numSecv += (minimumSpans[i] - span);
    }
      
    //mapDistincts.clear();
           
    fout << numSecv << "\n";
           
    return 0;
}