Cod sursa(job #1218827)

Utilizator grayshadeLaurentiu Nicola grayshade Data 12 august 2014 17:10:03
Problema Secventa 5 Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <map>
#include <tr1/unordered_map>

using namespace std;

vector<unsigned int> v;

typedef map<unsigned int, int> my_hash;
//typedef std::tr1::unordered_map<unsigned int, int> my_hash;

unsigned long long count(int n, int limit)
{
    my_hash s;

    unsigned long long count = 0;
    for (int 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");

    int n, l, u;
    fi >> n >> l >> u;

    v.reserve(n);
    for (int i = 0; i < n; i++)
    {
        unsigned int x;
        fi >> x;
        v.push_back(x);
    }

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