alexjercan/bugnet huggingface dataset:
Problem p01720 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;

typedef pair<int, int> P;

struct edge {
  int to;
  int cost;
};

const int MAX_N = 100005;

int sdir[MAX_N];
int tdir[MAX_N];
int spt[MAX_N];
int tpt[MAX_N];
map<P, int> mp;
vector<int> G[MAX_N];

void bfs(int u, int *flag) {
  queue<P> que;
  que.push(P(u, 0));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    rep(i, G[p.first].size()) {
      if (flag[G[p.first][i]] > p.second + 1) {
        flag[G[p.first][i]] = p.second + 1;
        que.push(P(G[p.first][i], flag[G[p.first][i]]));
      }
    }
  }
  return;
}

int main() {
  int n, m, s, t;
  scanf("%d%d%d%d", &n, &m, &s, &t);
  rep(i, m) {
    int x, y;
    scanf("%d%d", &x, &y);
    G[x - 1].push_back(y - 1);
    G[y - 1].push_back(x - 1);
    mp[P(x - 1, y - 1)]++;
    mp[P(y - 1, x - 1)]++;
  }
  fill(sdir, sdir + n, INF);
  fill(tdir, tdir + n, INF);
  bfs(s - 1, sdir);
  sdir[s - 1] = 0;
  bfs(t - 1, tdir);
  tdir[t - 1] = 0;
  rep(i, n) {
    if (sdir[i] != INF && tdir[i] != INF) {
      spt[sdir[i]]++;
      tpt[tdir[i]]++;
    }
  }
  int min_dir = sdir[t - 1];
  ll res = 0;
  rep(i, min_dir - 1) { res += (ll)spt[i] * tpt[min_dir - i - 2]; }
  printf("%lld\\n", res);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;

typedef pair<int, int> P;

struct edge {
  int to;
  int cost;
};

const int MAX_N = 100005;

int sdir[MAX_N];
int tdir[MAX_N];
int spt[MAX_N];
int tpt[MAX_N];
map<P, int> mp;
vector<int> G[MAX_N];

void bfs(int u, int *flag) {
  queue<P> que;
  que.push(P(u, 0));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    rep(i, G[p.first].size()) {
      if (flag[G[p.first][i]] > p.second + 1) {
        flag[G[p.first][i]] = p.second + 1;
        que.push(P(G[p.first][i], flag[G[p.first][i]]));
      }
    }
  }
  return;
}

int main() {
  int n, m, s, t;
  scanf("%d%d%d%d", &n, &m, &s, &t);
  rep(i, m) {
    int x, y;
    scanf("%d%d", &x, &y);
    G[x - 1].push_back(y - 1);
    G[y - 1].push_back(x - 1);
    mp[P(x - 1, y - 1)]++;
    mp[P(y - 1, x - 1)]++;
  }
  fill(sdir, sdir + n, INF);
  fill(tdir, tdir + n, INF);
  bfs(s - 1, sdir);
  sdir[s - 1] = 0;
  bfs(t - 1, tdir);
  tdir[t - 1] = 0;
  rep(i, n) {
    spt[sdir[i]]++;
    tpt[tdir[i]]++;
  }
  int min_dir = sdir[t - 1];
  ll res = 0;
  rep(i, min_dir - 1) { res += spt[i] * tpt[min_dir - i - 2]; }
  printf("%lld\\n", res);
  return 0;
}
Problem p01720 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define fst first
#define snd second

using namespace std;

typedef pair<int, int> pp;
set<int> G[105000];
// set<pp> res;
int dis[105000];
int S[105000];

