Cod sursa(job #3173233)

Utilizator RazvanVelcuVelcu Razvan RazvanVelcu Data 22 noiembrie 2023 09:12:17
Problema Secventa 5 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <iostream>
#include <unordered_map>
using namespace std;
int n;
long long v[1050000];
int f(int k)
{
    int ans=0;
    unordered_map<int, int> fr;
    int l=1, cnt=0;
    for(int r=1; r<=n; r++)
    {
	fr[v[r]]++;
        if(fr[v[r]]==1)
            cnt++;
            while(cnt>k)
            {
		fr[v[l]]--;
                if(fr[v[l]]==0)
                    cnt--;
                l++;
            }
        ans+=r-l+1;
    }
    return ans;
}
int main()
{
    int l, u;
    cin>>n>>l>>u;
    for(int i=1; i<=n; i++)
        cin>>v[i];
    cout<<f(u)-f(l-1);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

ifstream in("secv5.in");
ofstream out("scev5.out");

#define in cin
#define out cout

int n,l,u;
long long v[1050000];

int f(int max)
{
    int ans=0;
    unordered_map<int,int> fr;
    int lf=1,cnt=0;
    for(int r=1; r<=n; r++)
    {
        fr[v[r]]++;
        if(fr[v[r]]==1)
        {
            cnt++;
            while(cnt>max)
            {
                fr[v[l]]--;
                if(fr[v[l]]==0)
                    cnt--;
                lf++;
            }
        }
        ans+=r-lf+1;
    }
    return ans;
}

int main()
{

    cin>>n>>l>>u;
    for(int i=1; i<=n; i++)
        cin>>v[i];
    cout<<f(u)-f(l-1);
    return 0;
}