Cod sursa(job #1717548)

Utilizator oldatlantianSerban Cercelescu oldatlantian Data 15 iunie 2016 00:59:45
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.56 kb
#include <bits/stdc++.h>
using namespace std;
typedef long long i64;
typedef unsigned  i32;

const int NMAX = (1<<20)+5,
          MOD  = 666013;

class furt_reader {
private:
    FILE *input_file;
    static const int SIZE = 1 << 17;
    char buffer[SIZE]; int cursor;

    inline void advance() {
        if(++cursor==SIZE) {
            cursor = 0;
            fread(buffer, SIZE, 1, input_file);
        }
    }

    inline char current() {
        return buffer[cursor];
    }
public:
    furt_reader(const char *file_name) {
        cursor     = 0;
        input_file = fopen(file_name, "r");
        fread(buffer, SIZE, 1, input_file);
    }
    furt_reader(FILE *file) {
        cursor     = 0;
        input_file = file;
        fread(buffer, SIZE, 1, input_file);
    }

    furt_reader &operator >>(i32 &value) {
        value = 0;
        while(current()<'0' || current()>'9')
            advance();
        while(current()>='0' && current()<='9') {
            value = value*10+(current()-'0');
            advance();
        }
        return *this;
    }

    furt_reader &operator >>(int &value) {
        value = 0;
        while(current()<'0' || current()>'9')
            advance();
        while(current()>='0' && current()<='9') {
            value = value*10+(current()-'0');
            advance();
        }
        return *this;
    }

    void close(void) {
        fclose(input_file);
    }
};

i32          v[NMAX];

class IHASH {
private:
    vector<pair<i32, i32> > h[MOD];
public:
    i32 &operator[] (i32 arg) {
        i32 poz = arg % MOD;
        for(i32 i=0; i<h[arg].size(); ++i)
            if(h[arg][i].first==arg)
                return h[arg][i].second;
        h[arg].push_back(make_pair(arg, 0));
        return h[arg].back().second;
    }

    void clear(void) {
        for(i32 i=0; i<MOD; ++i)
            h[i].clear();
    }
} mp;

i64 get(int l, int n) {
    if(l==0) return 0LL;

    i64 ans = 0;
    int a, b, c = 0;
    mp.clear();

    mp[0]=1;
    for(a=1, b=1; a<=n; ++a) {
        if(!mp[v[a]])
            ++c;
        ++mp[v[a]];

        while(c>l)
            if(!(--mp[v[b++]])) ///E 00:38, am o scuza...
                --c;

        ans+=a-b+1;
    }

    return ans;
}

int main(void) {
    furt_reader f("secv5.in");
    ofstream    g("secv5.out");
    int n, l, u, c, dr;
    i64 ans;

    ans = 0;

    f>>n>>l>>u;
    for(int i=1; i<=n; ++i)
        f>>v[i];

    g<<get(u, n)-get(l-1, n)<<'\n';

    f.close();
    g.close();
}