Cod sursa(job #1295810)

Utilizator andreiiiiPopa Andrei andreiiii Data 20 decembrie 2014 11:20:19
Problema Secventa 5 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <algorithm>
#include <fstream>

using namespace std;

typedef long long i64;
const int Nmax = (1 << 20) + 10;

unsigned int A[Nmax];
int N, L, U;
i64 Ans = 0;

void Read() {
    ifstream fin("secv5.in");
    fin >> N >> L >> U;
    for (int i = 1; i <= N; ++i)
        fin >> A[i];
    fin.close();
}

void Norm() {
    vector<pair<unsigned int, int>> B(N);
    for (int i = 0; i < N; ++i)
        B[i] = {A[i + 1], i + 1};
    sort(B.begin(), B.end());
    for (int i = 0, k = 1; i < N; ++i) {
        if (i > 0 && B[i].first != B[i - 1].first) ++k;
        A[B[i].second] = k;
    }
}

int CountL[Nmax], CountR[Nmax];
void Solve() {
    int left = 1, right = 1;
    int cnt1 = 0, cnt2 = 0;
    for (int i = 1; i <= N; ++i) {
        CountL[A[i]]++;
        if (CountL[A[i]] == 1) ++cnt1;
        CountR[A[i]]++;
        if (CountR[A[i]] == 1) ++cnt2;

        while (cnt1 > U) {
            CountL[A[left]]--;
            if (CountL[A[left]] == 0) --cnt1;
            ++left;
        }

        while (right <= i && cnt2 - (CountR[A[right]] == 1) >= L) {
            CountR[A[right]]--;
            if (CountR[A[right]] == 0) --cnt2;
            ++right;
        }

        if (cnt1 >= L)
            Ans += right - left + 1;
    }
}

void Write() {
    ofstream fout("secv5.out");
    fout << Ans << '\n';
    fout.close();
}

int main()
{
    Read();
    Norm();
    Solve();
    Write();
}