Cod sursa(job #1766490)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 27 septembrie 2016 23:27:13
Problema Secventa 5 Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.35 kb
#include<fstream>
#include<vector>

using namespace std;

ofstream fout ("secv5.out");

class InputReader{
public:
    InputReader() {}
    InputReader (const char *name) {
        fin = fopen(name, "r");
        buffpos = Size - 1;
    }

    inline InputReader &operator >> (int &n) {
        char x = nextpos();
        while (x < '0' || x > '9') {
            x = nextpos();
        }
        n = 0;
        while (x >= '0' && x <= '9') {
            n = n * 10 + x - '0';
            x = nextpos();
        }
        return *this;
    }
private:
    FILE *fin;
    int buffpos;
    static const int Size = 1 << 17;
    char buff[ Size ];

    inline char nextpos() {
        ++ buffpos;
        if (buffpos == Size) {
            buffpos = 0;
            fread(buff, Size, 1, fin);
        }
        return buff[ buffpos ];
    }
} fin ("secv5.in");

class Hash {
public:
    static const int mod = 1e6 + 3;
    vector<pair< int, int >> h[ mod ];

    int baga (int x) {
        int key = x % mod;
        for (int i = 0; i < (int)h[ key ].size(); ++ i) {
            if (h[ key ][ i ].first == x) {
                ++ h[ key ][ i ].second;
                return 0;
            }
        }
        h[ key ].push_back( make_pair(x, 1) );
        return 1;
    }
    int scoate (int x) {
        int key = x % mod;
        for (int i = 0; i < (int)h[ key ].size(); ++ i) {
            if (h[ key ][ i ].first == x) {
                -- h[ key ][ i ].second;
                if (h[ key ][ i ].second == 0) {
                    swap(h[ key ][ i ], h[ key ].back());
                    h[ key ].pop_back();
                    return 1;
                }
                return 0;
            }
        }
        return 0;
    }
} h1, h2;

const int nmax = 1 << 20;
int v[nmax + 1];

int main() {
    int l, u, n;
    fin >> n >> l >> u;
    for (int i = 1; i <= n; ++ i) {
        fin >> v[ i ];
    }

    int a, b, ind1, ind2;
    a = b = 0;
    ind1 = ind2 = 1;

    long long ans = 0;
    for (int i = 1; i <= n; ++ i) {
        a += h1.baga( v[ i ] );
        b += h2.baga( v[ i ] );
        while (a >= l && ind1 <= i) {
            a -= h1.scoate( v[ ind1 ++ ] );
        }

        while (b > u && ind2 < i) {
            b -= h2.scoate( v[ ind2 ++ ] );
        }

        if (b >= l)
            ans += 1LL * (ind1 - ind2);
    }

    fout << ans << "\n";

    fout.close();
    return 0;
}