Cod sursa(job #3186469)

Utilizator mihai234Mihai Sercaianu mihai234 Data 23 decembrie 2023 10:39:44
Problema Secventa 5 Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.71 kb
#include <fstream>
#include <unordered_map>
#define MAXN 1048576
using namespace std;
long long v[MAXN + 1];
int nrsecv(int n, int x ) {
    int i, j;
    unordered_map<long long, int> f;
    long long ans, cnt;
    i = j = cnt = ans = 0;
    while ( i < n ) {
        f[v[i]]++;
        cnt += (f[v[i]] == 1);
        i++;
        while ( cnt > x ) {
            f[v[j]]--;
            cnt -= (f[v[j]] == 0);
            j++;
        }
        ans += i - j;
    }
    return ans;
}
int main()
{
    ifstream fin( "secv5.in" );
    ofstream fout( "secv5.out" );
    int n, i, x, y;
    fin >> n >> x >> y;
    for ( i = 0; i < n; i++ ) 
        fin >> v[i];
    fout << nrsecv( n, y ) - nrsecv( n, x - 1 ) << '\n';
    return 0;
}