Run time error in the code

Revision en3, by cheapcoder, 2019-12-29 09:51:39

Following is the snippet from my code https://codeforces.com/contest/1283/submission/67856968 This program gives RUN TIME ERROR for n = 2 * 10^5. I don't understand which of the following(vector, map, etc) cannot hold 2 * 10^5 values?

So what is the reason for runtime error?

void solve() { int n, tmp, a, b, c, d, val = 0; cin >> n; vi vec, v1, v2;//vi = vector < int > map < int, int > o, f; rep(i, 0, n) {//repeat from i =0 to i < n; o[i + 1]++; f[i + 1]++; }

rep(i, 1, n + 1) {
    cin >> tmp;
    vec.pb(tmp);//pb = push_back in vec;
    if(tmp)    {
       val++;
       o.erase(i);
       f.erase(tmp);
    }
}
if(val == n) {
    for(auto i: vec)
       cout << i << " ";
    cout << endl;
    return;
}
if(!val) {
    rep(i, 1, n)//for loop from i = 1 to i < n;
       cout << i + 1 << " ";
    cout << 1 << endl;
    return;
}

for(auto s: o) {
    if(o.size() == 2 && f.size() == 2) {
       for(auto a: o)
         v1.pb(a.x);//pb = push_back
       for(auto a: f)
         v2.pb(a.x);
       sort(all(v1));
       sort(all(v2));
       a = v1[0], b = v1[1];
       c = v2[0], d = v2[1];
       if(a != d && b != c) {
         vec[a - 1] = d;
         vec[b - 1] = c;
       }
       else {
         vec[a - 1] = c;
         vec[b - 1] = d;
       }
       break;
    }
    for(auto j: f) {
       if(s.x  == j.x)
         continue;
       else {
         vec[s.x - 1] = j.x;
         f.erase(j.x);
         o.erase(s.x);
         break;
       }
    }

}

Tags #c++, maps, #vectors, mysterious runtime error, runtimeerror, #runtime, #error

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English cheapcoder 2019-12-29 09:51:39 39
en2 English cheapcoder 2019-12-29 09:50:22 1 Tiny change: 'val = 0;\n cin >> n;\' -> 'val = 0;\ncin >> n;\'
en1 English cheapcoder 2019-12-29 09:49:37 1400 Initial revision (published)