Cod sursa(job #1018217)

Utilizator Adrian1997Radulescu Adrian Adrian1997 Data 29 octombrie 2013 08:20:46
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.74 kb
#include <fstream>
#include <vector>
#define ct 500000
using namespace std;
ifstream f("secv5.in");
ofstream g("secv5.out");
int n,L,U,v[1050011];
int s1[1050011],s2[1050011];
bool viz[1050011];

vector<pair<int,int> > h[ct+111];
vector<pair<int,int> >::iterator it;

int main(void){
    register int x,y,i,j,p=1,u,nr=0,lim;
    bool ok,con;
    pair<int,int> q;

    f>>n>>L>>U;
    for(i=1;p<=n;i+=(i<n?1:0)){
        if(i<=n && !con)
            f>>v[i];
        if(i==n)
            con=true;
        x=v[i]%ct;
        ok=false;
        for(it=h[x].begin(),j=0;it!=h[x].end();it++,j++){
            q=*it;
            if(q.first==v[i]){
                h[x][j].second++,ok=true;
                break;
            }
        }
        if(!ok)
            h[x].push_back(make_pair(v[i],1)),nr++;
        if(nr>U || i==n){
            s2[p]=i-1;
            y=v[p]%ct;
            do{
                for(it=h[y].begin(),j=0;it!=h[y].end();it++,j++){
                    q=*it;
                    if(q.first==v[p]){
                        h[y][j].second--;
                        if(h[y][j].second==0){
                            nr--;
                            h[y].erase(h[y].begin()+j);
                        }
                        break;
                    }
                }
            }while(nr>U);
            p++;
        }
        if(nr==U)
            s2[p]=i,viz[p]=true;
        if(i==n && nr==U){
            //am ajuns la limita
            while(!viz[p])
                p--;
            lim=p;
            break;
        }

    }
    for(i=1;i<=ct;i++)
        h[i].clear();

    p=1,nr=0;
    for(i=1;p<=lim;i+=(i<n?1:0)){
        x=v[i]%ct;
        ok=false;
        for(it=h[x].begin(),j=0;it!=h[x].end();it++,j++){
            q=*it;
            if(q.first==v[i]){
                h[x][j].second++,ok=true;
                break;
            }
        }
        if(!ok)
            h[x].push_back(make_pair(v[i],1)),nr++;
        if(nr>L || i==n){
            s1[p]=i-1;
            y=v[p]%ct;
            do{
                for(it=h[y].begin(),j=0;it!=h[y].end();it++,j++){
                    q=*it;
                    if(q.first==v[p]){
                        h[y][j].second--;
                        if(h[y][j].second==0){
                            nr--;
                            h[y].erase(h[y].begin()+j);
                        }
                        break;
                    }
                }
            }while(nr>L);
            p++;
        }
        if(nr==L)
            s1[p]=i;

    }

    long long sol=0;
    for(i=1;i<=lim;i++){
        sol+=s2[i]-s1[i]+1;
    }
    g<<sol;
    f.close();
    g.close();
    return 0;
}