Cod sursa(job #656010)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 3 ianuarie 2012 19:37:15
Problema Secventa 5 Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.73 kb
#include <fstream>
#include <vector>

#define NMax 1050000
#define U 666013
#define x first
#define n second

using namespace std;

vector < pair <unsigned, int> > H[U];
int N, S, NDistinct, L[2], Limit[2][NMax];
unsigned V[NMax];

inline void Insert (unsigned X)
{
    int Key=X%U;
    for (unsigned i=0; i<H[Key].size (); ++i)
    {
        if (H[Key][i].x==X)
        {
            ++H[Key][i].n;
            return;
        }
    }
    ++NDistinct;
    H[Key].push_back (make_pair (X, 1));
}

inline void Delete (unsigned X)
{
    int Key=X%U;
    for (vector < pair <unsigned, int> > :: iterator i=H[Key].begin (); i!=H[Key].end (); ++i)
    {
        if ((*i).x==X)
        {
            --(*i).n;
            if ((*i).n==0)
            {
                --NDistinct;
                H[Key].erase (i);
            }
            return;
        }
    }
}

void Read ()
{
    ifstream fin ("secv5.in");
    fin >> N >> L[0] >> L[1];
    --L[0];
    for (int i=1; i<=N; ++i)
    {
        fin >> V[i];
    }
}

void Print ()
{
    ofstream fout ("secv5.out");
    fout << S << "\n";
}

void SolveL (int K)
{
    Limit[K][0]=1;
    for (int i=1; i<=N; ++i)
    {
        Limit[K][i]=Limit[K][i-1];
        Insert (V[i]);
        while (NDistinct>L[K])
        {
            Delete (V[Limit[K][i]]);
            ++Limit[K][i];
        }
        if (K==0)
        {
            S-=(i-Limit[K][i]+1);
        }
        else
        {
            S+=(i-Limit[K][i]+1);
        }
    }
    int Left=Limit[K][N];
    while (Left<=N)
    {
        Delete (V[Left]);
        ++Left;
    }
}

int main()
{
    Read ();
    SolveL (0);
    SolveL (1);
    Print ();
    return 0;
}