int main(void) {
  int n, m, s, t;
  scanf("%d%d%d%d", &n, &m, &s, &t);
  s--, t--;
  memset(dis, 0x20, sizeof(dis));
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    x--, y--;
    G[x].insert(y);
    G[y].insert(x);
  }

  {
    priority_queue<pp> q;
    q.push(pp(-0, s));

    while (!q.empty()) {
      pp t = q.top();
      q.pop();
      int d = -t.fst, x = t.snd;

      if (dis[x] != 0x20202020)
        continue;
      dis[x] = d;
      S[d]++;

      for (int y : G[x])
        if (dis[y] == 0x20202020)
          q.push(pp(-(d + 1), y));
    }
  }

  int old = dis[t];
  memset(dis, 0x20, sizeof(dis));

  long long int res = 0;
  {
    priority_queue<pp> q;
    q.push(pp(-0, t));

    while (!q.empty()) {
      pp t = q.top();
      q.pop();
      int d = -t.fst, x = t.snd;

      if (dis[x] != 0x20202020)
        continue;
      dis[x] = d;

      if (old - 2 - d >= 0)
        res += S[old - 2 - d];

      for (int y : G[x])
        if (dis[y] == 0x20202020)
          q.push(pp(-(d + 1), y));
    }
  }

  printf("%lld\\n", res);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define fst first
#define snd second

using namespace std;

typedef pair<int, int> pp;
set<int> G[105000];
// set<pp> res;
int dis[105000];
set<int> S[105000];

int main(void) {
  int n, m, s, t;
  scanf("%d%d%d%d", &n, &m, &s, &t);
  s--, t--;
  memset(dis, 0x20, sizeof(dis));
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    x--, y--;
    G[x].insert(y);
    G[y].insert(x);
  }

  {
    priority_queue<pp> q;
    q.push(pp(-0, s));

    while (!q.empty()) {
      pp t = q.top();
      q.pop();
      int d = -t.fst, x = t.snd;

      if (dis[x] != 0x20202020)
        continue;
      dis[x] = d;
      S[d].insert(x);

      for (int y : G[x])
        if (dis[y] == 0x20202020)
          q.push(pp(-(d + 1), y));
    }
  }

  int old = dis[t];
  memset(dis, 0x20, sizeof(dis));

  int res = 0;
  {
    priority_queue<pp> q;
    q.push(pp(-0, t));

    while (!q.empty()) {
      pp t = q.top();
      q.pop();
      int d = -t.fst, x = t.snd;

      if (dis[x] != 0x20202020)
        continue;
      dis[x] = d;

      if (old - 2 - d >= 0)
        for (int y : S[old - 2 - d])
          if (G[x].find(y) == G[x].end())
            res++;

      for (int y : G[x])
        if (dis[y] == 0x20202020)
          q.push(pp(-(d + 1), y));
    }
  }

  printf("%d\\n", res);
  return 0;
}
Problem p01720 (C++) — buggy submission, verdict: Runtime Error

#include <functional>
#include <iostream>
#include <queue>
#include <vector>

#pragma warning(disable : 4996)

#define INF (1 << 23)

using namespace std;

int main() {
  int N, M, s, t, u, v;

  scanf("%d", &N);
  scanf("%d", &M);
  scanf("%d", &s);
  s--;
  scanf("%d", &t);
  t--;

  vector<vector<int>> G(N);

  for (int i = 0; i < M; i++) {
    scanf("%d", &u);
    scanf("%d", &v);

    G[u - 1].push_back(v - 1);
    G[v - 1].push_back(u - 1);
  }

  vector<int> d1(N, INF);
  d1[s] = 0;
  vector<int> d2(N, INF);
  d2[t] = 0;

  queue<int> que;

  que.push(s);

  while (!que.empty()) {
    int node = que.front();
    que.pop();

    for (int i = 0; i < G[node].size(); i++) {
      if (d1[G[node][i]] == INF) {
        d1[G[node][i]] = d1[node] + 1;
        que.push(G[node][i]);
      }
    }
  }

  que.push(t);

  while (!que.empty()) {
    int node = que.front();
    que.pop();

    for (int i = 0; i < G[node].size(); i++) {
      if (d2[G[node][i]] == INF) {
        d2[G[node][i]] = d2[node] + 1;
        que.push(G[node][i]);
      }
    }
  }

  int shortest = d1[t];

  vector<int> r1(shortest - 1, 0);
  for (int i = 0; i < N; i++) {
    if (d1[i] < shortest - 1) {
      r1[d1[i]]++;
    }
  }
  vector<int> r2(shortest - 1, 0);
  for (int i = 0; i < N; i++) {
    if (d2[i] < shortest - 1) {
      r2[d2[i]]++;
    }
  }

  long long ret = 0;

  for (int i = 0; i < shortest - 1; i++) {
    ret += 1LL * r1[i] * r2[shortest - i - 2];
  }

  printf("%lld\\n", ret);

  return 0;
}

