Cod sursa(job #2909642)

Utilizator vlad2009Vlad Tutunaru vlad2009 Data 14 iunie 2022 13:50:30
Problema Secventa 5 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <fstream>
#include <map>

using namespace std;

const int MAX_N = 1 << 20;
int a[MAX_N + 1];
map<int, int> mp;
int n, l, u;

long long Count(int k) {
    mp.clear();
    int j = 1, cnt = 0;
    long long answer = 0;
    for (int i = 1; i <= n; i++) {
        mp[a[i]]++;
        if (mp[a[i]] == 1) {
            cnt++;
        }
        while (j <= i && cnt > k) {
            if (mp[a[j]] == 1) {
                cnt--;
            }
            mp[a[j]]--;
            j++;
        }
        answer += i - j + 1;
    }
    return answer;
}

int main() {
    ifstream fin("secv5.in");
    ofstream fout("secv5.out");
    fin >> n >> l >> u;
    for (int i = 1; i <= n; i++) {
        fin >> a[i];
    }
    fout << Count(u) - Count(l - 1);
    return 0;
}