Cod sursa(job #1727487)

Utilizator moise_alexandruMoise Alexandru moise_alexandru Data 10 iulie 2016 22:31:42
Problema Secventa 5 Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.13 kb
#include <cassert>
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
const int maxn = (1 << 20) + 5;
const int mod = 99991;
unsigned int v[maxn];
vector <pair <unsigned int, int>> hassh[mod + 5];
char T[35];
unsigned int n, l, u;
int poz = 0;

inline void citeste(unsigned int &numar)
{
     numar = 0;
     while (T[poz] < '0' || T[poz] > '9')
          if (++poz == 30)
               fread(T, 1, 30, stdin), poz=0;
     while ('0' <= T[poz] && T[poz] <= '9')
     {
          numar = numar * 10 + T[poz] - '0';
          if (++poz == 30)
               fread(T, 1, 30, stdin), poz=0;
     }
}

int search_poz(int p, int x)
{
    int sz = hassh[x].size();
    for(int i = 0; i < sz; i++)
        if(hassh[x][i].first == p)
            return i;
    return -1;
}

void sterge(int p)
{
    int x = p % mod;
    int ind = search_poz(p, x);
    assert(ind >= 0);
    assert(hassh[x].size() > ind);
    if(hassh[x][ind].second == 1)
    {
        swap(hassh[x][ind], hassh[x][hassh[x].size()-1]);
        hassh[x].pop_back();
    }
    else
        hassh[x][ind].second--;
}

void insert(int p)
{
    int x = p % mod;
    int ind = search_poz(p, x);
    if(ind == -1)
        hassh[x].push_back(make_pair(p, 1));
    else
        hassh[x][ind].second++;
}



long long count_substrings(int p)
{
    long long num = 0;
    int dim = 0;
    for(unsigned int st = 1, dr = 1; dr <= n; dr++)
    {
        if(search_poz(v[dr], v[dr] % mod) == -1)
            dim++;
        insert(v[dr]);
        while(dim > p)
        {
            sterge(v[st]);
            if(search_poz(v[st], v[st] % mod) == -1)
                dim--;
            st++;
        }
        num = num + dr - st;
    }
    return num;
}

int main()
{
    freopen("secv5.in", "r", stdin);
    freopen("secv5.out", "w", stdout);
    citeste(n);
    citeste(l);
    citeste(u);
    for(int i = 1; i <= n; i++)
        citeste(v[i]);
    long long aux = count_substrings(u);
    for(int i = 0; i < mod; i++)
        hassh[i].clear();
    printf("%lld\n", aux - count_substrings(l - 1));
    return 0;
}