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

// Amazing Graze
#include <cmath>
#include <iostream>
// #include <deque>
#include <algorithm>
#include <vector>

using namespace std;

#define ABS(x) ((x) < 0 ? -(x) : x)
#define POW(x) ((x) * (x))

struct Mech {
  int x, y;
  int power;
};

struct Bullet {
  int x, y;
  bool operator<(const Bullet &var) const { return x < var.x; };
};

int AN, BN, R;
// deque<Mech> mech;
// deque<Bullet> bullet;
vector<Mech> mech;
vector<Bullet> bullet;

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

  int totalpower = 0;
  int i = 0;
  Mech tm1 = {};
  Bullet tb1 = {}, tb2 = {};
  // deque<Bullet>::iterator sortbeg, sortend;
  vector<Bullet>::iterator sortbeg, sortend;
  mech.reserve(100000);
  bullet.reserve(100000);
  while (1) {
    cin >> AN >> BN >> R;
    if (AN == 0 && BN == 0)
      break;
    R *= 4; // R4倍
    mech.clear();
    bullet.clear();
    // 代入
    for (i = 0; i < AN; ++i) {
      cin >> tm1.x >> tm1.y;
      tm1.power = 0;
      mech.push_back(tm1);
    }
    /*//xソートしながら
    cin >> tb1.x >> tb1.y;
    bullet.push_back(tb1);
    for (i = 1; i < BN; ++i){
            cin >> tb1.x >> tb1.y;
            if(bullet.back() < tb1)bullet.push_back(tb1);
            else bullet.push_front(tb1);
    }*/
    for (i = 0; i < BN; ++i) {
      cin >> tb1.x >> tb1.y;
      bullet.push_back(tb1);
    }
    sort(bullet.begin(), bullet.end());
    // 戦闘力計算
    totalpower = 0;
    for (i = 0; i < AN; ++i) { // 各戦闘機について
      // Xソートで範囲確認
      tb1.x = mech[i].x - R;
      tb2.x = mech[i].x + R;
      if (tb1.x > 0)
        sortbeg = lower_bound(bullet.begin(), bullet.end(), tb1);
      else
        sortbeg = bullet.begin();
      if (sortbeg == bullet.end())
        continue;
      sortend = upper_bound(sortbeg, bullet.end(), tb2);
      for (; sortbeg != sortend; ++sortbeg) {
        if (ABS(mech[i].y - sortbeg->y) > R)
          continue;
        if (POW(mech[i].x - sortbeg->x) + POW(mech[i].y - sortbeg->y) <=
            POW(R)) {
          ++mech[i].power;
        }
      }
      totalpower += mech[i].power;
    }

    cout << totalpower << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// Amazing Graze
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>

using namespace std;

#define ABS(x) ((x) < 0 ? -(x) : x)
#define POW(x) ((x) * (x))

struct Mech {
  int x, y;
  int power;
};

struct Bullet {
  int x, y;
  bool operator<(const Bullet &var) const { return x < var.x; };
};

int AN, BN, R;
deque<Mech> mech;
deque<Bullet> bullet;

int main() {
  int totalpower = 0;
  Mech tm1 = {};
  Bullet tb1 = {}, tb2 = {};
  deque<Bullet>::iterator sortbeg, sortend;
  while (1) {
    cin >> AN >> BN >> R;
    if (AN == 0 && BN == 0)
      break;
    R *= 4; // R4倍
    mech.clear();
    bullet.clear();
    // 代入
    for (int i = 0; i < AN; ++i) {
      cin >> tm1.x >> tm1.y;
      tm1.power = 0;
      mech.push_back(tm1);
    }
    // xソートしながら
    cin >> tb1.x >> tb1.y;
    bullet.push_back(tb1);
    for (int i = 1; i < BN; ++i) {
      cin >> tb1.x >> tb1.y;
      if (bullet.back() < tb1)
        bullet.push_back(tb1);
      else
        bullet.push_front(tb1);
    }
    // 戦闘力計算
    totalpower = 0;
    for (int i = 0; i < AN; ++i) { // 各戦闘機について
      // Xソートで範囲確認
      tb1.x = mech[i].x - R;
      tb2.x = mech[i].x + R;
      if (tb1.x > 0)
        sortbeg = lower_bound(bullet.begin(), bullet.end(), tb1);
      else
        sortbeg = bullet.begin();
      if (sortbeg == bullet.end())
        continue;
      sortend = upper_bound(sortbeg, bullet.end(), tb2);
      for (; sortbeg != sortend; ++sortbeg) {
        if (ABS(mech[i].y - sortbeg->y) > R)
          continue;
        if (POW(mech[i].x - sortbeg->x) + POW(mech[i].y - sortbeg->y) <=
            POW(R)) {
          ++mech[i].power;
        }
      }
      totalpower += mech[i].power;
    }

    cout << totalpower << endl;
  }

  return 0;
}