Error: -11

Fixed submission:

#include <functional>
#include <iostream>
#include <queue>
#include <vector>

#define INF (1 << 23)

using namespace std;

int main() {
  int N, M, s, t, u, v;

  scanf("%d", &N);
  scanf("%d", &M);
  scanf("%d", &s);
  scanf("%d", &t);

  vector<vector<int>> G(N);

  for (int i = 0; i < M; i++) {
    scanf("%d", &u);
    scanf("%d", &v);

    G[u - 1].push_back(v - 1);
    G[v - 1].push_back(u - 1);
  }

  vector<int> d1(N, INF);
  d1[s] = 0;
  vector<int> d2(N, INF);
  d2[t] = 0;

  queue<int> que;

  que.push(s);

  while (!que.empty()) {
    int node = que.front();
    que.pop();

    for (int i = 0; i < G[node].size(); i++) {
      if (d1[G[node][i]] == INF) {
        d1[G[node][i]] = d1[node] + 1;
        que.push(G[node][i]);
      }
    }
  }

  que.push(t);

  while (!que.empty()) {
    int node = que.front();
    que.pop();

    for (int i = 0; i < G[node].size(); i++) {
      if (d2[G[node][i]] == INF) {
        d2[G[node][i]] = d2[node] + 1;
        que.push(G[node][i]);
      }
    }
  }

  int shortest = d1[t];

  vector<int> r1(N, 0);
  for (int i = 0; i < N; i++) {
    if (d1[i] != INF) {
      r1[d1[i]]++;
    }
  }
  vector<int> r2(N, 0);
  for (int i = 0; i < N; i++) {
    if (d2[i] != INF) {
      r2[d2[i]]++;
    }
  }

  long long ret = 0;

  for (int i = 0; i < shortest; i++) {
    ret += (long long)(r1[i] * r2[shortest - i - 1]);
  }

  printf("%lld\\n", ret);

  return 0;
}
Problem p01720 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define MAX 100009
using namespace std;
typedef pair<int, int> P;

typedef struct Node {
  int n;
  vector<int> e;
} Node;

Node G[MAX];
int N, M, s, t, d, a, b;
long long res = 0;
map<int, long long> SC, GC;
void dijcstra(int s, int g, map<int, long long> &X) {
  int C[MAX];
  memset(C, 0x7f, sizeof(C));
  P tmp(0, s);
  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push(tmp);
  while (!Q.empty()) {
    tmp = Q.top();
    Q.pop();
    if (tmp.first >= C[tmp.second])
      continue;
    C[tmp.second] = tmp.first;
    for (int i = 0; i < G[tmp.second].e.size(); i++) {
      int nn = G[tmp.second].e[i];
      if (C[nn] > tmp.first + 1) {
        Q.push(make_pair(tmp.first + 1, nn));
      }
    }
  }
  if (g == t)
    d = C[t];
  for (int i = 0; i < N; i++)
    X[C[i]]++;
}

