Cod sursa(job #861572)

Utilizator toranagahVlad Badelita toranagah Data 21 ianuarie 2013 18:45:49
Problema Secventa 5 Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.92 kb
#include <cstdio>
#include <deque>
#include <fstream>
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
#define FORIT(it, v)for(typeof((v).begin()) it=(v).begin();it!=(v).end();++it)
const int MAX_N = (1 << 21) + 10;
typedef vector<pair<int, int> >::iterator vit;
typedef unsigned long long uint64;
typedef unsigned int uint32;
ifstream fin("secv5.in");
ofstream fout("secv5.out");
 
#define MaxChar 1000000
#define verf ((++CharB==MaxChar)?( fin.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);
}

const int MAX_HASH = 100100;
const int MOD = 100069;
struct Hash {
    vector<pair<int, int> > h[MAX_N];
    void erase(vit iter) {
        int pos = iter->first % MOD;
        h[pos].erase(iter);
    }
    void insert(pair<int, int> p) {
        int pos = p.first % MOD;
        h[pos].push_back(p);
    }
    vit find(int val) {
        int pos = val % MOD;
        FORIT (it, h[pos]) {
            if (it->first == val) return it;
        }
        return end();
    }
    vit end() {
        return h[MOD].end();
    }
    void clear() {
        for (int i = 0; i < MAX_N; ++i) {
            h[i].clear();
        }
    }
} hash;
  
uint64 compute_subsets(uint32* v, int n, int limit) {
    if (limit == 0) return 0;
    uint64 result = 0;
    deque<int> dq;
    // Hash hash;
    int numValues = 0;
    for (int i = 1; i <= n; ++i) {
        dq.push_back(v[i]);
        int valBack = dq.back();
        vit itBack = hash.find(valBack);
        if (itBack == hash.end()) {
            ++numValues;
            if (numValues > limit) {
                int valFront = dq.front();
                vit 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();
    }
    //hash.clear();
    return result;
}