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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

// typedef
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long ll;

#define omajinai                                                               \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
#define endll '\\n'

// container util
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define UNIQUE(a) (a).erase(unique((a).begin(), (a).end()), (a).end())
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); --i)
#define RREP(i, n) RFOR(i, n, 0)

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

// debug
#define dump(x) cerr << "[L " << __LINE__ << "] " << #x << " = " << (x) << "\\n";

const int _N = 100000, _C = 100000;

/// BIT
struct BIT {
  int n;
  int el[_N + _C + 2];
  BIT(int n) : n(n + 1) { REP(i, n) el[i] = 0; }
  void add(int i, int a) {
    i++;
    while (i <= n) {
      el[i] += a;
      i += i & -i;
    }
  }
  int sum(int i) {
    i++;
    int s = 0;
    while (i > 0) {
      s += el[i];
      i -= i & -i;
    }
    return s;
  }
};

///

int n, c;
typedef pair<ll, int> cond;
ll pts[_N];
int ctrs[_C];
int t[_C];
bool tp[_C];

VI teamref[_N];
int moved[_N];

bool comp(const cond &a, const cond &b) {
  return a.first == b.first ? a.second > b.second : a.first < b.first;
}

int main() {
  cond conds[_N + _C]; // conditions <point, team no.>
  cin >> n >> c;
  BIT tree(n + c);
  int j = 0;
  REP(i, c) {
    cin >> tp[i];
    if (tp[i]) {
      cin >> t[i];
    } else {
      int p;
      cin >> t[i] >> p;
      t[i]--;
      pts[t[i]] += p;
      conds[j++] = MP(pts[t[i]], t[i]);
    }
  }
  REP(i, n) {
    conds[j + i] = MP(0, i);
    tree.add(i, 1);
  }
  j += n;
  sort(conds, conds + j, comp);
  REP(i, j) { teamref[conds[i].second].push_back(i); }
  REP(i, c) {
    if (tp[i]) {
      // show score
      int low = 0, high = tree.n - 1;
      while (low < high) {
        int mid = (low + high) / 2;
        if (tree.sum(mid) < n - t[i] + 1)
          low = mid + 1;
        else
          high = mid;
      }
      cout << conds[high].second + 1 << " " << conds[high].first << endl;
    } else {
      // move
      int pt = moved[t[i]]++;
      tree.add(teamref[t[i]][pt], -1);
      tree.add(teamref[t[i]][pt + 1], 1);
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

// typedef
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long ll;

// container util
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define UNIQUE(a) (a).erase(unique((a).begin(), (a).end()), (a).end())
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const int _N = 100000, _C = 100000;

typedef pair<ll, int> P;

P data[_N];
int n, c;

bool comp(const P &a, const P &b) { return a.second > b.second; }

int main() {
  cin >> n >> c;
  REP(i, n) data[i].second = -i;
  bool sorted = true;
  REP(i, c) {
    int o;
    cin >> o;
    if (o) { // get place
      int m;
      cin >> m;
      --m;
      if (sorted)
        sort(data, data + n, greater<P>());
      sorted = false;
      cout << -data[m].second + 1 << " " << data[m].first << "\\n";
    } else { // add point
      int t, p;
      cin >> t >> p;
      --t;
      if (!sorted)
        sort(data, data + n, comp);
      sorted = true;
      data[t].first += p;
    }
  }
  return 0;
}