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

#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <map>
// #include <utility>
#include <iostream>
#include <set>
// #include <memory>
#include <algorithm>
#include <string>
#include <vector>
// #include <functional>
#include <sstream>
// #include <deque>
#include <complex>
#include <cstdio>
#include <queue>
#include <stack>
// #include <cctype>
#include <cstring>
// #include <ctime>
#include <bitset>
#include <iterator>
// #include <numeric>
// #include <list>
// #include <iomanip>
#include <unordered_map>

#if __cplusplus >= 201103L
#include <array>
#include <forward_list>
#include <initializer_list>
#include <tuple>
#include <unordered_set>

#define cauto const auto &
#else

#endif

using namespace std;

namespace {
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(), (v).rend()
#define map unordered_map

#define MOD 1000000007LL
#define EPS 1e-8
static const int INF = 1 << 24;
map<ll, ll> ma;
priority_queue<pll> pq;

void mainmain() {
  ll n, q;
  cin >> n >> q;
  pq.push(mkp(0, -1));
  rep(i, q) {
    ll a, b;
    cin >> a >> b;
    // ma[a]+=b;
    ll c = ma[a] += b;
    pq.push(mkp(c, -a));
    while (1) {
      pii top = pq.top();
      // if(top.S==-a&&b<0&&top.F>c){
      if (ma[-top.S] != top.F) {
        pq.pop();
        continue;
      } else {
        cout << -top.S << " " << top.F << endl;
        break;
      }
    }
  }
}

} // namespace
main() try { mainmain(); } catch (...) {
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second

static const double PI = 6 * asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<ll, ll> P;
static const int INF = 1 << 24;

ll n, q;
bool f[1000010];
vector<int> ve;
map<ll, ll> ma;
pair<ll, ll> MAX;

bool cmpcount(P T1, P T2) { return T1.S < T2.S; }

int main() {
  cin >> n >> q;
  ll a, v;
  MAX.S = -1;
  rep(i, q) {
    cin >> a >> v;
    if (!f[a]) {
      ve.PB(a);
      f[a] = true;
    }
    ma[a] += v;
    if (ma[a] > MAX.S || (ma[a] == MAX.S && MAX.F > a) ||
        (v >= 0 && MAX.F == a)) {
      MAX.F = a;
      MAX.S = ma[a];
      // cout<<"A ";
    } else if (v < 0 && MAX.F == a) {
      MAX.F = ve[0];
      MAX.S = ma[ve[0]];
      // cout<<"else\\n";
      MAX = *max_element(ma.begin(), ma.end(), cmpcount);
      /*
      rep(j,ve.size()){
              //cout<<"ma[ve[i]] ve[i]"<<ma[ve[j]]<<" "<<ve[j]<<endl;

              if(ma[ve[j]]>MAX.S||(ma[ve[j]]==MAX.S&&MAX.F>ve[j])){
                      MAX.F=ve[j]; MAX.S=ma[ve[j]];
              }

      }
      */
    } else {
      // cout<<"else\\n";
      rep(j, ve.size()) {
        // cout<<"ma[ve[i]] ve[i]"<<ma[ve[j]]<<" "<<ve[j]<<endl;
        if (ma[ve[j]] > MAX.S || (ma[ve[j]] == MAX.S && MAX.F > ve[j])) {
          MAX.F = ve[j];
          MAX.S = ma[ve[j]];
          break;
        }
      }
      // MAX= *max_element(ma.begin(),ma.end(),cmpcount);
    }
    cout << MAX.F << " " << MAX.S << endl;
  }
  return 0;
}