Cod sursa(job #2755647)

Utilizator lahayonTester lahayon Data 27 mai 2021 22:12:03
Problema Secventa 5 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <fstream>
#include <iostream>
#include <unordered_map>
#include <vector>


using namespace std;

 vector<unsigned int> values;

long long sequences(int limit){

    long long result = 0;
    unordered_map<unsigned int, int> hashtable;
    unordered_map<unsigned int, int>::iterator it;

    int left = 0;

    for(int i = 0; i < values.size(); ++i){

        it = hashtable.find(values[i]);
        if(it != hashtable.end())
            ++it->second;
        else hashtable.insert(pair<unsigned int, int>(values[i], 1));

        while(hashtable.size() > limit){
            it = hashtable.find(values[left]);
            if(--it->second == 0) hashtable.erase(values[left]);
            ++left;
        }
         result += (i - left + 1);
    }
    return result;

}

int main()
{ 

    ifstream cin("secv5.in");
    ofstream cout("secv5.out");

    int N, L, U, x;
    cin >> N >> L >> U;
    
   


    for(int i = 0; i < N; ++i){
        cin >> x;
        values.push_back(x);
    }

    cout << sequences(U) - sequences(L - 1);

    cin.close();
    cout.close();

  return 0;
}