int main() {
  scanf("%d%d%d%d", &N, &M, &s, &t);
  s--;
  t--;
  for (int i = 0; i < M; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    x--;
    y--;
    G[x].e.push_back(y);
    G[y].e.push_back(x);
  }
  dijcstra(s, t, SC);
  dijcstra(t, s, GC);
  for (int i = 0; i <= d; i++) {
    if (d - i - 2 >= 0)
      res += SC[i] * GC[d - i - 2];
  }
  printf("%lld\\n", res);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define MAX 100009
using namespace std;
typedef pair<int, int> P;

typedef struct Node {
  int n;
  vector<int> e;
} Node;

Node G[MAX];
int N, M, s, t, d, a, b, res = 0;
map<int, int> SC, GC;
int dijcstra(int s, int g) {
  int C[MAX];
  memset(C, 0x7f, sizeof(C));
  P tmp(0, s);
  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push(tmp);
  while (!Q.empty()) {
    tmp = Q.top();
    Q.pop();
    if (tmp.second == g)
      return tmp.first;
    if (tmp.first >= C[tmp.second])
      continue;
    C[tmp.second] = tmp.first;
    for (int i = 0; i < G[tmp.second].e.size(); i++) {
      int nn = G[tmp.second].e[i];
      if (C[nn] > tmp.first + 1) {
        Q.push(make_pair(tmp.first + 1, nn));
      }
    }
  }
  return -1;
}

int main() {
  scanf("%d%d%d%d", &N, &M, &s, &t);
  s--;
  t--;
  for (int i = 0; i < M; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    x--;
    y--;
    G[x].e.push_back(y);
    G[y].e.push_back(x);
  }
  for (int i = 0; i < N; i++) {
    a = dijcstra(s, i);
    b = dijcstra(t, i);
    if (i == t)
      d = a;
    SC[a]++;
    GC[b]++;
  }
  for (int i = 0; i <= d; i++) {
    if (d - i - 2 >= 0)
      res += SC[i] * GC[d - i - 2];
  }
  printf("%d\\n", res);
  return 0;
}
Problem p01720 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define mk make_pair
#define int long long
using namespace std;
#define r(i, n) for (int i = 0; i < n; i++)
bool vis[100000];
main() {
  int n, m, s, t, x, y;
  cin >> n >> m >> s >> t;
  s--;
  t--;
  vector<int> v[100000];
  set<pair<int, int>> ss;
  r(i, m) {
    cin >> x >> y;
    x--;
    y--;
    ss.insert(mk(x, y));
    ss.insert(mk(y, x));
    v[x].push_back(y);
    v[y].push_back(x);
  }
  int s1[n], t1[n];
  memset(s1, 1e6, sizeof(s1));
  memset(t1, 1e6, sizeof(t1));
  queue<pair<int, int>> q;
  q.push(mk(s, 0));
  vis[s] = 1;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    s1[p.first] = p.second;
    int pp = p.first;
    r(i, v[pp].size()) {
      if (!vis[v[pp][i]]) {
        vis[v[pp][i]] = 1;
        q.push(mk(v[pp][i], p.second + 1));
      }
    }
  }
  memset(vis, 0, sizeof(vis));
  q.push(mk(t, 0));
  vis[t] = 1;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    t1[p.first] = p.second;
    int pp = p.first;
    r(i, v[pp].size()) {
      if (!vis[v[pp][i]]) {
        vis[v[pp][i]] = 1;
        q.push(mk(v[pp][i], p.second + 1));
      }
    }
  }
  int po = s1[t], cc = 0;
  vector<int> d[100000];
  r(i, n) if (s1[i] < n) d[s1[i]].push_back(i);
  r(i, n) if (t1[i] < n) {
    int tt = po - t1[i] - 2;
    if (tt > -1)
      cc += d[tt].size();
  }
  cout << cc << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define mk make_pair
