Cod sursa(job #1096265)

Utilizator dariusdariusMarian Darius dariusdarius Data 1 februarie 2014 19:32:52
Problema Secventa 5 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.11 kb
#include <algorithm>
#include <fstream>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
const int MAX_N = (1 << 20) + 5;
const int MAX_BUFFER_SIZE = 1 << 15;

class HashTable {
    public:
        HashTable() {}
        void clear() {
            for(int i = 0; i < modulo; ++ i) {
                hash[i].clear();
            }
            count = 0;
        }
        inline int size() {
            return count;
        }
        inline void erase(unsigned int x) {
            int h = x % modulo;
            for(vector< pair<unsigned int, unsigned int> > :: iterator it = hash[h].begin(); it != hash[h].end(); ++ it) {
                if(it->first == x) {
                    -- it->second;
                    if(it->second == 0) {
                        -- count;
                        swap(*it, hash[h].back());
                        hash[h].pop_back();
                    }
                    return;
                }
            }
        }
        inline void insert(unsigned int x) {
            int h = x % modulo;
            for(vector< pair<unsigned int, unsigned int> > :: iterator it = hash[h].begin(); it != hash[h].end(); ++ it) {
                if(it->first == x) {
                    ++ it->second;
                    return;
                }
            }
            hash[h].push_back(make_pair(x, 1));
            ++ count;
        }
    private:
        static const int modulo = 666013;
        int count;
        vector< pair<unsigned int, unsigned int> > hash[modulo];
};
class InputReader {
public:
    InputReader(const char *in_file) {
        file = fopen(in_file, "r");
        size = MAX_BUFFER_SIZE;
        cursor = 0;
        fread(buffer, size, 1, file);
    }
    inline InputReader &operator>>(unsigned int &number) {
        number = 0;
        while('0' > buffer[cursor] || buffer[cursor] > '9') {
            ++ cursor;
            if(cursor == size) {
                cursor = 0;
                fread(buffer, size, 1, file);
            }
        }
        while('0' <= buffer[cursor] && buffer[cursor] <= '9') {
            number = number * 10 + buffer[cursor ++] - '0';
            if(cursor == size) {
                cursor = 0;
                fread(buffer, size, 1, file);
            }
        }
        return *this;
    }
private:
    FILE *file;
    int cursor, size;
    char buffer[MAX_BUFFER_SIZE];
};

unsigned int a[MAX_N];
HashTable current_hash;

long long solve(int n, int upper) {
    int p = 1;long long count = 0;
    current_hash.clear();
    for(int u = 1; u <= n; ++ u) {
        current_hash.insert(a[u]);
        while(current_hash.size() > upper) {
            current_hash.erase(a[p]);
            ++ p;
        }
        count += (u - p + 1);
    }
    cout << "Solved with upper " << upper << "\n";
    return count;
}

int main() {
    InputReader fin("secv5.in");
    ofstream fout("secv5.out");
    unsigned int n, lower, upper;
    fin >> n >> lower >> upper;
    for(int i = 1; i <= n; ++ i) {
        fin >> a[i];
    }
    fout << solve(static_cast<int>(n), static_cast<int>(upper)) - solve(static_cast<int>(n), static_cast<int>(lower - 1)) << "\n";
    return 0;
}