alexjercan/bugnet huggingface dataset:
Problem p01523 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

int N, M;
typedef pair<int, int> P;
vector<P> r;
int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    P p;
    cin >> p.first >> p.second;
    r.push_back(p);
  }
  sort(r.begin(), r.end());
  int cnt = 0;
  int c = 1;
  int i = 0;
  while (true) {
    int n = 0;
    while (i < r.size() && r[i].first <= c) {
      n = max(n, r[i].second);
      i++;
    }
    c = n + 1;
    if (n == 0) {
      if (c <= N) {
        cout << "Impossible" << endl;
        return 0;
      }
    }
    cnt++;
    if (c > N) {
      break;
    }
    if (i == r.size()) {
      if (c <= N) {
        cout << "Impossible" << endl;
        return 0;
      }
      break;
    }
  }
  cout << cnt << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

int N, M;
typedef pair<int, int> P;
vector<P> r;
int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    P p;
    cin >> p.first >> p.second;
    r.push_back(p);
  }
  sort(r.begin(), r.end());
  int cnt = 0;
  int c = 1;
  int i = 0;
  while (true) {
    int n = 0;
    while (i < r.size() && r[i].first <= c) {
      n = max(n, r[i].second);
      i++;
    }
    c = n + 1;
    cnt++;
    if (i == r.size()) {
      if (c <= N) {
        cout << "Impossible" << endl;
        return 0;
      }
      break;
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p01523 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
int n, m, a[100], b[100];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++)
    scanf("%d%d", &a[i], &b[i]);
  int l = 1, c = 0;
  while (l <= n) {
    int ptr = -1;
    for (int j = 0; j < m; j++) {
      if (a[j] <= l && b[j] >= l && b[ptr] < b[j])
        ptr = j;
    }
    if (ptr == -1) {
      printf("Impossible\\n");
      return 0;
    }
    l = b[ptr] + 1;
    c++;
  }
  printf("%d\\n", c);
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int n, m, a[100], b[100];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++)
    scanf("%d%d", &a[i], &b[i]);
  int l = 1, c = 0;
  while (l <= n) {
    int ptr = -1;
    for (int j = 0; j < m; j++) {
      if (a[j] <= l)
        ptr = j;
    }
    if (ptr == -1) {
      printf("Impossible\\n");
      return 0;
    }
    l = b[ptr] + 1;
    c++;
  }
  printf("%d\\n", c);
  return 0;
}
Problem p01523 (C++) — buggy submission, verdict: Time Limit Exceeded

// AOJ 2409

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

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> data(m);
  for (int i = 0; i < m; i++) {
    cin >> data[i].first >> data[i].second;
  }

  int now = 1, ret = 0;

  while (1) {
    int ma = 0, japlj;
    bool flag = false;
    for (int i = 0; i < m; i++) {
      if (data[i].first <= now && now <= data[i].second) {
        // cout << "*";
        ma = max(ma, data[i].second);
        flag = true;
      }
    }
    // puts("");
    if (!flag)
      break;
    now = ma + 1;
    ret++;
    // cout << flag << " " << now << endl;
    // cin >> japlj;

    if (n < now)
      break;
  }

  if (n < now) {
    cout << ret << endl;
  } else {
    cout << "Impossible" << endl;
  }

#if 0
	int now = 1, ret = 0;
	
	while (now < n){
		int ma = 0;
		bool flag = false;
		for (int j = 0; j < m; j++){
			if (data[j].first <= now){
				ma = max(ma, data[j].second);
				flag = true;
			}
		}
		if (ma != 0){
			now = ma + 1;
			ret++;
		}
		// cout << now << endl;
		if (!flag) break;
	}
	
	if (n < now){
		cout << ret << endl;
	}
	else {
		cout << "Impossible" << endl;
	}
#endif

  return 0;
}

Error: TLE

Fixed submission:

