Cod sursa(job #1218833)

Utilizator grayshadeLaurentiu Nicola grayshade Data 12 august 2014 17:16:55
Problema Secventa 5 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <cstdint>
#include <fstream>
#include <vector>
#include <tr1/unordered_map>

using namespace std;
using namespace std::tr1;

unsigned long long count(const vector<uint32_t> &v, size_t limit)
{
    typedef unordered_map<uint32_t, int> my_hash;

    size_t n = v.size();

    my_hash s;
    uint64_t count = 0;
    for (size_t left = 0, right = 0; right < n; right++)
    {
        s[v[right]]++;

        while (s.size() > limit)
        {
            my_hash::iterator it = s.find(v[left++]);
            if (!--it->second)
                s.erase(it);
        }

        count += right - left + 1;
    }

    return count;
}

int main()
{
    ifstream fi("secv5.in");
    ofstream fo("secv5.out");

    size_t n, l, u;
    fi >> n >> l >> u;

    vector<uint32_t> v;
    v.reserve(n);
    for (size_t i = 0; i < n; i++)
    {
        uint32_t x;
        fi >> x;
        v.push_back(x);
    }

    fo << count(v, u) - count(v, l - 1) << '\n';
}