Problem p01882 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define Mp make_pair
#define X first
#define Y second
#define N 111111
using namespace std;
int n, m, i, id, x, V[N];
string s, str[N];
char ch[9];
set<pair<int, int>> S, S2;
set<pair<int, int>>::iterator it;
pair<int, int> era;
map<string, int> mp;
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    cin >> s;
    mp[s] = ++id;
    str[id] = s;
    scanf("%d", &x);
    V[id] = x;
    S2.insert(Mp(x, id));
  }
  for (i = 1; i <= n / 5; i++) {
    it = S2.end();
    it--;
    S.insert(*it);
    S2.erase(S2.find(*it));
  }
  scanf("%d", &m);
  for (i = 1; i <= m; i++) {
    scanf("%s", ch);
    cin >> s;
    if (ch[0] == '-') {
      x = mp[s];
      if (S.count(Mp(V[x], x))) {
        S.erase(S.find(Mp(V[x], x)));
        if ((S.size() + S2.size() + 1) % 5 != 0) {
          it = S2.end();
          it--;
          cout << str[(*it).Y];
          puts(" is working hard now.");
          S2.erase(S2.find(*it));
          S.insert(*it);
        }
      } else {
        S2.erase(S2.find(Mp(V[x], x)));
        if ((S.size() + S2.size()) % 5 == 4) {
          it = S.begin();
          cout << str[(*it).Y];
          puts(" is not working now.");
          S.erase(S.find(*it));
          S2.insert(*it);
        }
      }
    } else {
      scanf("%d", &x);
      mp[s] = ++id;
      str[id] = s;
      V[id] = x;
      if (S.size() == 0) {
        S2.insert(Mp(x, id));
        if (S2.size() == 5) {
          it = S2.end();
          it--;
          if ((*it) == Mp(x, id)) {
            cout << str[id];
            puts(" is working hard now.");
          } else {
            cout << str[id];
            puts(" is not working now.");
            cout << str[(*it).Y];
            puts(" is working hard now.");
          }
          S2.erase(S2.find(*it));
          S.insert(*it);
        } else {
          cout << str[id];
          puts(" is not working now.");
        }
      } else {
        it = S.begin();
        if (Mp(x, id) > *it) {
          era = *it;
          S.erase(S.find(*it));
          S.insert(Mp(x, id));
          cout << str[id];
          puts(" is working hard now.");
          if ((S.size() + S2.size() + 1) % 5 == 0) {
            S.insert(era);
          } else {
            S2.insert(era);
            cout << str[era.Y];
            puts(" is not working now.");
          }
        } else {
          S2.insert(Mp(x, id));
          if ((S.size() + S2.size()) % 5 == 0) {
            it = S2.end();
            it--;
            if (Mp(x, id) == (*it)) {
              cout << str[id];
              puts(" is working hard now.");
            } else {
              cout << str[id];
              puts(" is not working now.");
              cout << str[(*it).Y];
              puts(" is working hard now.");
            }
            S2.erase(S2.find(*it));
            S.insert(*it);
          } else {
            cout << str[id];
            puts(" is not working now.");
          }
        }
      }
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>

using namespace std;

const int N = 5e4 + 5;

struct Node {
  int s, id;
  Node(int s = 0, int id = 0) : s(s), id(id) {}
  bool operator<(const Node &r) const {
    return s > r.s || (s == r.s && id > r.id);
  }
};

int n, m, tot;
int a[N];
char name[N][30];
set<Node> hard, lazy;
map<string, int> ID;

inline void workhard(int id) { printf("%s is working hard now.\\n", name[id]); }

inline void notwork(int id) { printf("%s is not working now.\\n", name[id]); }

inline void lazy2hard() {
  hard.insert(*lazy.begin());
  workhard(lazy.begin()->id);
  lazy.erase(lazy.begin());
}

inline void hard2lazy() {
  auto it = hard.end();
  --it;
  notwork(it->id);
  lazy.insert(*it);
  hard.erase(it);
}

int main() {
  int x;
  char op[5];
  Node node;
  scanf("%d", &n);
  tot = n;
  for (int i = 0; i < n; ++i) {
    scanf("%s%d", name + i, a + i);
    lazy.insert(Node(a[i], i));
    ID[name[i]] = i;
  }
  while (hard.size() < n / 5) {
    hard.insert(*lazy.begin());
    lazy.erase(lazy.begin());
  }
  scanf("%d", &m);
  m += n;
  for (int i = n; i < m; ++i) {
    scanf("%s%s", op, name[i]);
    if (op[0] == '+') {
      scanf("%d", a + i);
      ID[name[i]] = i;
      node = Node(a[i], i);
      ++tot;
      if (hard.lower_bound(node) != hard.end()) {
        hard.insert(node);
        workhard(i);
        if (hard.size() > tot / 5)
          hard2lazy();
      } else if (lazy.lower_bound(node) != lazy.begin()) {
        lazy.insert(node);
        notwork(i);
        if (hard.size() < tot / 5)
          lazy2hard();
      } else {
        if (hard.size() < tot / 5) {
          workhard(i);
          hard.insert(node);
        } else {
          notwork(i);
          lazy.insert(node);
        }
      }
    } else {
      --tot;
      x = ID[name[i]];
      node = Node(a[x], x);
      if (hard.count(node)) {
        hard.erase(node);
        if (hard.size() < tot / 5)
          lazy2hard();
      } else {
        lazy.erase(node);
        if (hard.size() > tot / 5)
          hard2lazy();
      }
    }
  }
}