// AOJ 2409

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

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> data(m);
  for (int i = 0; i < m; i++) {
    cin >> data[i].first >> data[i].second;
  }

  int now = 1, ret = 0;

  while (now < n) {
    int ma = 0;
    bool flag = false;
    for (int j = 0; j < m; j++) {
      if (data[j].first <= now) {
        ma = max(ma, data[j].second);
        flag = true;
      }
    }
    if (ma != 0) {
      now = ma + 1;
      ret++;
    }
    // cout << now << endl;
    if (!flag)
      break;
  }

  if (n < now) {
    cout << ret << endl;
  } else {
    cout << "Impossible" << endl;
  }

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

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define P pair<int, int>
using namespace std;

int main() {
  int a, b;
  cin >> a >> b;
  int ruisekiwa[108]{};
  vector<P> k;
  int sum = 0;
  for (int c = 0; c < b; c++) {
    int d, e;
    cin >> d >> e;
    d--;
    ruisekiwa[d]++;
    ruisekiwa[e]--;
    e--;
    bool f = true;
    auto u = k.begin();
    for (int g = 0; g < sum; g++, u++) {
      if (k[g].first <= d && k[g].second >= e)
        f = false;
      else if (k[g].first >= d && k[g].second <= e) {
        k.erase(u);
        u--;
        g--;
        sum--;
      }
    }
    if (f) {
      k.push_back(P(d, e));
      sum++;
    }
  }
  bool o = true;
  int q = 0;
  for (int n = 0; n < a; n++) {
    q += ruisekiwa[n];
    if (q == 0)
      o = false;
  }
  if (!o) {
    puts("Impossible");
    return 0;
  }
  if (a == 1) {
    puts("1");
    return 0;
  }
  sort(k.begin(), k.end());
  int now = 0, ans = 0;
  while (now != a) {
    auto t = upper_bound(k.begin(), k.end(), P(now, 1 << 29));
    t--;
    now = (*t).second + 1;
    ans++;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define P pair<int, int>
using namespace std;

int main() {
  int a, b;
  cin >> a >> b;
  int ruisekiwa[100]{};
  P k[100];
  int sum = 0;
  for (int c = 0; c < b; c++) {
    int d, e;
    cin >> d >> e;
    d--;
    ruisekiwa[d]++;
    ruisekiwa[e]--;
    e--;
    bool f = true;
    for (int g = 0; g < sum; g++) {
      if (k[g].first <= d && k[g].second >= e)
        f = false;
    }
    if (f) {
      k[sum] = P(d, e);
      sum++;
    }
  }
  bool o = true;
  int q = 0;
  for (int n = 0; n < a; n++) {
    q += ruisekiwa[n];
    if (q == 0)
      o = false;
  }
  if (!o) {
    puts("Impossible");
    return 0;
  }
  if (a == 1) {
    puts("1");
    return 0;
  }
  sort(k, k + sum);
  int now = 0, ans = 0;
  while (now != a) {
    auto t = upper_bound(k, k + sum, P(now, 1 << 29));
    t--;
    now = (*t).second + 1;
    ans++;
  }
  cout << ans << endl;
}
Problem p01523 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define DEBUG(x) cerr << #x << ":" << x << endl;
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
typedef pair<int, int> P;

template <class T> void pv(T a, T b) {
  for (T it = a; it != b; ++it)
    cerr << *it << " ";
  cerr << endl;
}

bool defense[100];

int main() {
  int N, M;
  cin >> N >> M;

  vector<P> v(M);
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    if (a > b)
      swap(a, b);
    v[i] = mp(a - 1, b - 1);
  }
  // impossible test
  memset(defense, 0, sizeof defense);
  REP(i, M) {
    int s = v[i].first;
    int g = v[i].second;
    for (int i = s; i <= g; i++)
      defense[i] = true;
  }
  if (count(defense, defense + N, false) > 0) {
    cout << "Impossible" << endl;
    exit(0);
  }

  sort(ALL(v));
  int ans = 1;
  int cover = -1;
  {
    REP(i, M) {
      if (v[i].first == 0)
        cover = max(cover, v[i].second);
    }
  }
  // DEBUG(cover)

  for (;;) {
    if (cover == N - 1) {
      break;
    }
    int next = -1;
    REP(i, M) {
      if (v[i].first <= cover + 1) {
        next = max(next, v[i].second);
      } else
        break;
    }
    cover = next;
    // DEBUG(cover)
    ans++;
  }
  cout << ans << endl;
}

Error: 0
cover:4
cover:7


Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define DEBUG(x) cerr << #x << ":" << x << endl;
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
typedef pair<int, int> P;

template <class T> void pv(T a, T b) {
  for (T it = a; it != b; ++it)
    cerr << *it << " ";
  cerr << endl;
}

bool defense[100];

int main() {
  int N, M;
  cin >> N >> M;

  vector<P> v(M);
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    if (a > b)
      swap(a, b);
    v[i] = mp(a - 1, b - 1);
  }
  // impossible test
  memset(defense, 0, sizeof defense);
  REP(i, M) {
    int s = v[i].first;
    int g = v[i].second;
    for (int i = s; i <= g; i++)
      defense[i] = true;
  }
  if (count(defense, defense + N, false) > 0) {
    cout << "Impossible" << endl;
    exit(0);
  }

  sort(ALL(v));
  int ans = 1;
  int cover = -1;
  {
    REP(i, M) {
      if (v[i].first == 0)
        cover = max(cover, v[i].second);
    }
  }
  DEBUG(cover)

  for (;;) {
    if (cover == N - 1) {
      break;
    }
    int next = -1;
    REP(i, M) {
      if (v[i].first <= cover + 1) {
        next = max(next, v[i].second);
      } else
        break;
    }
    cover = next;
    DEBUG(cover)
    ans++;
  }
  cout << ans << endl;
}
Problem p01523 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> Pr;
const int INF = 1 << 28;