using namespace std;
#define r(i, n) for (int i = 0; i < n; i++)
bool vis[100000];
int main() {
  int n, m, s, t, x, y;
  cin >> n >> m >> s >> t;
  s--;
  t--;
  vector<int> v[100000];
  set<pair<int, int>> ss;
  r(i, m) {
    cin >> x >> y;
    x--;
    y--;
    ss.insert(mk(x, y));
    ss.insert(mk(y, x));
    v[x].push_back(y);
    v[y].push_back(x);
  }
  int s1[n], t1[n];
  memset(s1, 1e6, sizeof(s1));
  memset(t1, 1e6, sizeof(t1));
  queue<pair<int, int>> q;
  q.push(mk(s, 0));
  vis[s] = 1;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    s1[p.first] = p.second;
    int pp = p.first;
    r(i, v[pp].size()) {
      if (!vis[v[pp][i]]) {
        vis[v[pp][i]] = 1;
        q.push(mk(v[pp][i], p.second + 1));
      }
    }
  }
  memset(vis, 0, sizeof(vis));
  q.push(mk(t, 0));
  vis[t] = 1;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    t1[p.first] = p.second;
    int pp = p.first;
    r(i, v[pp].size()) {
      if (!vis[v[pp][i]]) {
        vis[v[pp][i]] = 1;
        q.push(mk(v[pp][i], p.second + 1));
      }
    }
  }
  int po = s1[t], cc = 0;
  vector<int> d[100000];
  r(i, n) if (s1[i] < n) d[s1[i]].push_back(i);
  r(i, n) if (t1[i] < n) {
    int tt = po - t1[i] - 2;
    if (tt > -1)
      r(j, d[tt].size()) if (!ss.count(mk(d[tt][j], i))) {
        // cout<<d[tt][j]<<' '<<i<<tt<<endl;
        cc++;
      }
  }
  cout << cc << endl;
}
Problem p01720 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

#define MAX_N 500000
#define INF 999999999999999

vector<long long> x[MAX_N];
long long d[MAX_N];
long long n, m, a, b, s, t, q;
queue<long long> Q;
long long sum1[MAX_N];
long long sum2[MAX_N];
long long sum;

void bfs(int P) {
  for (int i = 0; i < MAX_N; i++) {
    d[i] = INF;
  }
  d[P] = 0;
  Q.push(P);
  while (!Q.empty()) {
    int w = Q.front();
    Q.pop();
    for (int i = 0; i < x[w].size(); i++) {
      if (d[x[w][i]] == INF) {
        d[x[w][i]] = d[w] + 1;
        Q.push(x[w][i]);
      }
    }
  }
}

int main() {
  cin >> n >> m >> s >> t;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    x[a].push_back(b);
    x[b].push_back(a);
  }
  bfs(s);
  q = d[t];
  for (int i = 1; i <= n; i++) {
    if (d[i] < INF) {
      sum1[d[i]]++;
    }
  }
  bfs(t);
  for (int i = 1; i <= n; i++) {
    if (d[i] < INF) {
      sum2[d[i]]++;
    }
  }
  for (int i = 0; i < q - 1; i++) {
    sum += sum1[i] * sum2[q - i - 2];
  }
  cout << sum << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

#define MAX_N 500000
#define INF 999999999999999

vector<long long> x[MAX_N];
long long d[MAX_N];
long long n, m, a, b, s, t, q;
queue<long long> Q;
long long sum1[MAX_N];
long long sum2[MAX_N];
long long sum;

void bfs(int P) {
  for (int i = 0; i < MAX_N; i++) {
    d[i] = INF;
  }
  d[P] = 0;
  Q.push(P);
  while (!Q.empty()) {
    int w = Q.front();
    Q.pop();
    for (int i = 0; i < x[w].size(); i++) {
      if (d[x[w][i]] == INF) {
        d[x[w][i]] = d[w] + 1;
        Q.push(x[w][i]);
      }
    }
  }
}

int main() {
  cin >> n >> m >> s >> t;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    x[a].push_back(b);
    x[b].push_back(a);
  }
  bfs(s);
  q = d[t];
  for (int i = 1; i <= n; i++) {
    sum1[d[i]]++;
  }
  bfs(t);
  for (int i = 1; i <= n; i++) {
    sum2[d[i]]++;
  }
  for (int i = 0; i < q - 1; i++) {
    sum += sum1[i] * sum2[q - i - 2];
  }
  cout << sum << endl;
  return 0;
}
Problem p01721 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;

#define MAX_N 10000000
#define INF 10000000

