Cod sursa(job #2683257)

Utilizator loraclorac lorac lorac Data 10 decembrie 2020 18:59:28
Problema Secventa 5 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.53 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in("secv5.in");
ofstream out("secv5.out");
typedef long long ll;
const int lim=(1<<20)+5;
unordered_map<int,int> f;
int v[lim];
int main()
{
    int n,l,u;
    in>>n>>l>>u;
    for(int i=1;i<=n;++i)
        in>>v[i];
    ll ans1=0,ans2=0;
    ll st=1,dr=1;
    int cnt;
    l--;
    if(l>0)
    {
        st=dr=cnt=1;
        ans1=1;
        f[v[1]]++;
        while(dr<n)
        {
            ++dr;
            f[v[dr]]++;
            if(f[v[dr]]==1) ++cnt;
            if(cnt<=l) ans1+=1LL*(dr-st+1);
            else
            {
                while(cnt>l)
                {
                    f[v[st]]--;
                    if(f[v[st]]==0) --cnt,f.erase(f.find(v[st]));
                    ++st;
                }
                ans1+=1LL*(dr-st+1);
            }
        }
        ///ans1+=((dr-st)*(dr-st+1))/2;
    }
    for(int i=st;i<=dr;++i)
    if(f.find(v[i])!=f.end())
        f.erase(f.find(v[i]));
    st=dr=cnt=1;
    ans2=1;
    f[v[1]]++;
    while(dr<n)
    {
        ++dr;
        f[v[dr]]++;
        if(f[v[dr]]==1) ++cnt;
        if(cnt<=u) ans2+=1LL*(dr-st+1);
        else
        {
            while(cnt>u)
            {
                f[v[st]]--;
                if(f[v[st]]==0) --cnt,f.erase(f.find(v[st]));
                ++st;
            }
            ans2+=1LL*(dr-st+1);
        }
    }
    ///ans2+=((dr-st)*(dr-st+1))/2;
    ///cout<<ans1<<' '<<ans2<<'\n';
    out<<ans2-ans1<<'\n';
    return 0;
}