Cod sursa(job #1779207)

Utilizator andru47Stefanescu Andru andru47 Data 14 octombrie 2016 22:30:33
Problema Secventa 5 Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.71 kb
#include <bits/stdc++.h>
#define per pair<unsigned int,int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define MOD 666013
using namespace std;
int n,l,u;
unsigned int a[1048581];
vector <per> Hash[MOD+5];
inline void add(unsigned int x,int cnt)
{
    unsigned int mask = x % MOD;
    for (auto it = Hash[mask].begin(); it!=Hash[mask].end(); ++it)
    {
        per y = *it;
        if (y.f==x)
        {
            Hash[mask].erase(it);
            Hash[mask].pb(mp(x,y.s+cnt));
            return ;
        }
    }
    Hash[mask].pb(mp(x,1));
    return ;
}
inline bool find(unsigned int x)
{
    unsigned int mask = x % MOD;
    for (auto &it : Hash[mask])
        if (it.f==x&&it.s>0)return true;
    return false;
}
inline long long solve(int x)
{
    int dist = 0, p1 = 1;
    long long sol = 0LL;
    for (int i = 1; i<=n; ++i)
    {
        if (find(a[i])==false)
            ++dist;
        add(a[i],1);
        while(dist>x)
        {
            add(a[p1],-1);
            if (find(a[p1])==false)--dist;
            ++p1;
        }
        sol += 1ll*(i-p1+1);
    }
    for (int i = 0 ; i <= MOD; ++i)
        Hash[i].clear();
    return sol;
}
class InParser
{
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch()
    {
        ++sp;
        if (sp == 4096)
        {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume)
    {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (unsigned int &n)
    {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-')
        {
            n = 0;
            sgn = -1;
        }
        else
        {
            n = c - '0';
        }
        while (isdigit(c = read_ch()))
        {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }

    InParser& operator >> (int &n)
    {
        char c;
        n = 0;
        while (!isdigit(c = read_ch()) && c != '-');
        long long sgn = 1;
        if (c == '-')
        {
            n = 0;
            sgn = -1;
        }
        else
        {
            n = c - '0';
        }
        while (isdigit(c = read_ch()))
        {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
};

int main()

{
    InParser fin("secv5.in");
    freopen("secv5.out","w",stdout);
    fin >> n >> l >> u;
    for (int i = 1; i<=n; ++i)
        fin >> a[i];
    printf("%d\n",solve(u) - solve(l-1));
    return 0;
}