Problem p01882 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <vector>
#define MAXN 100010
#define INF 0x3f3f3f3f
#define eps 1e-8
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define DEBUG
#define DataIn

typedef long long LL;

using namespace std;

map<string, pair<int, int>> mp;

struct Node {
  int v, t;
  string nam;
  Node(int v, int t, string nam) : v(v), t(t), nam(nam) {}
  Node() {}
  bool operator<(const Node &tmp) const {
    return v > tmp.v || v == tmp.v && t > tmp.t;
  }
};

struct Node2 {
  int v, t;
  string nam;
  Node2(int v, int t, string nam) : v(v), t(t), nam(nam) {}
  Node2() {}
  bool operator<(const Node2 &tmp) const {
    return v < tmp.v || v == tmp.v && t < tmp.t;
  }
};

int main() {
  int n;
  set<Node> notwork;
  set<Node2> work;
  int clk = 0, tot = 0;
  scanf("%d", &n);
  const char msg[2][33] = {"is not working now.", "is working hard now."};
  rep(i, 0, n) {
    char nam[33];
    int v;
    scanf("%s%d", nam, &v);

    mp[nam] = make_pair(v, ++clk);

    if (notwork.size() == 0 || *notwork.begin() < Node(v, clk, nam)) {
      notwork.insert(Node(v, clk, nam));
    } else {
      work.insert(Node2(v, clk, nam));
    }
    tot++;

    int cnt = (int)(tot * 0.2 + eps);

    while (work.size() > cnt) {
      Node2 x = *work.begin();
      notwork.insert(Node(x.v, x.t, x.nam));
      work.erase(work.begin());
    }
    while (work.size() < cnt) {
      Node x = *notwork.begin();
      work.insert(Node2(x.v, x.t, x.nam));
      notwork.erase(notwork.begin());
    }
  }
  int m;
  scanf("%d", &m);

  while (m--) {
    char nam[33], cmd[2];
    int v;
    scanf("%s%s", cmd, nam);
    if (cmd[0] == '+') {
      scanf("%d", &v);
      mp[nam] = make_pair(v, ++clk);

      int flag, flag2 = -1;
      string buf;
      if (notwork.size() == 0 || *notwork.begin() < Node(v, clk, nam)) {
        notwork.insert(Node(v, clk, nam));
        flag = 0;
      } else {
        work.insert(Node2(v, clk, nam));
        flag = 1;
      }
      tot++;

      int cnt = (int)(tot * 0.2 + eps);
      while (work.size() > cnt) {
        Node2 x = *work.begin();
        if (x.v == v && x.t == clk)
          flag = 0;
        else {
          flag2 = 0;
          buf = x.nam;
        }
        notwork.insert(Node(x.v, x.t, x.nam));
        work.erase(work.begin());
      }
      while (work.size() < cnt) {
        Node x = *notwork.begin();
        if (x.v == v && x.t == clk)
          flag = 1;
        else {
          flag2 = 1;
          buf = x.nam;
        }
        work.insert(Node2(x.v, x.t, x.nam));
        notwork.erase(notwork.begin());
      }
      printf("%s %s\\n", nam, msg[flag]);
      if (flag2 >= 0)
        printf("%s %s\\n", buf.c_str(), msg[flag2]);
    } else {
      pair<int, int> x = mp[nam];
      Node nd = *notwork.begin();
      if (x.first < nd.v || x.first == nd.v && x.second <= nd.t) {
        set<Node>::iterator it =
            notwork.lower_bound(Node(x.first, x.second, nam));
        if (it != notwork.end())
          notwork.erase(it);
      } else {
        set<Node2>::iterator it =
            work.lower_bound(Node2(x.first, x.second, nam));
        if (it != work.end())
          work.erase(it);
      }

      tot--;
      int cnt = (int)(tot * 0.2 + eps);

      while (work.size() > cnt) {
        Node2 x = *work.begin();
        printf("%s %s\\n", x.nam.c_str(), msg[0]);
        notwork.insert(Node(x.v, x.t, x.nam));
        work.erase(work.begin());
      }
      while (work.size() < cnt) {
        Node x = *notwork.begin();
        printf("%s %s\\n", x.nam.c_str(), msg[1]);
        work.insert(Node2(x.v, x.t, x.nam));
        notwork.erase(notwork.begin());
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
typedef unsigned long long llu;
const int INF = 0x3f3f3f3f;
const int MAXN = 2e5 + 5;
const int MAXM = 1e6 + 5;
int n, m;
map<string, int> mp1;
map<string, int> mp2;
struct A {
  string name;
  int lev;
  int t;
  A(string na, int lv, int tt) : name(na), lev(lv), t(tt){};
  A(){};
  bool operator<(A b) const {
    if (lev == b.lev)
      return t > b.t;
    return lev > b.lev;
  }
  bool operator==(A b) const { return (lev == b.lev && t == b.t); }
  bool operator>(A b) const {
    if (lev == b.lev)
      return t < b.t;
    return lev < b.lev;
  }
};
vector<A> v;
int now = 1;
char t[300];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  v.clear();
  mp1.clear();
  mp2.clear();
  scanf("%d", &n);
  string b, c;
  int val;
  for (int i = 0; i < n; i++) {
    scanf("%s", t);
    b = t;
    scanf("%d", &val);
    v.push_back(A(b, val, now++));
    mp1[b] = val;
    mp2[b] = now - 1;
  }
  sort(v.begin(), v.end());
  /*for(int i=0;i<v.size();i++)
  {
      cout<<v[i].name<<' '<<v[i].lev<<' '<<v[i].t<<endl;
  }*/
  int d = n / 5;
  int dd;
  int ct;
  vector<A>::iterator cnt;
  A aa;
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    scanf("%s", t);
    b = t;
    scanf("%s", t);
    c = t;
    if (b[0] == '+') {
      scanf("%d", &val);
      aa = A(c, val, now++);
      cnt = lower_bound(v.begin(), v.end(), aa);
      ct = cnt - v.begin() + 1;
      v.insert(cnt, aa);
      mp1[c] = val;
      mp2[c] = now - 1;
      n++;
      dd = n / 5;
      if (ct > dd) {
        if (dd == d) {
          cout << c << " is not working now." << endl;
        } else {
          cout << c << " is not working now." << endl;
          cout << v[dd - 1].name << " is working hard now." << endl;
          d = dd;
        }
      } else {
        if (dd == d) {
          cout << c << " is working hard now." << endl;
          cout << v[dd].name << " is not working now." << endl;
        } else {
          cout << c << " is working hard now." << endl;
          d = dd;
        }
      }

    } else {
      aa = A(c, mp1[c], mp2[c]);
      cnt = lower_bound(v.begin(), v.end(), aa);
      ct = cnt - v.begin() + 1;
      v.erase(cnt);
      n--;
      dd = n / 5;
      if (ct > d) {
        if (d != dd) {
          cout << v[dd].name << " is not working now." << endl;
          d = dd;
        }
      } else {
        if (d == dd) {
          cout << v[dd - 1].name << " is working hard now." << endl;
        }
      }
    }
  }
  return 0;
}