Cod sursa(job #840631)

Utilizator stoicatheoFlirk Navok stoicatheo Data 22 decembrie 2012 22:27:08
Problema Secventa 5 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.37 kb
#include<fstream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
int n,L,U,nmax;
vector <pair <unsigned int,int> > A;
int v[1050000];
vector <int> viz;
 
inline long long Nr(int size)
{
    long long sol=0;
    int st=1,dr=1,now=0;
    viz.clear();
    viz.resize(nmax+1,0);
    while(dr<=n)
    {
        if(viz[v[dr]]==0)
            now++;
        viz[v[dr]]++;
        while(now>size)
        {
            viz[v[st]]--;
            if(viz[v[st]]==0)
                now--;
            st++;
        }
        sol+=(dr-st+1);
        dr++;
    }
    return sol;
}
 
int main()
{
    int i,ns,poz;
    unsigned int aux;
    char s[20];
    ifstream fin("secv5.in");
    fin>>n>>L>>U;
    fin.getline(s,20);
    for(i=1;i<=n;i++)
    {
        fin.getline(s,20);
        ns=strlen(s);
        poz=0;
        aux=0;
        while(poz<ns)
            aux=aux*10+s[poz++]-'0';
        A.push_back(make_pair(aux,i));
    }
    fin.close();
     
    int norm=0;
    sort(A.begin(),A.end());
    v[A[0].second]=++norm;
    for(i=1;i<n;i++)
    {
        if(A[i].first==A[i-1].first)
            v[A[i].second]=norm;
        else
            v[A[i].second]=++norm;
    }
    nmax=norm;
     
    long long sol=Nr(U)-Nr(L-1);
    ofstream fout("secv5.out");
    fout<<sol<<"\n";
    fout.close();
    return 0;
}