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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;

int main() {
  int N;
  long long int A, B;
  cin >> N >> A >> B;
  vector<vector<pair<long long int, long long int>>> vs(
      5, vector<pair<long long int, long long int>>(1, make_pair(0, 0)));
  for (int i = 0; i < N; ++i) {
    int t;
    long long int x, y;
    cin >> t >> x >> y;
    t--;
    vs[t].push_back(make_pair(x, y));
  }
  sort(vs.begin(), vs.end(),
       [](const vector<pair<long long int, long long int>> &l,
          const vector<pair<long long int, long long int>> &r) {
         return l.size() < r.size();
       });
  vector<pair<long long int, long long int>> l;
  vector<pair<long long int, long long int>> r;
  for (auto v1 : vs[0]) {
    for (auto v2 : vs[1]) {
      for (auto v3 : vs[2]) {
        l.emplace_back(v3.first + v1.first + v2.first,
                       v1.second + v2.second + v3.second);
      }
    }
  }
  for (auto v1 : vs[3]) {
    for (auto v2 : vs[4]) {
      r.emplace_back(v1.first + v2.first, v1.second + v2.second);
    }
  }
  sort(l.begin(), l.end());
  sort(r.begin(), r.end(), greater<pair<long long int, long long int>>());
  auto rt = r.begin();
  auto lt = r.begin();
  map<long long int, int> nb;
  bool ok = false;
  for (auto n : l) {
    while (rt != r.end()) {
      if (rt == r.end())
        break;
      if (rt->first + n.first >= A) {
        nb[rt->second]++;
        rt++;
      } else {
        break;
      }
    }
    while (lt != r.end()) {
      if (lt == r.end())
        break;
      if (lt->first + n.first > B) {
        nb[lt->second]--;
        if (nb[lt->second] == 0)
          nb.erase(lt->second);
        lt++;
      } else {
        break;
      }
    }
    auto at = nb.lower_bound(A - n.second);
    auto bt = nb.upper_bound(B - n.second);
    if (at != bt)
      ok = true;
  }
  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;

#define Seg_Max_N (1 << 18)

using Value = vector<long long int>;
const Value ini = vector<long long int>();
struct segtree {
  int N;
  vector<Value> dat;

  segtree() {}
  segtree(int n) : dat(2 * Seg_Max_N) {
    N = 1;
    while (N < n)
      N *= 2;
    for (int i = 0; i < 2 * N - 1; i++) {
      dat[i] = ini;
    }
  }
  Value connect(const Value &l, const Value &r) {
    Value v(l);
    v.insert(v.end(), r.begin(), r.end());
    sort(v.begin(), v.end());
    return v;
  }
  // update k th element
  void update(int k, long long int a) {
    k += N - 1;
    dat[k].push_back(a);

    while (k > 0) {
      k = (k - 1) / 2;
      const Value al(dat[k * 2 + 1]);
      const Value ar(dat[k * 2 + 2]);
      dat[k] = connect(al, ar);
    }
  }
  // min [a, b)
  bool query(int a, int b, long long int vl, long long int vr) {
    return query(a, b, vl, vr, 0, 0, N);
  }
  bool query(int a, int b, long long int vl, long long int vr, int k, int l,
             int r) {
    if (r <= a or b <= l)
      return false;
    if (a <= l and r <= b) {
      auto it = lower_bound(dat[k].begin(), dat[k].end(), vl);
      if (it != dat[k].end()) {
        return *it <= vr;
      } else {
        return false;
      }
      return lower_bound(dat[k].begin(), dat[k].end(), vl) !=
             upper_bound(dat[k].begin(), dat[k].end(), vr);
    }
    const int m = (l + r) / 2;
    const bool al(query(a, b, vl, vr, k * 2 + 1, l, m));
    const bool ar(query(a, b, vl, vr, k * 2 + 2, m, r));
    return al || ar;
  }
};
template <typename T> struct Compress {
  map<T, int> mp;
  map<int, T> revmp;
  vector<T> v;

  Compress(vector<T> vs) { setmp(vs); }

  Compress() : mp(), revmp() {}
  void setmp(vector<T> vs) {
    sort(vs.begin(), vs.end());
    vs.erase(unique(vs.begin(), vs.end()), vs.end());
    for (int i = 0; i < static_cast<int>(vs.size()); ++i) {
      mp[vs[i]] = i;
      v.push_back(vs[i]);
      revmp[i] = vs[i];
    }
  }
};

int main() {
  int N;
  long long int A, B;
  cin >> N >> A >> B;
  vector<vector<pair<long long, long long int>>> ts(
      5, vector<pair<long long int, long long int>>(1, make_pair(0, 0)));
  for (int i = 0; i < N; ++i) {
    int t;
    long long x, y;
    cin >> t >> x >> y;
    t--;
    ts[t].emplace_back(x, y);
  }
  sort(ts.begin(), ts.end(),
       [](const vector<pair<long long int, long long int>> &l,
          const vector<pair<long long int, long long int>> &r) {
         return l.size() < r.size();
       });
  swap(ts[2], ts[4]);
  swap(ts[0], ts[3]);
  segtree seg(ts[3].size() * ts[4].size());
  Compress<long long int> xcomp;
  vector<long long int> xs;
  for (auto t3 : ts[3]) {
    for (auto t4 : ts[4]) {
      long long int nx = t3.first + t4.first;
      long long int ny = t3.second + t4.second;
      xs.push_back(nx);
    }
  }
  xcomp.setmp(xs);
  for (auto t3 : ts[3]) {
    for (auto t4 : ts[4]) {
      long long int nx = t3.first + t4.first;
      long long int ny = t3.second + t4.second;
      seg.update(xcomp.mp[nx], ny);
    }
  }
  bool ans = false;
  for (auto t0 : ts[0]) {
    for (auto t1 : ts[1]) {
      for (auto t2 : ts[2]) {
        long long int nx = t0.first + t1.first + t2.first;
        long long int ny = t0.second + t2.second + t1.second;

        long long int restx_min = A - nx;
        long long int restx_max = B - nx;

        long long int resty_min = A - ny;
        long long int resty_max = B - ny;

        int xl = lower_bound(xcomp.v.begin(), xcomp.v.end(), restx_min) -
                 xcomp.v.begin();
        int xr = upper_bound(xcomp.v.begin(), xcomp.v.end(), restx_max) -
                 xcomp.v.begin();
        if (xl == xr)
          continue;
        bool ok = seg.query(xl, xr, resty_min, resty_max);
        if (ok) {
          ans = true;
        }
      }
    }
  }
  if (ans)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}