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

#define ll long long
#define pb push_back
#include <bits/stdc++.h>
using namespace std;
struct qq {
  int id, d, fr, to;
  int stt;
};
const int N = 500000;
qq que[N];
int t, m, q, a, b, dd, k = 0;
map<int, int> invals;
void addRange(int left, int right) {
  auto l = invals.upper_bound(left), r = invals.upper_bound(right);
  if (l != invals.begin()) {
    l--;
    if (l->second < left)
      l++;
  }
  if (l != r) {
    left = min(left, l->first);
    right = max(right, (--r)->second);
    invals.erase(l, ++r);
  }
  invals[left] = right;
}

bool queryRange(int left, int right) {
  auto it = invals.upper_bound(left);
  if (it == invals.begin() || (--it)->second < right)
    return false;
  return true;
}

void removeRange(int left, int right) {
  auto l = invals.upper_bound(left), r = invals.upper_bound(right);
  if (l != invals.begin()) {
    l--;
    if (l->second < left)
      l++;
  }
  if (l == r)
    return;
  int l1 = min(left, l->first), r1 = max(right, (--r)->second);
  invals.erase(l, ++r);
  if (l1 < left)
    invals[l1] = left;
  if (r1 > right)
    invals[right] = r1;
}
bool cmp(qq a, qq b) {
  if (a.d != b.d)
    return a.d < b.d;
  return a.id < b.id;
}
void solve() {
  invals.clear();
  k = 0;
  scanf("%d%d%d", &t, &m, &q);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &dd, &a, &b);
    que[k].id = 1;
    que[k].d = dd + 1;
    que[k].fr = a;
    que[k].to = b;
    k++;
  }
  for (int i = 0; i < q; i++) {
    scanf("%d%d%d", &dd, &a, &b);
    que[k].id = 2;
    que[k].d = dd;
    que[k].fr = a;
    que[k].to = b;
    que[k].stt = i;
    k++;
  }
  sort(que, que + k, cmp);
  bool ans[q];
  memset(ans, 0, sizeof ans);
  for (int i = 0; i < k; i++) {
    if (que[i].id == 1) {
      addRange(que[i].fr, que[i].to);
    } else {
      if (queryRange(que[i].fr, que[i].to) || que[i].fr >= que[i].to) {
        ans[que[i].stt] = true;
      } else
        ans[que[i].stt] = false;
    }
  }
  for (int i = 0; i < q; i++) {
    if (ans[i])
      printf("Yes\\n");
    else
      printf("No\\n");
  }
}
int main() {
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#define ll long long
#define pb push_back
#include <bits/stdc++.h>
using namespace std;
struct qq {
  int id, d, fr, to;
  int stt;
};
const int N = 500000;
qq que[N];
int t, m, q, a, b, dd, k = 0;
vector<pair<int, int>> invals;
void addRange(int left, int right) {
  int n = invals.size();
  vector<pair<int, int>> tmp;
  for (int i = 0; i <= n; i++) {
    if (i == n || invals[i].first > right) {
      tmp.push_back({left, right});
      while (i < n)
        tmp.push_back(invals[i++]);
    } else if (invals[i].second < left)
      tmp.push_back(invals[i]);
    else {
      left = min(left, invals[i].first);
      right = max(right, invals[i].second);
    }
  }
  swap(invals, tmp);
}

bool queryRange(int left, int right) {
  int n = invals.size(), l = 0, r = n - 1;
  while (l <= r) {
    int m = l + (r - l) / 2;
    if (invals[m].first >= right)
      r = m - 1;
    else if (invals[m].second <= left)
      l = m + 1;
    else
      return invals[m].first <= left && invals[m].second >= right;
  }
  return false;
}
bool cmp(qq a, qq b) {
  if (a.d != b.d)
    return a.d < b.d;
  return a.id < b.id;
}
void solve() {
  invals.clear();
  k = 0;
  scanf("%d%d%d", &t, &m, &q);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &dd, &a, &b);
    que[k].id = 1;
    que[k].d = dd + 1;
    que[k].fr = a;
    que[k].to = b;
    k++;
  }
  for (int i = 0; i < q; i++) {
    scanf("%d%d%d", &dd, &a, &b);
    que[k].id = 2;
    que[k].d = dd;
    que[k].fr = a;
    que[k].to = b;
    que[k].stt = i;
    k++;
  }
  sort(que, que + k, cmp);
  bool ans[q];
  memset(ans, 0, sizeof ans);
  for (int i = 0; i < k; i++) {
    if (que[i].id == 1) {
      addRange(que[i].fr, que[i].to);
    } else {
      if (queryRange(que[i].fr, que[i].to) || que[i].fr >= que[i].to) {
        ans[que[i].stt] = true;
      } else
        ans[que[i].stt] = false;
    }
  }
  for (int i = 0; i < q; i++) {
    if (ans[i])
      printf("Yes\\n");
    else
      printf("No\\n");
  }
}
int main() {
  solve();
  return 0;
}