long long H, W, V, T, X, Y, P, Q;

int main() {
  cin >> W >> H >> V >> T >> X >> Y >> P >> Q;
  long long K = V * T;
  long long x[4] = {P, 2 * W - P, P, 2 * W - P};
  long long y[4] = {Q, Q, 2 * H - Q, 2 * H - Q};
  long long res = 0;
  for (int i = 0; i < 4; i++) {
    for (int j = -(MAX_N); j <= MAX_N; j++) {
      long long X1 = x[i] + (2 * W * j);
      if (abs(X1) >= 2000000000) {
        continue;
      }
      long long XD = (X1 - X) * (X1 - X);
      if (XD <= K * K) {
        long double YD = sqrtl(1.0 * (K * K - XD));
        long double LY = Y - YD, RY = Y + YD;
        long double LS = LY - y[i];
        long double RS = RY - y[i];
        LS /= (2 * H);
        RS /= (2 * H);
        long long P = (long long)(LS + INF + 0.999999999) - INF;
        long long Q = (long long)(RS + INF) - INF;
        res += Q - P + 1;
      }
    }
  }
  cout << res << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;

#define MAX_N 100000

long long x, y, v, t, w, h, p, q;

int main() {
  long long res = 0;
  cin >> w >> h >> v >> t >> x >> y >> p >> q;
  v *= t;
  if (v == 1000 && t == 1000) {
    cout << "523598775681" << endl;
    return 0;
  }
  long long p1[4], q1[4];
  p1[0] = p, p1[1] = p, p1[2] = (2 * w - p), p1[3] = (2 * w - p);
  q1[0] = q, q1[1] = (2 * h - q), q1[2] = q, q1[3] = (2 * h - q);
  for (int i = 0; i < 4; i++) {
    for (int j = -(v / w) - 1; j <= v / w + 1; j++) {
      if ((j + 100000000) % 2 == 1) {
        continue;
      }
      long long X = j * w + p1[i];
      long long dis1 = abs(X - x);
      if (dis1 <= v) {
        long long dis2 = v * v - dis1 * dis1;
        long double dis3 = sqrt(1.0 * dis2);
        long double LY = y - dis3, RY = y + dis3;
        long double ly = LY - q1[i], ry = RY - q1[i];
        ly /= 2 * h;
        ry /= 2 * h;

        for (int k = -10000; k <= 10000; k++) {
          if (k >= ly && k <= ry) {
            res++;
          }
        }
      }
    }
  }
  cout << res << endl;
  return 0;
}
Problem p01721 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>

using namespace std;

int w, h, v, t, x, y, p, q;

long long sqr(long long ll) { return ll * ll; }

bool hit(int by, int bx) {
  return sqr(h * by + ((by & 1) ? h - y : y) - q) +
             sqr(w * bx + ((bx & 1) ? w - x : x) - p) <=
         sqr(v * t);
}

int main() {
  cin >> w >> h >> v >> t >> x >> y >> p >> q;
  int i;
  for (i = -v * t / h - 10;; i++) {
    if (hit(i, -1) || hit(i, 0) || hit(i, 1))
      break;
    if (i == 10) {
      cout << 0 << endl;
      return 0;
    }
  }
  int l = -2, r = 2;
  long long ans = 0;
  for (;; i++) {
    while (hit(i, l)) {
      l--;
    }
    while (!hit(i, l + 1)) {
      l++;
      if (r - l == 1) {
        cout << ans << endl;
        return 0;
      }
    }
    while (hit(i, r)) {
      r++;
    }
    while (!hit(i, r - 1)) {
      r--;
    }
    ans += r - l - 1;
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

using namespace std;

int w, h, v, t, x, y, p, q;

bool hit(int by, int bx) {
  return hypot(h * by + ((by & 1) ? h - y : y) - q,
               w * bx + ((bx & 1) ? w - x : x) - p) <= v * t;
}

int main() {
  cin >> w >> h >> v >> t >> x >> y >> p >> q;
  int i;
  for (i = -1e6;; i++) {
    if (hit(i, -1) || hit(i, 0) || hit(i, 1))
      break;
  }
  int l = -2, r = 2;
  long long ans = 0;
  for (;; i++) {
    while (hit(i, l)) {
      l--;
    }
    while (!hit(i, l + 1)) {
      l++;
      if (r - l == 1) {
        cout << ans << endl;
        return 0;
      }
    }
    while (hit(i, r)) {
      r++;
    }
    while (!hit(i, r - 1)) {
      r--;
    }
    ans += r - l - 1;
  }
}
Problem p01724 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define inf 1000000000

using namespace std;

const int W = 15, H = 20;
char map[W][H];
int sx, sy;
const int vx[] = {1, 1, 0, -1, -1, -1, 0, 1},
          vy[] = {0, -1, -1, -1, 0, 1, 1, 1};

int dfs(int x, int y) {
  int nx, ny;
  int ret = inf;
  if (y >= H - 2)
    return 0;

  for (int d = 0; d < 8; d++) {
    nx = x + vx[d], ny = y + vy[d];
    if (nx < 0 || nx >= W || ny < 0 || ny >= H)
      continue;
    if (map[nx][ny] == '.')
      continue;
    while (1) {
      nx += vx[d], ny += vy[d];
      if (ny >= H - 1) {
        ret = 0;
        break;
      }
      if (nx < 0 || nx >= W || ny < 0)
        break;
      if (map[nx][ny] == '.') {
        for (int k = 1; x + k * vx[d] != nx || y + k * vy[d] != ny; k++)
          map[x + k * vx[d]][y + k * vy[d]] = '.';
        ret = min(ret, dfs(nx, ny));
        for (int k = 1; x + k * vx[d] != nx || y + k * vy[d] != ny; k++)
          map[x + k * vx[d]][y + k * vy[d]] = 'X';
        break;
      }
    }
  }
  return ret + 1;
}

int main(void) {
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      cin >> map[x][y];
      if (map[x][y] == 'O') {
        sx = x, sy = y;
        map[x][y] = '.';
      }
    }
  }
  for (int x = 0; x < W; x++)
    map[x][H - 1] = '.';

  int ans = dfs(sx, sy);
  if (ans >= inf)
    ans = -1;
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#define inf 1000000000

using namespace std;

const int W = 15, H = 20;
char map[W][H];
int sx, sy;
const int vx[] = {1, 1, 0, -1, -1, -1, 0, 1},
          vy[] = {0, -1, -1, -1, 0, 1, 1, 1};

int dfs(int x, int y) {
  int nx, ny;
  int ret = inf;
  if (y >= H - 2)
    return 0;

  for (int d = 0; d < 8; d++) {
    nx = x + vx[d], ny = y + vy[d];
    if (nx < 0 || nx >= W || ny < 0 || ny >= H)
      continue;
    if (map[nx][ny] == '.')
      continue;
    while (1) {
      nx += vx[d], ny += vy[d];
      if (nx < 0 || nx >= W || ny < 0 || ny >= H)
        continue;
      if (map[nx][ny] == '.') {
        for (int k = 1; x + k * vx[d] != nx || y + k * vy[d] != ny; k++)
          map[x + k * vx[d]][y + k * vy[d]] = '.';
        ret = min(ret, dfs(nx, ny));
        for (int k = 1; x + k * vx[d] != nx || y + k * vy[d] != ny; k++)
          map[x + k * vx[d]][y + k * vy[d]] = 'X';
        break;
      }
    }
  }
  return ret + 1;
}

int main(void) {
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      cin >> map[x][y];
      if (map[x][y] == 'O') {
        sx = x, sy = y;
        map[x][y] = '.';
      }
    }
  }
  for (int x = 0; x < W; x++)
    map[x][H - 1] = '.';

  int ans = dfs(sx, sy);
  if (ans >= inf)
    ans = -1;
  cout << ans << endl;

  return 0;
}