dimanche 27 février 2022

Getting a runtime error using set as a map key value

I am getting a runtime error on a few test cases, i suspect it has something to do with my use of multiset as a map key value , the number of sets i use in the map is less than 100,000 each. I have no real other way of doing this problem without a map or a multiset. Nothing should go out of bounds, it should be impossible for the code to access and empty set

map<multiset<long long>, long long> ma;
map<multiset<long long>, long long> mb;
set<multiset<long long> > v;

int main(){
    long long w,k;
    cin>>w>>k;
    if(w==1){
         long long n;
    cin>>n;
    long long a[n+5];
    long long sorta[n+5];
    long long ld=0;
    sorta[0]=0;
    for(long long i=1; i<=n; i++){
        cin>>a[i];
        if(a[i]<a[i-1]){
            ld=i;
        }
        sorta[i]=ld;
    }
    long long m;
    cin>>m;
        long long b[m+5];
        long long sortb[m+5];
        sortb[0]=0;
        ld=0;
     for(long long i=1; i<=m; i++){
        cin>>b[i];
        if(b[i]<b[i-1]){
            ld=i;
        }
        sortb[i]=ld;
    }
    multiset <long long> s;
    long long las=0;
    for(long long i=1; i<=min(n,k); i++){
        s.insert(a[i]);
        
            las=a[1];
        
    }   
    if(1>=sorta[k]){
        ma[s]++;
        v.insert(s);
    }

    for(long long i=k+1; i<=n; i++){
        s.erase(s.find(las));
        s.insert(a[i]);
        las=a[i-k+1];
        if(sorta[i]<=i-k+1){
            ma[s]++;

            v.insert(s);
        }
    }
    s.clear();
    for(long long i=1; i<=min(m,k); i++){
        s.insert(b[i]);
        las=b[1];
    }   
    if(sortb[k]<=1){
        mb[s]++;
    }
    for(long long i=k+1; i<=m; i++){
        s.erase(s.find(las));
        s.insert(b[i]);
        las=b[i-k+1];
        if(sortb[i]<=i-k+1){
            mb[s]++;
        }
    }
    long long pas=0;
    set< multiset<long long> >:: iterator ii;
    for(ii=v.begin(); ii!=v.end(); ii++ ){
        pas+=ma[*ii]*mb[*ii];

    }
    cout<<pas;
    return 0;
    }
}

Aucun commentaire:

Enregistrer un commentaire