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

// #include "bits/stdc++.h"

#include <iostream>
#include <vector>
// #include <map>
// #include <string>
// #include <algorithm>
// #include <numeric>
// #include <limits>

using namespace std;

const int none = 0;
const int open = 1;
const int close = -1;

int n;
int w;
int h;

inline void hit(vector<int> &vec, int pos, int range) {
  int start = max(0, pos - range);
  int end = min((int)vec.size() - 1, pos + range);
  vec[start] += open;
  vec[end] += close;
}

inline bool check(vector<int> &vec) {
  int state = 0;
  for (size_t i = 0; i < vec.size() - 1; i++) {
    state += vec[i];
    // std::cout << "debug state " << state << std::endl; // debug
    if (state <= 0) {
      return false;
    }
  }
  return true;
}

void print(const vector<int> &vec) {}

int main() {

  cin >> n >> w >> h;
  w++;
  h++;
  vector<int> vec_w(w, none);
  vector<int> vec_h(h, none);
  for (int i = 0; i < n; i++) {
    int x;
    int y;
    int range;
    cin >> x >> y >> range;
    hit(vec_w, x, range);
    hit(vec_h, y, range);
    // print(vec_w);
    // print(vec_h);
    // cout << endl;
  }
  if (check(vec_w) or check(vec_h)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  // std::cout << "\\e[38;5;0m\\e[48;5;40m --- end ---  \\e[m" << std::endl; //
  // debug
  return 0;
}

Error: TLE

Fixed submission:

// #include "bits/stdc++.h"

#include <iostream>
#include <vector>
// #include <map>
// #include <string>
// #include <algorithm>
// #include <numeric>
// #include <limits>

using namespace std;

const int none = 0;
const int next_side = 1;
const int before_side = 2;
const int both_side = 3;

int n;
int w;
int h;

inline void hit(vector<char> &vec, int pos, int range) {
  int start = max(0, pos - range);
  int end = min((int)vec.size() - 1, pos + range);
  // std::cout << "debug start " << start << " end " << end << std::endl; //
  // debug
  switch (vec[start]) {
  case none:
  case next_side:
    vec[start] = next_side;
    break;
  case before_side:
  case both_side:
    vec[start] = both_side;
    break;
  default:
    cerr << "invalide side" << endl;
    exit(1);
  }
  for (int i = start + 1; i <= end - 1; i++) {
    vec[i] = both_side;
  }
  switch (vec[end]) {
  case none:
  case before_side:
    vec[end] = before_side;
    break;
  case next_side:
  case both_side:
    vec[end] = both_side;
    break;
  default:
    cerr << "invalide side" << endl;
    exit(1);
  }
}

inline bool check(vector<char> &vec) {
  if (vec[0] == none) { // need not before_side check
    return false;
  }
  for (int i = 1; i < (int)vec.size() - 1; i++) {
    if (vec[i] != both_side) {
      return false;
    }
  }
  if (vec[vec.size() - 1] == none) { // need not next_side check
    return false;
  }
  return true;
}

void print(const vector<char> &vec) {
  for (auto v : vec) {
    switch (v) {
    case none:
      cout << "-";
      break;
    case next_side:
      cout << "<";
      break;
    case before_side:
      cout << ">";
      break;
    case both_side:
      cout << "+";
      break;
    }
  }
  cout << endl;
}

int main() {

  cin >> n >> w >> h;
  w++;
  h++;
  vector<char> vec_w(w, none);
  vector<char> vec_h(h, none);
  for (int i = 0; i < n; i++) {
    int x;
    int y;
    int range;
    cin >> x >> y >> range;
    hit(vec_w, x, range);
    hit(vec_h, y, range);
    // print(vec_w);
    // print(vec_h);
    // cout << endl;
  }
  if (check(vec_w) or check(vec_h)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  // std::cout << "\\e[38;5;0m\\e[48;5;40m --- end ---  \\e[m" << std::endl; //
  // debug
  return 0;
}