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

#include "bits/stdc++.h"
#include <unordered_map>
#pragma warning(disable : 4996)
using namespace std;

const long long int MAX = 102000000;
int main() {
  long long int w, h, v, t, x, y, p, q;
  cin >> w >> h >> v >> t >> x >> y >> p >> q;
  long long int dis = v * t;

  long long int ans = 0;
  for (long long int cy = -(MAX / (2 * h)); cy <= MAX / (2 * h); ++cy) {
    long long int dy = (q - y) + cy * h * 2;
    if ((dis * dis) - (dy * dy) >= (p - x) * (p - x)) {
      long long int dx = sqrt(dis * dis - dy * dy);
      long long int l = (dx + (p - x)) / (w * 2);
      long long int r = (dx + (x - p)) / (w * 2);
      ans += l + r + 1;
    } else {
      if (cy > 100)
        break;
    }
  }
  for (long long int cy = -(MAX / (2 * h)); cy <= MAX / (2 * h); ++cy) {

    long long int dy = (q + y) + cy * h * 2;
    if ((dis * dis) - (dy * dy) >= (p - x) * (p - x)) {
      long long int dx = sqrt(dis * dis - dy * dy);
      long long int l = (dx + (p - x)) / (w * 2);
      long long int r = (dx + (x - p)) / (w * 2);
      ans += l + r + 1;
    } else {
      if (cy > 100)
        break;
    }
  }
  for (long long int cy = -(MAX / (2 * h)); cy <= MAX / (2 * h); ++cy) {

    long long int dy = (q - y) + cy * h * 2;
    if ((dis * dis) - (dy * dy) >= 0) {
      long long int dx = sqrt(dis * dis - dy * dy);
      long long int l = max(0, int((dx + 2 * w - p - x) / (w * 2)));
      long long int r = max(0, int((dx + 2 * w - 2 * w + p + x) / (w * 2)));
      ans += l + r;
    } else {
      if (cy > 100)
        break;
    }
  }
  for (long long int cy = -(MAX / (2 * h)); cy <= MAX / (2 * h); ++cy) {

    long long int dy = (q + y) + cy * h * 2;
    if ((dis * dis) - (dy * dy) >= 0) {
      long long int dx = sqrt(dis * dis - dy * dy);
      long long int l = max(0, int((dx + 2 * w - p - x) / (w * 2)));
      long long int r = max(0, int((dx + 2 * w - 2 * w + p + x) / (w * 2)));
      ans += l + r;
    } else {
      if (cy > 100)
        break;
    }
  }
  cout << ans << endl;
  cin >> ans;

  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#pragma warning(disable : 4996)
using namespace std;

const int MAX = 30000000;
int main() {
  int w, h, v, t, x, y, p, q;
  cin >> w >> h >> v >> t >> x >> y >> p >> q;
  long long int dis = v * t;

  long long int ans = 0;
  for (long long int cy = -MAX; cy <= MAX; ++cy) {
    long long int dy = (q - y) + cy * h * 2;
    if ((dis * dis) - (dy * dy) >= (p - x) * (p - x)) {
      long long int dx = sqrt(dis * dis - dy * dy);
      long long int l = (dx + (p - x)) / (w * 2);
      long long int r = (dx + (x - p)) / (w * 2);
      ans += l + r + 1;
    }
  }
  for (long long int cy = -MAX; cy <= MAX; ++cy) {
    long long int dy = (q + y) + cy * h * 2;
    if ((dis * dis) - (dy * dy) >= (p - x) * (p - x)) {
      long long int dx = sqrt(dis * dis - dy * dy);
      long long int l = (dx + (p - x)) / (w * 2);
      long long int r = (dx + (x - p)) / (w * 2);
      ans += l + r + 1;
    }
  }
  for (long long int cy = -MAX; cy <= MAX; ++cy) {
    long long int dy = (q - y) + cy * h * 2;
    if ((dis * dis) - (dy * dy) >= 0) {
      long long int dx = sqrt(dis * dis - dy * dy);
      long long int l = max(0, int((dx + 2 * w - p - x) / (w * 2)));
      long long int r = max(0, int((dx + 2 * w - 2 * w + p + x) / (w * 2)));
      ans += l + r;
    }
  }
  for (long long int cy = -MAX; cy <= MAX; ++cy) {
    long long int dy = (q + y) + cy * h * 2;
    if ((dis * dis) - (dy * dy) >= 0) {
      long long int dx = sqrt(dis * dis - dy * dy);
      long long int l = max(0, int((dx + 2 * w - p - x) / (w * 2)));
      long long int r = max(0, int((dx + 2 * w - 2 * w + p + x) / (w * 2)));
      ans += l + r;
    }
  }
  cout << ans << endl;
  cin >> ans;

  return 0;
}