Cod sursa(job #1144860)

Utilizator Athena99Anghel Anca Athena99 Data 17 martie 2014 18:11:33
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb
#include <fstream>
#include <vector>

using namespace std;

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

typedef long long uint;

const uint mod= 666013;
const uint nmax= 1<<20;

uint v[nmax+1];
uint sol= 0;

struct str {
    uint x, y;
}; vector <str> h1[mod], h2[mod];

inline str mp( uint x, uint y ) {
    str sol;
    sol.x= x, sol.y= y;
    return sol;
}

void f( vector <str> &h, uint x, uint add, uint &hashnr ) {
    uint ok= 0;
    for ( uint i= 0; i<(uint)h.size(); ++i ) {
        if ( h[i].x==x ) {
            ok= 1, h[i].y+= add;
            if ( h[i].y==0 ) {
                h[i]= h.back(), h.pop_back();
                --hashnr;
            }
            break;
        }
    }

    if ( ok==0 ) {
        h.push_back( mp( x, 1 ) );
        ++hashnr;
    }
}

int check( vector <str> &h, uint x ) {
    for ( vector <str>::iterator it= h.begin(); it!=h.end(); ++it ) {
        if ( (*it).x==x && (*it).y>=2 ) {
            return 1;
        }
    }
    return 0;
}

int main(  ) {
    uint n, a, b;
    fin>>n>>a>>b;
    for ( uint i= 1, l= 1, r= 1, x= 0, y= 0; i<=n; ++i ) {
        fin>>v[i];
        f( h1[v[i]%mod], v[i], 1, x ), f( h2[v[i]%mod], v[i], 1, y );
        
        for ( ; b<x; f( h1[v[l]%mod], v[l], -1, x ), ++l ) ; 
        for ( ; a<y || check(h2[v[r]%mod], v[r]); f( h2[v[r]%mod], v[r], -1, y ), ++r ) ;

        if ( x<=b && y>=a ) {
            sol= sol+r-l+1;
        }
    }

    fout<<sol<<"\n";
    
    return 0;
}