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

#include <algorithm>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>

bool check(std::vector<std::tuple<int, int>> const &v, int M) {

  if (std::get<0>(v.front()) != 0) {
    return false;
  }

  int bound = std::get<1>(v[0]);
  for (int i = 1; i < v.size(); ++i) {
    if (std::get<0>(v[i]) > bound) {
      return false;
    }
    bound = std::max(bound, std::get<1>(v[i]));
  }
  if (bound != M) {
    return false;
  }
  return true;
}

int main() {

  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  std::string line;
  std::getline(std::cin, line);

  char *p = &*line.begin();

  int N = std::strtol(p, &p, 10);
  int W = std::strtol(p, &p, 10);
  int H = std::strtol(p, &p, 10);

  std::vector<std::tuple<int, int>> X;
  std::vector<std::tuple<int, int>> Y;

  for (int i = 0; i < N; ++i) {
    std::getline(std::cin, line);

    char *p = &*line.begin();
    int x = std::strtol(p, &p, 10);
    int y = std::strtol(p, &p, 10);
    int w = std::strtol(p, &p, 10);

    X.emplace_back(std::max(0, x - w), std::min(W, x + w));
    Y.emplace_back(std::max(0, y - w), std::min(H, y + w));
  }

  std::sort(X.begin(), X.end(),
            [&](std::tuple<int, int> const &l, std::tuple<int, int> const &r) {
              if (std::get<0>(l) < std::get<0>(r)) {
                return true;
              }
              if (std::get<0>(l) == std::get<0>(r)) {
                return std::get<1>(l) < std::get<1>(r);
              }
              return false;
            });

  std::sort(Y.begin(), Y.end(),
            [&](std::tuple<int, int> const &l, std::tuple<int, int> const &r) {
              if (std::get<0>(l) < std::get<0>(r)) {
                return true;
              }
              if (std::get<0>(l) == std::get<0>(r)) {
                return std::get<1>(l) < std::get<1>(r);
              }
              return false;
            });

  if (W < H) {
    std::cout << (check(X, W) ? "Yes" : "No") << std::endl;
  } else if (W > H) {
    std::cout << (check(Y, H) ? "Yes" : "No") << std::endl;
  } else {
    bool f1 = check(X, W);
    bool f2 = check(Y, H);
    std::cout << ((f1 || f2) ? "Yes" : "No") << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

int main() {

  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  std::string line;
  std::getline(std::cin, line);

  char *p = &*line.begin();

  int N = std::strtol(p, &p, 10);
  int W = std::strtol(p, &p, 10);
  int H = std::strtol(p, &p, 10);

  std::vector<bool> X(W, false);
  std::vector<bool> Y(H, false);

  for (int i = 0; i < N; ++i) {
    std::getline(std::cin, line);

    char *p = &*line.begin();
    int x = std::strtol(p, &p, 10);
    int y = std::strtol(p, &p, 10);
    int w = std::strtol(p, &p, 10);

    for (int j = std::max(0, x - w), e = x; j < e; ++j) {
      X[j] = true;
    }
    for (int j = x, e = std::min(W, x + w); j < e; ++j) {
      X[j] = true;
    }

    for (int j = std::max(0, y - w), e = y; j < e; ++j) {
      Y[j] = true;
    }
    for (int j = y, e = std::min(H, y + w); j < e; ++j) {
      Y[j] = true;
    }
  }

  //   for (auto f : X) { std::cout << f; }
  //   std::cout << std::endl;
  //
  //   for (auto f : Y) { std::cout << f; }
  //   std::cout << std::endl;
  //
  if (W < H) {
    for (bool f : X) {
      if (!f) {
        std::cout << "No" << std::endl;
        return 0;
      }
    }
    std::cout << "Yes" << std::endl;
  } else if (W > H) {
    for (bool f : Y) {
      if (!f) {
        std::cout << "No" << std::endl;
        return 0;
      }
    }
    std::cout << "Yes" << std::endl;
  } else {
    bool f1 = true;
    bool f2 = true;
    for (bool f : X) {
      if (!f) {
        f1 = false;
        break;
      }
    }
    for (bool f : Y) {
      if (!f) {
        f2 = false;
        break;
      }
    }

    if (f1 || f2) {
      std::cout << "Yes" << std::endl;
    } else {
      std::cout << "No" << std::endl;
    }
  }
  return 0;
}