Cod sursa(job #1181160)

Utilizator assa98Andrei Stanciu assa98 Data 1 mai 2014 23:15:13
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.76 kb
#include <fstream>
#include <iostream>
#include <algorithm>
#include <vector>

#define pe pair<unsigned int,int>
#define mp make_pair
#define fi first
#define se second

using namespace std;

class HASH {
private:
    static const int MOD=666013;
    vector<pe> H[MOD+10];

    inline int h(const unsigned int &a) {
        return a%MOD;
    }

    inline bool find(const unsigned int &a) {
        for(auto it:H[h(a)]) {
            if(it.fi==a) {
                return true;
            }
        }
        return false;
    }

public:
    void insert(const unsigned int &a) {
        if(!find(a)) {
            H[h(a)].push_back(mp(a,1));
            return;
        }

        for(auto it=H[h(a)].begin(); it!=H[h(a)].end(); it++) {
            if(it->fi==a) {
                it->se++;
                return;
            }
        }
    }

    int count(const unsigned int &a) {
        for(auto it:H[h(a)]) {
            if(it.fi==a) {
                return it.fi;
            }
        }
        return 0;
    }

    void erase(const int &a) {
        if(!find(a)) {
            return;
        }
        for(auto it=H[h(a)].begin(); it!=H[h(a)].end(); it++) {
            if(it->fi==a) {
                it->se--;
                return;
            }
        }
    }

    void clear() {
        for(int i=0;i<=MOD;i++) {
            H[i].clear();
        }
    }

};

ifstream fin("secv5.in");
ofstream fout("secv5.out");

const int MAX_N=(1<<20)+100;

unsigned int v[MAX_N];
int d1[MAX_N];
int d2[MAX_N];
int n;

HASH S;

void get(int x,int d[MAX_N]) {
    if(x==0) {
        return;
    }
    S.clear();
    int dist=0;
    int sz=0;
    for(int i=1; i<=n && dist<=x; i++) {
        if(S.count(v[i])>0) {
            S.insert(v[i]);
            sz++;
        }
        else {
            if(dist==x) {
                break;
            }
            else {
                S.insert(v[i]);
                sz++;
                dist++;
            }
        }
    }
    d[1]=sz;
    for(int i=2;i<=n;i++) {
        S.erase(v[i-1]);
        d[i]=d[i-1]-1;
        if(S.count(v[i-1])==0) {
            dist--;
            while(i+d[i]<=n && dist<=x) {
                if(S.count(v[i+d[i]])) {
                    S.insert(v[i+d[i]]);
                }
                else {
                    if(dist==x) {
                        break;
                    }
                    else {
                        S.insert(v[i+d[i]]);
                        dist++;
                    }
                }
                d[i]++;
            }
        }
    }
}

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


    get(a-1,d1);
    get(b,d2);

    long long ans=0;
    for(int i=1; i<=n; i++) {
        ans+=d2[i]-d1[i];
    }

    fout<<ans;

    return 0;
}