int n, m;
int dp[101];
vector<Pr> ps;

int main() {
  fill_n(dp + 1, 100, INF);

  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int s, t;
    scanf("%d %d", &s, &t);
    ps.push_back(Pr(s, t));
  }

  sort(ps.begin(), ps.end());

  for (int i = 0; i < m; i++) {
    for (int j = ps[i].first; j <= ps[i].second; j++) {
      dp[j] = min(dp[j], dp[ps[i].first - 1] + 1);
    }
  }

  int res = dp[n];
  if (res != INF)
    printf("%d\\n", res);
  else
    puts("Impossible");
}

Error: 0

Fixed submission:

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

const int INF = 100000;

bitset<100> A[100];
struct STATE {
  bitset<100> bs;
  int i;
  int j;

  bool operator<(const STATE t) const {
    return (bs.to_ulong() < t.bs.to_ulong());
  }
};

int main() {
  int n, m;
  scanf("%d %d", &n, &m);

  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d %d", &a, &b);

    for (int j = a; j <= b; j++) {
      A[i].set(j);
    }
  }

  int mini = INF;

  queue<STATE> que;
  set<STATE> done;
  for (que.push({0, 0, 0}); !que.empty(); que.pop()) {
    STATE st = que.front();
    if (done.count(st))
      continue;
    done.insert(st);

    if (st.i == m) {
      if (st.bs.count() == n) {
        mini = min(mini, st.j);
        break;
      }
      continue;
    }

    for (int i = st.i; i < m; i++) {
      que.push({st.bs | A[i], i + 1, st.j + 1});
    }
  }

  if (mini != INF)
    printf("%d\\n", mini);
  else
    puts("Impossible");

  return 0;
}
Problem p01523 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

typedef pair<int, int> P;
const int MAX = 105;
const int INF = (1 << 25);
int T[MAX];
vector<P> V;
int n, m;

void input() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    V.push_back(P(a, b));
  }
}

void solve() {
  fill(T, T + MAX, INF);
  T[0] = 0;

  while (1) {
    bool update = false;

    for (int i = 0; i < V.size(); i++) {
      int now = V[i].first;
      int nex = V[i].second;
      for (int j = now + 1; j <= nex; j++) {
        if (T[j] > T[now] + 1) {
          T[j] = T[now] + 1;
          update = true;
        }
      }

      if (T[now - 1] != INF) {
        for (int j = now; j <= nex; j++) {
          if (T[j] > T[now - 1] + 1) {
            T[j] = T[now - 1] + 1;
            update = true;
          }
        }
      }
    }

    if (!update)
      break;
  }

  if (T[n] == INF)
    cout << "Impossible" << endl;
  else
    cout << T[n] << endl;
}

