Cod sursa(job #2735513)

Utilizator MocalinnoMoca Andrei Catalin Mocalinno Data 2 aprilie 2021 15:00:31
Problema Secventa 5 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.73 kb
#include <bits/stdc++.h>
using namespace std;
void DAU(const string& task = "") {
    if (!task.empty())
        freopen((task + ".in").c_str(), "r", stdin),
        freopen((task + ".out").c_str(), "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
void PLEC() {
    exit(0);
}
#define int unsigned int
class Zaharel {
public:
    Zaharel() {}
    Zaharel(const int& _n) :
        n(_n), ind(0), pos(0), v(_n + 1), vec(_n + 1) {}
    inline void Insert(const int& val) {
        v[++pos] = {val, pos};
    }
    inline long long Query(const int& st, const int& dr) {
        return Query(dr) - Query(st - 1);
    }
    inline void Compute() {
        sort(v.begin(), v.end());
        for (int i = 1; i <= n; ++i) {
            if (v[i].first != v[i - 1].first)
                ++ind;
            vec[v[i].second] = ind;
        }
    }
private:
    inline long long Query(const int& x) {
        freq = vector<int>(ind + 1);
        int i = 1, curr = 0;
        long long res = 0;
        for (int j = 1; j <= n; ++j) {
            ++freq[vec[j]];
            if (freq[vec[j]] == 1)
                ++curr;
            while (curr > x) {
                --freq[vec[i]];
                if (!freq[vec[i]])
                    --curr;
                ++i;
            }
            res += j - i + 1;
        }
        return res;
    }
    int n, ind, pos;
    vector<pair<int, int>> v;
    vector<int> vec, freq;
};
int n, l, r, x;
signed main() {
    DAU("secv5");
    cin >> n >> l >> r;
    Zaharel zaharel(n);
    for (int i = 1; i <= n; ++i) {
        cin >> x;
        zaharel.Insert(x);
    }
    zaharel.Compute();
    cout << zaharel.Query(l, r);
    PLEC();
}