Cod sursa(job #3293743)

Utilizator Andrei_GAndreiG Andrei_G Data 12 aprilie 2025 14:40:47
Problema Secventa 5 Scor 60
Compilator cpp-64 Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul I Marime 1.34 kb
#include <fstream>
#include <algorithm>
#include <string>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#define int long long
using namespace std;

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

map<int, int> M;
map<int, int> N;

int n, x, y, v[1000005], a = 0, b = 0;

signed main(){
    cin>>n>>x>>y;
    for (int i = 1; i <= n; i++){
        cin>>v[i];
    }
    x--;
    int st = 1, dr = 0, cnt = 0;
    while (dr != n){
        dr++;
        M[v[dr]]++;
        if (M[v[dr]] == 1){
            cnt++;
        }
        if (cnt <= x){
            a += dr - st + 1;
        }
        else{
            while (cnt > x){
                if (M[v[st]] == 1){
                    cnt--;
                }
                M[v[st]]--;
                st++;
            }
            a += dr - st + 1;
        }
    }
    st = 1;
    dr = 0;
    cnt = 0;
    while (dr != n){
        dr++;
        N[v[dr]]++;
        if (N[v[dr]] == 1){
            cnt++;
        }
        if (cnt <= y){
            b += dr - st + 1;
        }
        else{
            while (cnt > y){
                if (N[v[st]] == 1){
                    cnt--;
                }
                N[v[st]]--;
                st++;
            }
            b += dr - st + 1;
        }
    }
    cout<<b - a;
}