Cod sursa(job #861531)

Utilizator toranagahVlad Badelita toranagah Data 21 ianuarie 2013 17:58:44
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.33 kb
#include <cstdio>
#include <deque>
#include <fstream>
#include <iostream>
#include <tr1/unordered_map>
#include <map>
using namespace std;
using namespace std::tr1;
#define FORIT(it, v)for(typeof((v).begin()) it=(v).begin();it!=(v).end();++it)
const int MAX_N = (1 << 21) + 10;
typedef unordered_map<int, int>::iterator mit;
typedef unsigned long long uint64;
typedef unsigned int uint32;
ifstream fin("secv5.in");
ofstream fout("secv5.out");

#define MaxChar 1000000
#define verf ( (++CharB==MaxChar) ? ( cin.read(Buffer,MaxChar),CharB=0 ) : (1) )
 
long CharB=MaxChar-1;
char Buffer [ MaxChar ];
 
void cit ( uint32 &a ){
    bool ok=0;
    for ( ; !( (Buffer[ CharB ]>='0' && Buffer[ CharB ]<='9') || ( Buffer[ CharB ] == '-' ) ); verf )
        ;
    if ( Buffer[ CharB ] == '-' ){
        verf;
        ok=1;
    }
    for ( a=0; (Buffer[ CharB ]>='0' && Buffer[ CharB ]<='9'); a*=10,a+=( Buffer[ CharB ]-'0'), verf )
        ;
    if ( ok ){
        a=-a;
    }
}

uint32 N, L, U;
uint32 sir[MAX_N];
uint64 compute_subsets(uint32*, int, int);

int main() {
    verf;
    cit(N), cit(L), cit(U);
    //fin >> N >> L >> U;
    for (int i = 1; i <= N; ++i) {
        cit(sir[i]);
        //fin >> sir[i];
    }
    fout << compute_subsets(sir, N, U) - compute_subsets(sir, N, L - 1);
}
 
uint64 compute_subsets(uint32* v, int n, int limit) {
    if (limit == 0) return 0;
    uint64 result = 0;
    deque<int> dq;
    unordered_map<int, int> hash;
    int numValues = 0;
    for (int i = 1; i <= n; ++i) {
        dq.push_back(v[i]);
        int valBack = dq.back();
        mit itBack = hash.find(valBack);
        if (itBack == hash.end()) {
            ++numValues;
            if (numValues > limit) {
                int valFront = dq.front();
                mit itFront = hash.find(valFront);
                while (itFront->second > 1) {
                    --itFront->second;
                    dq.pop_front();
                    if (dq.front() != valFront) {
                        valFront = dq.front();
                        itFront = hash.find(valFront);
                    }
                }
                hash.erase(itFront);
                dq.pop_front();
                --numValues;
            }
            hash.insert(make_pair(valBack, 1));
        } else {
            ++itBack->second;
        }
        result += dq.size();
    }
    return result;
}