Cod sursa(job #912276)

Utilizator dicu_dariaDaria Dicu dicu_daria Data 12 martie 2013 11:14:35
Problema Secventa 5 Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.49 kb
#include <fstream>
#include <cstdio>
#include <cstring>
#include <vector>
#define f first
#define s second
#define MOD 666013
#define N 1050000
#define mp make_pair
#define pb push_back
#define DIM 10000

using namespace std;

vector<pair<int, int> > H[MOD+10];
unsigned int stk, L, i, j, aux[N], A[N], a[N], x, n, p, l, u, poz = 0;
unsigned long long sol;
bool ok;
char buff[DIM];

void create_Hash()
{
    for(i = 1; i <= n; i++)
    {
        x = a[i];
        p = x % MOD;
        for(ok = 1, j = 0; j < H[p].size(); j++)
            if(H[p][j].f == x)
            {
                ok = 0;
                break;
            }
        if(ok) H[p].pb(mp(x, 0));
    }
}
void clear_Hash()
{
    while(stk <= n)
        {
            x = a[stk];
            p = x % MOD;
            for(i = 0; i < H[p].size(); i++)
                if(H[p][i].f == x)
                {
                    H[p][i].s--;
                    break;
                }
            stk++;
        }
}

void get(unsigned int &numar)
{
     numar = 0;
     while (buff[poz] < '0' || buff[poz] > '9')
          if (++poz == DIM)
               fread(buff,1,DIM,stdin),poz=0;

     while ('0'<=buff[poz] && buff[poz]<='9')
     {
          numar = numar*10 + buff[poz] - '0';
          if (++poz == DIM)
               fread(buff,1,DIM,stdin),poz=0;
     }
}

void get_Hash(unsigned int lim)
{
    stk = 1; L = 0;
    for(j = 1; j <= n; j++)
    {
        x = a[j];
        p = x % MOD;
        for(i = 0; i < H[p].size(); i++)
            if(H[p][i].f == x)
            {
                if(!H[p][i].s) L++;
                H[p][i].s++;
                ok = 0;
                break;
            }
        while(L > lim)
        {
            x = a[stk];
            p = x % MOD;
            for(i = 0; i < H[p].size(); i++)
                if(H[p][i].f == x)
                {
                    H[p][i].s--;
                    if(!H[p][i].s) L--;
                    break;
                }
            stk++;
        }
        aux[j] = stk;
    }
}
int main()
{
    freopen("secv5.in", "r", stdin);
    ofstream fo("secv5.out");
    fread(buff, 1, DIM, stdin);
    get(n); get(l); get(u);
    for(i = 1; i <= n; i++) get(a[i]);
    create_Hash();
    get_Hash(u);
    clear_Hash();
    memcpy(A, aux, sizeof(aux));
    create_Hash();
    get_Hash(l-1);
    for(i = 1; i <= n; i++) sol += (long long)(aux[i] - A[i]);
    fo << sol << "\n";
    return 0;
}