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

#include <bits/stdc++.h>
using namespace std;

#define int long long
struct NODE {
  vector<pair<int, int>> seg;
  vector<int> special;
  bool sorted;
  NODE *l, *r;
  NODE() {
    l = r = NULL;
    sorted = false;
  }
};

#define N_MAX (1073741824ll)
NODE *add(int l, int r, int v, NODE *root, int a = 1, int b = N_MAX) {
  if (r < a || b < l)
    return root;
  if (root == NULL)
    root = new NODE();
  if (l <= a && b <= r) {
    root->special.push_back(v);
    return root;
  } else {
    root->seg.push_back({v, min(r, b) - max(l, a) + 1});
    int m = (a + b) / 2;

    root->l = add(l, r, v, root->l, a, m);
    root->r = add(l, r, v, root->r, m + 1, b);
  }
  return root;
}
void init(NODE *root) {
  if (root == NULL)
    return;
  sort(root->seg.begin(), root->seg.end());
  sort(root->special.begin(), root->special.end());
  for (int i = 1; i < root->seg.size(); i++) {
    root->seg[i].second += root->seg[i - 1].second;
  }
  init(root->l);
  init(root->r);
  return;
}

int get(int l, int r, int v, NODE *root, int a = 1, int b = N_MAX) {
  if (r < a || b < l)
    return 0;
  if (root == NULL)
    return 0;
  int ans = 0;
  ans += (min(r, b) - max(l, a) + 1) *
         (upper_bound(root->special.begin(), root->special.end(), v) -
          root->special.begin());
  if (l <= a && b <= r) {
    int idx = upper_bound(root->seg.begin(), root->seg.end(),
                          make_pair(v + 1, -1ll)) -
              root->seg.begin() - 1;
    if (idx >= 0) {
      ans += root->seg[idx].second;
    }
  } else {
    int m = (a + b) / 2;
    ans += get(l, r, v, root->l, a, m);
    ans += get(l, r, v, root->r, m + 1, b);
  }
  return ans;
}

signed main() {
  NODE *root = new NODE();
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<int> val;
  while (M--) {
    int a, b, v;
    scanf("%lld%lld%lld", &a, &b, &v);
    add(a, b, v, root);
    val.push_back(v);
  }
  init(root);
  // for( auto &it : seg ){
  // sort(it.second.begin(),it.second.end());
  // for(int i = 1 ; i < it.second.size() ; i++){
  // it.second[i].second += it.second[i-1].second;
  // }
  // }
  // for( auto &it : special ){
  // sort(it.second.begin(),it.second.end());
  // }
  // cout << get(1,1000,1,root) << endl;
  sort(val.begin(), val.end());
  val.erase(unique(val.begin(), val.end()), val.end());
  while (Q--) {
    int a, b, j;
    scanf("%lld%lld%lld", &a, &b, &j);
    int l = 0, r = val.size() - 1;
    while (l != r) {
      int m = (l + r) / 2;
      if (get(a, b, val[m], root) < j) {
        l = m + 1;
      } else {
        r = m;
      }
    }
    printf("%lld\\n", val[l]);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long
unordered_map<int, vector<pair<int, int>>> seg;
unordered_map<int, vector<int>> special;

#define N_MAX (1073741824ll)
void add(int l, int r, int v, int k = 1, int a = 1, int b = N_MAX) {
  if (r < a || b < l)
    return;
  if (l <= a && b <= r) {
    special[k].push_back(v);
    return;
  } else {
    seg[k].push_back({v, min(r, b) - max(l, a) + 1});
    int m = (a + b) / 2;
    add(l, r, v, k * 2, a, m);
    add(l, r, v, k * 2 + 1, m + 1, b);
  }
}

int get(int l, int r, int v, int k = 1, int a = 1, int b = N_MAX) {
  if (r < a || b < l)
    return 0;
  int ans = 0;
  if (special.count(k)) {
    ans += (min(r, b) - max(l, a) + 1) *
           (upper_bound(special[k].begin(), special[k].end(), v) -
            special[k].begin());
  }

  if (l <= a && b <= r) {
    if (seg.count(k)) {
      int idx =
          upper_bound(seg[k].begin(), seg[k].end(), make_pair(v + 1, -1ll)) -
          seg[k].begin() - 1;
      if (idx >= 0) {
        ans += seg[k][idx].second;
      }
    }
  } else {
    int m = (a + b) / 2;
    ans += get(l, r, v, k * 2, a, m);
    ans += get(l, r, v, k * 2 + 1, m + 1, b);
  }
  return ans;
}

signed main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<int> val;
  while (M--) {
    int a, b, v;
    scanf("%lld%lld%lld", &a, &b, &v);
    a += 1;
    b += 1;
    add(a, b, v);
    val.push_back(v);
  }
  for (auto &it : seg) {
    sort(it.second.begin(), it.second.end());
    for (int i = 1; i < it.second.size(); i++) {
      it.second[i].second += it.second[i - 1].second;
    }
  }
  for (auto &it : special) {
    sort(it.second.begin(), it.second.end());
  }
  sort(val.begin(), val.end());
  val.erase(unique(val.begin(), val.end()), val.end());
  while (Q--) {
    int a, b, j;
    scanf("%lld%lld%lld", &a, &b, &j);
    a += 1;
    b += 1;
    int l = 0, r = val.size() - 1;
    while (l != r) {
      int m = (l + r) / 2;
      if (get(a, b, val[m]) < j) {
        l = m + 1;
      } else {
        r = m;
      }
    }
    printf("%lld\\n", val[l]);
  }
}