Cod sursa(job #1566156)

Utilizator sulzandreiandrei sulzandrei Data 11 ianuarie 2016 20:22:24
Problema Secventa 5 Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <cmath>
using namespace std;
typedef unsigned long long int ull;
ifstream in("secv5.in");
ofstream out("secv5.out");
unordered_map<ull,ull> h;
const int MAX_SIZE = ( 1 << 20 )+1;
ull v[MAX_SIZE],n;
ull size(ull a  , ull b)
{
    return abs(a-b)+1;
}
ull distsec(ull nr)
{
    ull sol = 0 ,
    to_remove = 1;
    for(int i= 1 ; i <= n ; i++)
    {
        h[v[i]]++;
        while(h.size()> nr)
        {
            h[v[to_remove]]--;
            if(h[v[to_remove]]==0)
                h.erase(v[to_remove]);
            to_remove++;
        }
        sol += size(i,to_remove);          //updatam numarul de subsiruri
    }
    return sol;
}
int main()
{
    ull l,u;
    in >> n >> u >> l;
    for(int i = 1  ; i <= n ; i++)
        in >> v[i];
    ull first = distsec(l);
    h.clear();
    ull second = distsec(u-1);
    out<<first-second;
    return 0;
}