Cod sursa(job #2797453)

Utilizator Vlad_AnicaAnica-Popa Vlad-Ioan Vlad_Anica Data 9 noiembrie 2021 22:01:05
Problema Secventa 5 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.21 kb
#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;
const int HMAX = 1000003, NMAX = 10000000;

struct pack
{
    unsigned int val;
    int fr;
};
struct coord
{
    int hv, ind;
};
vector<pack> ht[HMAX];
unsigned int arr[NMAX];

inline void reset();
inline int getHash(unsigned int nr);
inline coord getPos(unsigned int nr);
inline void insrt(unsigned int nr);
inline int getFr(unsigned int nr);
inline void changeFr(unsigned int nr, int add);
inline long long sol(int upBnd, int n);


int main()
{
    int n, l, u, i;
    long long sum;
    FILE *fin = fopen("secv5.in", "r");
    fscanf(fin, "%d%d%d", &n, &l, &u);

    for (i = 0; i <= n; i++)
    {
        fscanf(fin, "%u", &arr[i]);
        insrt(arr[i]);
    }

    sum = sol(u + 1, n) - sol(l, n);
    FILE *fout = fopen("secv5.out", "w");
    fprintf(fout, "%lld", sum);
    fclose(fout);
    return 0;
}

inline void reset()
{
    for (int i = 0; i < HMAX; i++)
    {
        int len = ht[i].size();
        for (int j = 0; j < len; j++)
            ht[i][j].fr = 0;
    }
}
inline int getHash(unsigned int nr)
{
    return nr % HMAX;
}
inline coord getPos(unsigned int nr)
{
    int hv = getHash(nr);
    int len = ht[hv].size(), ind = 0;
    while (ind < len && ht[hv][ind].val !=nr)
        ind++;
    if (ind == len)
        ind = -1;
    return {hv, ind};

}
inline void insrt(unsigned int nr)
{
    coord pos = getPos(nr);
    if (pos.ind == -1)
        ht[pos.hv].push_back({nr, 0});
}
inline int getFr(unsigned int nr)
{
    coord pos = getPos(nr);
    return ht[pos.hv][pos.ind].fr;
}
inline void changeFr(unsigned int nr, int add)
{
    coord pos = getPos(nr);
    ht[pos.hv][pos.ind].fr += add;
}
inline long long sol(int upBnd, int n)
{
    reset();
    int cnt = 0, ind1, ind2 = 0;
    long long sum = 0;
    for (ind1 = 0; ind1 < n; ind1++)
    {
        changeFr(arr[ind1], 1);
        if (getFr(arr[ind1]) == 1)
            cnt++;
        while (cnt >= upBnd)
        {
            changeFr(arr[ind2], -1);
            if (getFr(arr[ind2]) == 0)
                cnt--;
            ind2++;
        }
        sum += ind1 - ind2 + 1;
    }
    return sum;
}