int main() {

  input();
  solve();
  return 0;
}

Error: 0
8 4
3 7
1 5
2 5
4 8



Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

typedef pair<int, int> P;
const int MAX = 105;
const int INF = (1 << 25);
int T[MAX];
vector<int> V[MAX];
int n, m;

void input() {
  cin >> n >> m;
  cerr << n << " " << m << endl;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    cerr << a << " " << b << endl;
    V[a].push_back(b);
  }
  cerr << endl;
}

void solve() {
  fill(T, T + MAX, INF);

  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push(P(1, 0));
  T[1] = 0;

  while (!Q.empty()) {
    P p = Q.top();
    Q.pop();
    int now = p.first;
    int t = p.second;
    if (T[now] < t)
      continue;

    for (int i = 0; i < V[now].size(); i++) {
      int nex = V[now][i];
      for (int j = now + 1; j <= nex; j++)
        if (T[j] > t + 1) {
          T[j] = t + 1;
          Q.push(P(j, t + 1));
        }
    }
  }

  if (T[n] == INF)
    cout << "Impossible" << endl;
  else
    cout << T[n] << endl;
}

int main() {

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

#include <iostream>
#include <stdlib.h>

using namespace std;
int comp(const void *a, const void *b) {
  if (*((int *)b + 1) - *((int *)a + 1)) {
    return *((int *)b + 1) - *((int *)a + 1);
  } else {
    return *(int *)a - *(int *)b;
  }
}

int main() {
  int n, m, dr[101][2], drs = 0, redge = 0;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> dr[i][0] >> dr[i][1];
  }
  qsort(dr, m, sizeof(int) * 2, comp);

  int flag = 0;
  while (redge < n) {
    for (int i = 0; dr[i][1] > redge; i++) {
      if (dr[i][0] <= redge + 1) {
        redge = dr[i][1];
        flag = 1;
        drs++;
        break;
      }
    }
    if (!flag) {
      cout << "Impossible" << endl;
      return 0;
    }
    flag = 0;
  }
  cout << drs << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdlib.h>

using namespace std;
int comp(const void *a, const void *b) {
  if (*((int *)b + 1) - *((int *)a + 1)) {
    return *((int *)b + 1) - *((int *)a + 1);
  } else {
    return *(int *)b - *(int *)b;
  }
}

int main() {
  int n, m, dr[100][2], drs = 0, redge = 1;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> dr[i][0] >> dr[i][1];
  }
  qsort(dr, m, sizeof(int) * 2, comp);

  int flag = 0;
  while (redge < n) {
    for (int i = 0; i < m; i++) {
      if (dr[i][0] <= redge) {
        redge = dr[i][1];
        flag = 1;
        drs++;
        break;
      }
    }
    if (!flag) {
      cout << "Impossible" << endl;
      return 0;
    }
    flag = 0;
  }
  cout << drs << endl;

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

#include <iostream>
using namespace std;
int main() {
  int n, m, a[100], b[100], i, flag = 0, bmax = 0, cnt = 0, bmax2;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    if (a[i] == 1)
      flag = 1;
    if (a[i] == 1 && b[i] > bmax)
      bmax = b[i];
  }
  cnt = 1;
  bmax2 = 0;
  if (flag == 1) {
    while (bmax != n) {
      if (cnt == m + 10) {
        flag = 0;
        break;
      }
      for (i = 0; i < m; i++) {
        if (a[i] <= bmax + 1 && bmax + 1 <= b[i] && bmax2 < b[i]) {
          bmax2 = b[i];
        }
      }
      bmax = bmax2;
      cnt++;
    }
  }
  if (flag == 0)
    cout << "Impossible" << endl;
  else
    cout << cnt << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
struct set {
  int t;
  int s;
  int d;
};
int main() {
  int n, m, i, j, cnt, source, destination, start, end[20001], flag;
  struct set data[20001];
  while (1) {
    flag = 0;
    for (i = 1; i < 20001; i++)
      end[i] = 0;
    end[1] = 1;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    if (m != 0) {
      for (i = 0; i < m; i++) {
        cin >> data[i].t >> data[i].s >> data[i].d;
      }
      for (i = 0; i < m; i++) {
        for (j = 1; j < m; j++) {
          if (data[i].t < data[i - 1].t)
            swap(data[i], data[i - 1]);
        }
      }
      /*      for(i=0;i<m;i++){
        cout << data[i].t << ' ' << data[i].s << ' ' << data[i].d << endl;
        }*/
      for (i = 0; i < m; i++) {
        if (data[i].s == 1 || data[i].d == 1) {
          source = data[i].s;
          destination = data[i].d;
          start = i + 1;
          flag = 1;
          break;
        }
      }
      if (flag == 1) {
        if (source == 1) {
          end[data[i].d] = 1;
          cnt = 2;
        }
        if (destination == 1)
          cnt = 1;
        for (i = start; i < m; i++) {
          if (end[data[i].s] == 1 && end[data[i].d] == 0) {
            cnt++;
            end[data[i].d] = 1;
          }
        }
        cnt = 0;
        for (i = 1; i <= n; i++) {
          if (end[i] == 1)
            cnt++;
        }
        cout << cnt << endl;
      } else
        cout << 1 << endl;
    } else
      cout << 1 << endl;
  }

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

#include <algorithm>
#include <iostream>

using namespace std;

int main(void) {
  int n, m, room[101], cost[101];

  while (cin >> n >> m) {
    fill(room, room + 101, -1);
    fill(cost, cost + 101, -1);

    for (int i = 0, a, b; i < m; ++i) {
      cin >> a >> b;
      cost[a - 1] = max(cost[a - 1], b);
    }

    for (int i = 0; i < n; ++i) {
      if (cost[i] <= -1)
        continue;

      int idx;
      for (idx = i; idx < cost[i]; ++idx)
        room[idx] = i;

      for (int j = i + 1; j <= idx; ++j)
        cost[idx] = max(cost[idx], cost[j]);

      if (idx != i)
        i = idx - 1;
    }

    int ans = 1;

    for (int i = 0, val = room[0]; i < n; ++i) {
      if (room[i] == -1) {
        ans = -1;
        break;
      }
      if (room[i] != val) {
        ++ans;
        val = room[i];
      }
    }

    if (ans == -1)
      cout << "Impossible" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int main(void) {
  int n, m, room[101];

  while (cin >> n >> m) {
    fill(room, room + 101, -1);

    for (int i = 0, a, b; i < m; ++i) {
      cin >> a >> b;
      room[a - 1] = b;
    }

    int ans = 0;

    bool hit = false;

    for (int i = 0; i < (n - 1);) {
      if (room[i] == -1) {
        hit = true;
        break;
      }
      ++ans;
      for (int j = i; j < room[i]; ++j) {
        room[room[i]] = max(room[room[i]], room[j]);
      }
      i = room[i];
    }

    if (room[n] != n) {
      hit = true;
    }
    if (hit)
      cout << "Impossible" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
Problem p01526 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

typedef long long ll;
int N;
ll R;
ll x[200010], y[200010];
unordered_map<ll, unordered_set<ll>> g;
set<pair<ll, ll>> vis;

void dfs(ll y, ll x) {
  vis.insert(make_pair(y, x));
  // cout << x*R << " " << y*R << endl;
  rep(i, 3) rep(j, 3) {
    int dx = i - 1;
    int dy = j - 1;
    if (dx == 0 && dy == 0)
      continue;
    if (g.count(y + dy) == 0)
      continue;
    if (g[y + dy].count(x + dx) == 0)
      continue;
    if (vis.count(make_pair(y + dy, x + dx)))
      continue;
    dfs(y + dy, x + dx);
  }
}

int solve() {
  vis.clear();
  g.clear();
  rep(i, N) { g[y[i] / R].insert(x[i] / R); }
  int ans = 0;
  for (const auto &row : g) {
    for (const auto &x : row.second) {
      ll y = row.first;
      if (vis.count(make_pair(y, x)))
        continue;
      ans++;
      // cout << "b" << endl;
      dfs(y, x);
    }
  }
  return ans;
}

int conv(string s) {
  s.erase(s.find('.'), 1);
  stringstream ss(s);
  ll x;
  ss >> x;
  return x;
};

int main() {
  string RR;
  while (cin >> N >> RR) {
    R = conv(RR);
    rep(i, N) {
      string xx, yy;
      cin >> xx >> yy;
      x[i] = conv(xx), y[i] = conv(yy);
    }
    cout << solve() << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int N;
double R;
int x[200010], y[100010];
unordered_map<int, unordered_map<int, bool>> g;
set<pair<int, int>> vis;

void dfs(int y, int x) {
  vis.insert(make_pair(y, x));
  rep(i, 3) rep(j, 3) {
    int dx = i - 1;
    int dy = j - 1;
    if (dx == 0 && dy == 0)
      continue;
    if (g.count(y + dy) == 0)
      continue;
    if (g[y + dy].count(x + dx) == 0)
      continue;
    dfs(y + dy, x + dx);
  }
}

int solve() {
  vis.clear();
  g.clear();
  rep(i, N) { g[y[i] / R][x[i] / R] = true; }
  int ans = 0;
  for (const auto &row : g) {
    for (const auto &p : row.second) {
      if (vis.count(p))
        continue;
      ans++;
      int y = row.first;
      int x = p.first;
      dfs(x, y);
    }
  }
  return ans;
}

int conv(string s) {
  s.erase(s.find('.'));
  stringstream ss(s);
  int x;
  ss >> x;
  return x * 1000;
};

int main() {
  string RR;
  while (cin >> N >> RR) {
    R = conv(RR);
    rep(i, N) {
      string xx, yy;
      cin >> xx >> yy;
      x[i] = conv(xx), y[i] = conv(yy);
    }
    cout << solve() << endl;
  }
}
Problem p01526 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef pair<double, double> P;
typedef pair<int, int> PP;

double D(P a, P b) {
  return sqrt((a.F - b.F) * (a.F - b.F) + (a.S - b.S) * (a.S - b.S));
}

int p[222222], r[222222];
void init() {
  for (int i = 0; i < 222222; i++)
    p[i] = i, r[i] = 0;
}
int find(int x) { return (p[x] == x) ? x : (p[x] = find(p[x])); }
void unite(int x, int y) {
  x = find(x), y = find(y);
  if (x == y)
    return;
  if (r[x] < r[y])
    p[x] = y;
  else {
    p[y] = x;
    if (r[x] == r[y])
      r[x]++;
  }
}

int main() {
  init();
  int n;
  double d;
  cin >> n >> d;
  P a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i].F >> a[i].S;
  map<PP, int> m;
  for (int i = 0; i < n; i++) {
    int x = floor(a[i].F / d), y = floor(a[i].S / d);
    for (int dx = -1; dx < 2; dx++) {
      for (int dy = -1; dy < 2; dy++) {
        if (m.count(PP(x + dx, y + dy)))
          unite(i, m[PP(x + dx, y + dy)]);
      }
    }
    m[PP(x, y)] = i;
  }
  set<int> s;
  for (int i = 0; i < n; i++)
    s.insert(find(i));
  cout << s.size() << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef pair<double, double> P;

double D(P a, P b) {
  return sqrt((a.F - b.F) * (a.F - b.F) + (a.S - b.S) * (a.S - b.S));
}

int main() {
  int n;
  double r;
  cin >> n >> r;
  P a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i].F >> a[i].S;
  bool u[n];
  memset(u, 0, sizeof(u));
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (u[i])
      continue;
    u[i] = 1;
    ans++;
    for (int j = i + 1; j < n; j++) {
      if (u[j])
        continue;
      if (D(a[i], a[j]) <= r * 2)
        u[j] = 1;
    }
  }
  cout << ans << endl;
  return 0;
}