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

#include <bits/stdc++.h>
using namespace std;
#define maxn 100100
struct P1 {
  string name;
  int v, tim;
  bool operator<(const P1 &pp) const {
    if (v != pp.v)
      return v < pp.v;
    else
      return tim < pp.tim;
  }
};
struct P2 {
  string name;
  int v, tim;
  bool operator<(const P2 &pp) const {
    if (v != pp.v)
      return v > pp.v;
    else
      return tim > pp.tim;
  }
} a[maxn];
set<P1> s1;
set<P2> s2;
int n, m;
string man;
int v;
#define P pair<int, int>
map<string, P> mp;
P1 p2top1(P2 tmp) {
  P1 ans;
  ans.name = tmp.name;
  ans.v = tmp.v;
  ans.tim = tmp.tim;
  return ans;
}
P2 p1top2(P1 tmp) {
  P2 ans;
  ans.name = tmp.name;
  ans.v = tmp.v;
  ans.tim = tmp.tim;
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    a[i].tim = i;
    cin >> a[i].name;
    scanf("%d", &a[i].v);
    mp[a[i].name] = P(a[i].v, i);
  }
  sort(a + 1, a + n + 1);
  int lx = n / 5;
  for (int i = 1; i <= lx; ++i)
    s1.insert(p2top1(a[i]));
  for (int i = lx + 1; i <= n; ++i)
    s2.insert(a[i]);
  scanf("%d", &m);
  int tot = n;
  for (int i = 1; i <= m; ++i) {
    int now = n + i;
    char op[5];
    scanf("%s", op);
    if (*op == '+') {
      cin >> man;
      scanf("%d", &v);
      mp[man] = P(v, now);
      tot++;
      set<P1>::iterator it;
      it = s1.begin();
      P1 tmp, this_man;
      if (it != s1.end()) {
        tmp = (*it);
        this_man.name = man;
        this_man.v = v;
        this_man.tim = now;
      }
      if (it != s1.end() && tmp < this_man) {
        s1.insert(this_man);
        cout << man;
        puts(" is working hard now.");
        if (tot % 5 != 0) {
          P1 tmp = (*it);
          P2 ttmp = p1top2(tmp);
          s1.erase(it);
          s2.insert(ttmp);
          cout << tmp.name;
          puts(" is not working now.");
        }
      } else {
        if (tot % 5 == 0) {
          set<P2>::iterator it;
          it = s2.begin();
          P2 this_, tmp;
          if (it != s2.end()) {
            tmp = (*it);
            this_.name = man;
            this_.v = v;
            this_.tim = now;
          } else
            tmp.v = -100000;
          if (this_ < tmp) {
            P1 cpy = p2top1(this_);
            s1.insert(cpy);
            cout << man;
            puts(" is working hard now.");
          } else {
            s2.erase(*it);
            P1 cpy = p2top1(tmp);
            s1.insert(cpy);
            s2.insert(this_);
            cout << man;
            puts(" is not working now.");
            cout << tmp.name;
            puts(" is working hard now.");
          }
        } else {
          P2 this_;
          this_.name = man;
          this_.v = v;
          this_.tim = now;
          s2.insert(this_);
          cout << man;
          puts(" is not working now.");
        }
      }
    } else {
      tot--;
      cin >> man;
      P sx = mp[man];
      set<P1>::iterator it;
      P1 tmp1;
      tmp1.name = man;
      tmp1.v = sx.first;
      tmp1.tim = sx.second;
      it = s1.find(tmp1);
      if (it == s1.end()) {
        // puts("cc2");
        P2 tmp2;
        tmp2 = p1top2(tmp1);
        s2.erase(tmp2);
        if (tot % 5 == 4) {
          it = s1.begin();
          tmp1 = (*it);
          s1.erase(it);
          tmp2 = p1top2(tmp1);
          s2.insert(tmp2);
          cout << tmp1.name;
          puts(" is not working now.");
        }
      } else {
        // puts("cc3");
        s1.erase(it);
        if (tot % 5 != 4) {
          set<P2>::iterator it2;
          it2 = s2.begin();
          // s2.erase(it2);
          P2 ss = (*it2);
          s2.erase(it2);
          P1 tt = p2top1(ss);
          s1.insert(tt);
          cout << ss.name;
          puts(" is working hard now.");
        }
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> P;

map<P, string> Mp;
map<string, P> O;
P a[20006];

string option, name;
set<P> s1, s2;

int main() {
  int n, m, value, num;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    cin >> name >> a[i].first;
    a[i].second = 0;
    Mp[a[i]] = name;
    O[name] = a[i];
  }
  num = n / 5;
  sort(a, a + n);
  for (int i = n - 1; i >= n - num; i--)
    s1.insert(a[i]);
  for (int i = n - num - 1; i >= 0; i--)
    s2.insert(a[i]);
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    cin >> option >> name;
    if (option[0] == '+') {
      cin >> value;
      O[name] = P(value, i);
      n++;
      if (n / 5 > num) {
        num++;
        auto it = s2.end();
        it--;
        if (P(value, i) > *it) {
          cout << name << " is working hard now.\\n";
          s1.insert(P(value, i));
        } else {
          cout << name << " is not working now.\\n";
          cout << Mp[*it] << " is working hard now.\\n";
          s2.erase(*it);
          s1.insert(*it);
          s2.insert(P(value, i));
        }
      } else {
        if (s1.empty()) {
          cout << name << " is not working now.\\n";
          s2.insert(P(value, i));
        } else {
          auto it = s1.begin();
          if (P(value, i) > *it) {
            s1.erase(*it);
            s2.insert(*it);
            s1.insert(P(value, i));
            cout << name << " is working hard now.\\n";
            cout << Mp[*it] << " is not working now.\\n";
          } else {
            cout << name << " is not working now.\\n";
            s2.insert(P(value, i));
          }
        }
      }
    } else {
      n--;
      if (n / 5 < num) {
        num--;
        auto it = s1.find(O[name]);
        if (it != s1.end()) {
          s1.erase(it);
          continue;
        } else {
          s2.erase(*it);
          auto it1 = s1.begin();
          s1.erase(*it1);
          s2.insert(*it1);
          cout << Mp[*it1] << " is not working now.\\n";
        }
      } else {
        auto it = s1.find(O[name]);
        if (it != s1.end()) {
          s1.erase(*it);
          auto it1 = s2.end();
          it1--;
          s2.erase(*it1);
          s1.insert(*it1);
          cout << Mp[*it1] << " is working hard now.\\n";
        } else {
          s2.erase(*it);
        }
      }
    }
  }
  return 0;
}