Cod sursa(job #922856)

Utilizator alexalghisiAlghisi Alessandro Paolo alexalghisi Data 22 martie 2013 18:05:32
Problema Secventa 5 Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.42 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#include <cstring>

#define DN 1048580
#define KEY 33013
#define un unsigned
#define val first
#define normal second

using namespace std;

vector< pair<un int,int> > hash[KEY];
un int v[DN],ind=0;
int n,l,u,ap[DN];
long long nrl=0,nru=0;

int insert(un int x)
{
    int nivel=x%KEY;
    for(un int i=0;i<hash[nivel].size();++i)
        if(hash[nivel][i].val==x)
            return hash[nivel][i].normal;

    hash[nivel].push_back(make_pair(x,++ind));
    return ind;
}
long long gen(int lung)
{
    int st=1,dist=0;
    long long x=0;
    memset(ap,0,sizeof(ap));

    for(int i=1;i<=n;++i)
    {

        if(dist<lung || ap[v[i]])
        {
            ++ap[ v[i] ];
            if(ap[ v[i] ]==1)
                ++dist;
            x+=(i-st+1);
        }
        else
            {
                while(dist==lung)
                {
                    --ap[ v[st] ];
                    if(!ap[ v[st] ])
                        --dist;
                    ++st;
                }
                ++ap[ v[i] ];
                ++dist;

                x+=(i-st+1);
            }
    }
    return x;
}

int main()
{
    ifstream f("secv5.in");
    ofstream g("secv5.out");
    f>>n>>l>>u;
    --l;
    for(int i=1;i<=n;++i)
    {
        int x;
        f>>x;
        v[i]=insert(x);
    }

    int stu=1,stl=1;


    nru=gen(u);
    nrl=gen(l);

    g<<nru-nrl;

    return 0;
}