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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
#define B 550
#define F first
#define S second
typedef long long ll;
typedef pair<ll, ll> P;
ll t[100005];
struct que {
  ll o, a, b;
};
struct z {
  ll x;
  bool operator<(z &q) {
    if (t[x] != t[q.x])
      return -t[x] < -t[q.x];
    else
      return x < q.x;
  }
};
int n, c;
bool v[100005];
int main(void) {
  scanf("%d%d", &n, &c);
  for (int i = 0; i < c; i += B) {
    set<int> o;
    vector<que> m;
    for (int k = 0; k < n; k++)
      v[k] = false;
    for (int j = i; j < i + B && j < c; j++) {
      int x;
      scanf("%d", &x);
      if (x == 0) {
        ll a, b;
        scanf("%lld%lld", &a, &b);
        a--;
        v[a] = true;
        m.push_back(que{0, a, b});
      } else {
        int a;
        scanf("%d", &a);
        m.push_back(que{1, a, 0});
      }
    }
    vector<z> p;
    vector<P> q;
    for (int k = 0; k < n; k++) {
      if (!v[k])
        q.push_back(P(-t[k], k));
      else
        p.push_back(z{k});
    }
    sort(q.begin(), q.end());
    for (int j = 0; j < m.size(); j++) {
      ll r = m[j].o, a = m[j].a, b = m[j].b;
      if (r == 0)
        t[a] += b;
      else {
        sort(p.begin(), p.end());
        a--;
        bool e = false;
        for (int k = 0; k < p.size(); k++) {
          int h = lower_bound(q.begin(), q.end(), P(-t[p[k].x], p[k].x)) -
                  q.begin();
          if (a < h)
            break;
          else if (a == h) {
            printf("%lld %lld\\n", p[k].x + 1, t[p[k].x]);
            e = true;
            break;
          } else
            a--;
        }
        if (!e) {
          printf("%lld %lld\\n", q[a].S + 1, -q[a].F);
        }
      }
    }
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
#define B 530
#define F first
#define S second
typedef long long ll;
typedef pair<ll, ll> P;
ll t[100005];
struct que {
  ll o, a, b;
};
struct z {
  ll x;
  bool operator<(z &q) {
    if (t[x] != t[q.x])
      return -t[x] < -t[q.x];
    else
      return x < q.x;
  }
};
int n, c;
int main(void) {
  scanf("%d%d", &n, &c);
  for (int i = 0; i < c; i += B) {
    set<int> o;
    vector<que> m;
    for (int j = i; j < i + B && j < c; j++) {
      int x;
      scanf("%d", &x);
      if (x == 0) {
        ll a, b;
        scanf("%lld%lld", &a, &b);
        a--;
        o.insert(a);
        m.push_back(que{0, a, b});
      } else {
        int a;
        scanf("%d", &a);
        m.push_back(que{1, a, 0});
      }
    }
    vector<z> p;
    vector<P> q;
    for (int i = 0; i < n; i++) {
      if (o.find(i) == o.end())
        q.push_back(P(-t[i], i));
      else
        p.push_back(z{i});
    }
    sort(q.begin(), q.end());
    for (int j = 0; j < m.size(); j++) {
      ll r = m[j].o, a = m[j].a, b = m[j].b;
      if (r == 0)
        t[a] += b;
      else {
        sort(p.begin(), p.end());
        a--;
        bool e = false;
        for (int k = 0; k < p.size(); k++) {
          int h = lower_bound(q.begin(), q.end(), P(-t[p[k].x], p[k].x)) -
                  q.begin();
          if (a < h)
            break;
          else if (a == h) {
            printf("%lld %lld\\n", p[k].x + 1, t[p[k].x]);
            e = true;
            break;
          } else
            a--;
        }
        if (!e) {
          printf("%lld %lld\\n", q[a].S + 1, -q[a].F);
        }
      }
    }
  }
}