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

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

int main() {
  int n;
  cin >> n;

  vector<int> x(n), y(n);
  vector<char> c(n);

  for (int i = 0; i < n; ++i) {
    cin >> x[i] >> y[i] >> c[i];
  }

  multimap<int, int> ux, dx, ly, ry;
  multimap<int, int> upp, ump, dpp, dpm, lpp, lmp, rpp, rpm;
  for (int i = 0; i < n; ++i) {
    switch (c[i]) {
    case 'U':
      ux.emplace(x[i], i);
      upp.emplace(x[i] + y[i], i);
      ump.emplace(-x[i] + y[i], i);
      break;
    case 'D':
      dx.emplace(x[i], i);
      dpp.emplace(x[i] + y[i], i);
      dpm.emplace(x[i] - y[i], i);
      break;
    case 'L':
      ly.emplace(y[i], i);
      lpp.emplace(x[i] + y[i], i);
      lmp.emplace(-x[i] + y[i], i);
      break;
    case 'R':
      ry.emplace(y[i], i);
      rpp.emplace(x[i] + y[i], i);
      rpm.emplace(x[i] - y[i], i);
      break;
    }
  }

  const int never_happen = 100000000;
  int fastest = never_happen;

  // check U and D
  for (auto itu = ux.begin(); itu != ux.end();) {
    map<int, char> ud;
    ud.clear();

    auto pu = ux.equal_range(itu->first);
    auto pd = dx.equal_range(itu->first);
    itu = pu.second;

    for (auto it = pu.first; it != pu.second; ++it) {
      // fprintf(stderr,"%d %d %c\\n",x[it->second],y[it->second],c[it->second]);
      ud.emplace(y[it->second], c[it->second]);
    }

    for (auto it = pd.first; it != pd.second; ++it) {
      // fprintf(stderr,"%d %d %c\\n",x[it->second],y[it->second],c[it->second]);
      ud.emplace(y[it->second], c[it->second]);
    }

    bool found_u = false;
    int base;
    for (auto it = ud.begin(); it != ud.end(); ++it) {
      fprintf(stderr, "%d %c\\n", it->first, it->second);
      if (found_u != false) {
        if (it->second == 'D') {
          fastest = min(fastest, (it->first - base) * 5);
          found_u = false;
        } else {
          base = it->first;
        }
      } else {
        if (it->second == 'U') {
          found_u = true;
          base = it->first;
        }
      }
    }
  }

  // check L and R
  for (auto itl = ly.begin(); itl != ly.end();) {
    map<int, char> lr;
    lr.clear();

    auto pl = ly.equal_range(itl->first);
    auto pr = ry.equal_range(itl->first);
    itl = pl.second;

    for (auto it = pl.first; it != pl.second; ++it) {
      // fprintf(stderr,"%d %d %c\\n",x[it->second],y[it->second],c[it->second]);
      lr.emplace(x[it->second], c[it->second]);
    }

    for (auto it = pr.first; it != pr.second; ++it) {
      // fprintf(stderr,"%d %d %c\\n",x[it->second],y[it->second],c[it->second]);
      lr.emplace(x[it->second], c[it->second]);
    }

    bool found_r = false;
    int base;
    for (auto it = lr.begin(); it != lr.end(); ++it) {
      fprintf(stderr, "%d %c\\n", it->first, it->second);
      if (found_r != false) {
        if (it->second == 'L') {
          fastest = min(fastest, (it->first - base) * 5);
          found_r = false;
        } else {
          base = it->first;
        }
      } else {
        if (it->second == 'R') {
          found_r = true;
          base = it->first;
        }
      }
    }
  }

  // check U and L
  for (auto itu = ump.begin(); itu != ump.end();) {
    map<int, char> ul;
    ul.clear();

    auto pu = ump.equal_range(itu->first);
    auto pl = lmp.equal_range(itu->first);
    itu = pu.second;

    for (auto it = pu.first; it != pu.second; ++it) {
      // fprintf(stderr,"%d %d %c\\n",x[it->second],y[it->second],c[it->second]);
      ul.emplace(y[it->second], c[it->second]);
    }

    for (auto it = pl.first; it != pl.second; ++it) {
      // fprintf(stderr,"%d %d %c\\n",x[it->second],y[it->second],c[it->second]);
      ul.emplace(y[it->second], c[it->second]);
    }

    bool found_u = false;
    int base;
    for (auto it = ul.begin(); it != ul.end(); ++it) {
      fprintf(stderr, "%d %c\\n", it->first, it->second);
      if (found_u != false) {
        if (it->second == 'L') {
          fastest = min(fastest, (it->first - base) * 10);
          found_u = false;
        } else {
          base = it->first;
        }
      } else {
        if (it->second == 'U') {
          found_u = true;
          base = it->first;
        }
      }
    }
  }

  // check U and R
  for (auto itu = upp.begin(); itu != upp.end();) {
    map<int, char> ur;
    ur.clear();

    auto pu = upp.equal_range(itu->first);
    auto pr = rpp.equal_range(itu->first);
    itu = pu.second;

    for (auto it = pu.first; it != pu.second; ++it) {
      // fprintf(stderr,"%d %d %c\\n",x[it->second],y[it->second],c[it->second]);
      ur.emplace(y[it->second], c[it->second]);
    }

    for (auto it = pr.first; it != pr.second; ++it) {
      // fprintf(stderr,"%d %d %c\\n",x[it->second],y[it->second],c[it->second]);
      ur.emplace(y[it->second], c[it->second]);
    }

    bool found_u = false;
    int base;
    for (auto it = ur.begin(); it != ur.end(); ++it) {
      fprintf(stderr, "%d %c\\n", it->first, it->second);
      if (found_u != false) {
        if (it->second == 'R') {
          fastest = min(fastest, (it->first - base) * 10);
          found_u = false;
        } else {
          base = it->first;
        }
      } else {
        if (it->second == 'U') {
          found_u = true;
          base = it->first;
        }
      }
    }
  }

  // check D and L
  for (auto itd = dpp.begin(); itd != dpp.end();) {
    map<int, char> dl;
    dl.clear();

    auto pd = dpp.equal_range(itd->first);
    auto pl = lpp.equal_range(itd->first);
    itd = pd.second;

    for (auto it = pd.first; it != pd.second; ++it) {
      // fprintf(stderr,"%d %d %c\\n",x[it->second],y[it->second],c[it->second]);
      dl.emplace(y[it->second], c[it->second]);
    }

    for (auto it = pl.first; it != pl.second; ++it) {
      // fprintf(stderr,"%d %d %c\\n",x[it->second],y[it->second],c[it->second]);
      dl.emplace(y[it->second], c[it->second]);
    }

    bool found_d = false;
    int base;
    for (auto it = dl.rbegin(); it != dl.rend(); ++it) {
      fprintf(stderr, "%d %c\\n", it->first, it->second);
      if (found_d != false) {
        if (it->second == 'L') {
          fastest = min(fastest, (-it->first + base) * 10);
          found_d = false;
        } else {
          base = it->first;
        }
      } else {
        if (it->second == 'D') {
          found_d = true;
          base = it->first;
        }
      }
    }
  }

  // check D and R
  for (auto itd = dpm.begin(); itd != dpm.end();) {
    map<int, char> dr;
    dr.clear();

    auto pd = dpm.equal_range(itd->first);
    auto pr = rpm.equal_range(itd->first);
    itd = pd.second;

    for (auto it = pd.first; it != pd.second; ++it) {
      // fprintf(stderr,"%d %d %c\\n",x[it->second],y[it->second],c[it->second]);
      dr.emplace(y[it->second], c[it->second]);
    }

    for (auto it = pr.first; it != pr.second; ++it) {
      // fprintf(stderr,"%d %d %c\\n",x[it->second],y[it->second],c[it->second]);
      dr.emplace(y[it->second], c[it->second]);
    }

    bool found_d = false;
    int base;
    for (auto it = dr.rbegin(); it != dr.rend(); ++it) {
      fprintf(stderr, "%d %c\\n", it->first, it->second);
      if (found_d != false) {
        if (it->second == 'R') {
          fastest = min(fastest, (-it->first + base) * 10);
          found_d = false;
        } else {
          base = it->first;
        }
      } else {
        if (it->second == 'D') {
          found_d = true;
          base = it->first;
        }
      }
    }
  }

#ifdef NEVER_DEFINED
  // check L and R
  for (auto itl = ly.begin(); itl != ly.end(); ++itl) {
    auto p = ry.equal_range(itl->first);
    for (auto itr = p.first; itr != p.second; ++itr) {
      if (x[itl->second] > x[itr->second]) {
        fastest = min(fastest, (x[itl->second] - x[itr->second]) * 5);
      }
    }
  }

  // check U and L
  for (auto itu = ump.begin(); itu != ump.end(); ++itu) {
    auto p = lmp.equal_range(itu->first);
    for (auto itl = p.first; itl != p.second; ++itl) {
      if ((y[itu->second] < y[itl->second]) &&
          (x[itu->second] < x[itl->second])) {
        fastest = min(fastest, (y[itl->second] - y[itu->second]) * 10);
      }
    }
  }

  // check U and R
  for (auto itu = upp.begin(); itu != upp.end(); ++itu) {
    auto p = rpp.equal_range(itu->first);
    for (auto itr = p.first; itr != p.second; ++itr) {
      if ((y[itu->second] < y[itr->second]) &&
          (x[itu->second] > x[itr->second])) {
        fastest = min(fastest, (y[itr->second] - y[itu->second]) * 10);
      }
    }
  }

  // check D and L
  for (auto itd = dpp.begin(); itd != dpp.end(); ++itd) {
    auto p = lpp.equal_range(itd->first);
    for (auto itl = p.first; itl != p.second; ++itl) {
      if ((y[itd->second] > y[itl->second]) &&
          (x[itd->second] < x[itl->second])) {
        fastest = min(fastest, (y[itd->second] - y[itl->second]) * 10);
      }
    }
  }

  // check D and R
  for (auto itd = dpm.begin(); itd != dpm.end(); ++itd) {
    auto p = rpm.equal_range(itd->first);
    for (auto itr = p.first; itr != p.second; ++itr) {
      if ((y[itd->second] > y[itr->second]) &&
          (x[itd->second] > x[itr->second])) {
        fastest = min(fastest, (y[itd->second] - y[itr->second]) * 10);
      }
    }
  }

#endif

  if (fastest != never_happen) {
    cout << fastest << endl;
  } else {
    cout << "SAFE" << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;

  vector<int> x(n), y(n);
  vector<char> c(n);

  for (int i = 0; i < n; ++i) {
    cin >> x[i] >> y[i] >> c[i];
  }

  multimap<int, int> ux, dx, ly, ry;
  multimap<int, int> upp, ump, dpp, dpm, lpp, lmp, rpp, rpm;
  for (int i = 0; i < n; ++i) {
    switch (c[i]) {
    case 'U':
      ux.emplace(x[i], i);
      upp.emplace(x[i] + y[i], i);
      ump.emplace(-x[i] + y[i], i);
      break;
    case 'D':
      dx.emplace(x[i], i);
      dpp.emplace(x[i] + y[i], i);
      dpm.emplace(x[i] - y[i], i);
      break;
    case 'L':
      ly.emplace(y[i], i);
      lpp.emplace(x[i] + y[i], i);
      lmp.emplace(-x[i] + y[i], i);
      break;
    case 'R':
      ry.emplace(y[i], i);
      rpp.emplace(x[i] + y[i], i);
      rpm.emplace(x[i] - y[i], i);
      break;
    }
  }

  const int never_happen = 100000000;
  int fastest = never_happen;

  // check U and D
  for (auto itu = ux.begin(); itu != ux.end(); ++itu) {
    auto p = dx.equal_range(itu->first);
    for (auto itd = p.first; itd != p.second; ++itd) {
      if (y[itd->second] > y[itu->second]) {
        fastest = min(fastest, (y[itd->second] - y[itu->second]) * 5);
      }
    }
  }

  // check L and R
  for (auto itl = ly.begin(); itl != ly.end(); ++itl) {
    auto p = ry.equal_range(itl->first);
    for (auto itr = p.first; itr != p.second; ++itr) {
      if (x[itl->second] > x[itr->second]) {
        fastest = min(fastest, (x[itl->second] - x[itr->second]) * 5);
      }
    }
  }

  // check U and L
  for (auto itu = ump.begin(); itu != ump.end(); ++itu) {
    auto p = lmp.equal_range(itu->first);
    for (auto itl = p.first; itl != p.second; ++itl) {
      if ((y[itu->second] < y[itl->second]) &&
          (x[itu->second] < x[itl->second])) {
        fastest = min(fastest, (y[itl->second] - y[itu->second]) * 10);
      }
    }
  }

  // check U and R
  for (auto itu = upp.begin(); itu != upp.end(); ++itu) {
    auto p = rpp.equal_range(itu->first);
    for (auto itr = p.first; itr != p.second; ++itr) {
      if ((y[itu->second] < y[itr->second]) &&
          (x[itu->second] > x[itr->second])) {
        fastest = min(fastest, (y[itr->second] - y[itu->second]) * 10);
      }
    }
  }

  // check D and L
  for (auto itd = dpp.begin(); itd != dpp.end(); ++itd) {
    auto p = lpp.equal_range(itd->first);
    for (auto itl = p.first; itl != p.second; ++itl) {
      if ((y[itd->second] > y[itl->second]) &&
          (x[itd->second] < x[itl->second])) {
        fastest = min(fastest, (y[itd->second] - y[itl->second]) * 10);
      }
    }
  }

  // check D and R
  for (auto itd = dpm.begin(); itd != dpm.end(); ++itd) {
    auto p = rpm.equal_range(itd->first);
    for (auto itr = p.first; itr != p.second; ++itr) {
      if ((y[itd->second] > y[itr->second]) &&
          (x[itd->second] > x[itr->second])) {
        fastest = min(fastest, (y[itd->second] - y[itr->second]) * 10);
      }
    }
  }

  if (fastest != never_happen) {
    cout << fastest << endl;
  } else {
    cout << "SAFE" << endl;
  }
}