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

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (int(i) = (s); (i) <= (e); (i)++)
#define all(x) x.begin(), x.end()

int C, N;

int p[1010][1010];

int wide[1010][1010];
int hei[1010][1010];

int main() {
  cin >> C >> N;
  rep(i, 0, N - 1) {
    string s;
    cin >> s;
    rep(j, 0, N - 1) { p[i][j] = s[j] == '1'; }
  }

  rep(i, 0, N - 1) {
    rep(j, 0, N - 1) {
      if (j < N / 2)
        wide[i][j] ^= p[i][j];
      else
        wide[i][N - j - 1] ^= p[i][j];
      if (i < N / 2)
        hei[i][j] ^= p[i][j];
      else
        hei[N - i - 1][j] ^= p[i][j];
    }
  }
  int now = 0;
  rep(i, 0, N / 2 - 1) {
    rep(j, 0, N / 2 - 1) { now += wide[i][j] + hei[i][j]; }
  }

  int ans = now == 0;
  rep(q, 0, C - 2) {
    int D;
    cin >> D;
    rep(d, 0, D - 1) {
      int i, j;
      cin >> i >> j;
      i--;
      j--;
      if (j < N / 2)
        now += (wide[i][j] ^ 1) - wide[i][j];
      else
        now += (wide[i][N - 1 - j] ^ 1) - wide[i][N - 1 - j];
      if (i < N / 2)
        now += (hei[i][j] ^ 1) - hei[i][j];
      else
        now += (hei[N - 1 - i][j] ^ 1) - hei[N - 1 - i][j];
      if (j < N / 2)
        wide[i][j] ^= 1;
      else
        wide[i][N - j - 1] ^= 1;
      if (i < N / 2)
        hei[i][j] ^= 1;
      else
        hei[N - i - 1][j] ^= 1;
    }
    ans += now == 0;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (int(i) = (s); (i) <= (e); (i)++)
#define all(x) x.begin(), x.end()

int C, N;

int p[1010][1010];

int calc() {
  rep(i, 0, N - 1) {
    rep(j, 0, N - 1) {
      if (p[i][j] == p[i][N - j - 1]) {
      } else {
        return 0;
      }
      if (p[N - i - 1][j] == p[i][j]) {
      } else {
        return 0;
      }
    }
  }
  return 1;
}

int main() {
  cin >> C >> N;
  rep(i, 0, N - 1) {
    string s;
    cin >> s;
    rep(j, 0, N - 1) { p[i][j] = s[j] == '1'; }
  }
  int ans = calc();
  rep(q, 0, C - 2) {
    int D;
    cin >> D;
    rep(d, 0, D - 1) {
      int r, c;
      cin >> r >> c;
      r--;
      c--;
      p[r][c] ^= 1;
    }
    ans += calc();
  }
  cout << ans << endl;
}
Problem p00315 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
string s[1001];
int n, c, x, y, cp, p, ans;
int ch(int x, int y) {
  if (s[n - y - 1][n - x - 1] != s[y][x])
    return 0;
  if (s[n - y - 1][x] != s[y][x])
    return 0;
  if (s[y][n - x - 1] != s[y][x])
    return 0;
  return 1;
}
int main() {
  cin >> c >> n;
  r(i, n) cin >> s[i];
  r(i, n / 2) r(j, n / 2) cp += ch(j, i);
  ans = (cp == n * n / 4) ? 1 : 0;
  r(i, c - 1) {
    cin >> p;
    r(i, p) {
      cin >> y >> x;
      x--;
      y--;
      if (ch(x, y))
        cp--;
      if (s[y][x] == '0')
        s[y][x] = '1';
      else
        s[y][x] = '0';
      if (ch(x, y))
        cp++;
    }
    ans += (cp == n * n / 4) ? 1 : 0;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
string s[1001];
int n, c, x, y, p, ans;
bool ch() {
  r(i, n / 2) r(j, n / 2) if (s[n - i - 1][j] != s[i][j] ||
                              s[n - i - 1][n - j - 1] != s[i][j] ||
                              s[i][n - j - 1] != s[i][j]) return 0;
  return 1;
}
int main() {
  cin >> c >> n;
  r(i, n) cin >> s[i];
  if (ch())
    ans++;
  r(i, c - 1) {
    cin >> p;
    r(i, p) {
      cin >> y >> x;
      x--;
      y--;
      if (s[y][x] == '0')
        s[y][x] = '1';
      else
        s[y][x] = '0';
    }
    if (ch())
      ans++;
  }
  cout << ans << endl;
}
Problem p00315 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
#define F first
#define S second
typedef long long ll;
typedef pair<ll, ll> P;
int c, n, ans, s;
int p[2005][2005];
bool v[1005][1005];
int main(void) {
  cin >> c >> n;
  for (int i = 0; i < n; i++) {
    char f[1005];
    cin >> f;
    for (int j = 0; j < n; j++)
      if (f[j] == '1')
        p[i][j] = 1;
  }
  for (int i = 0; i < n / 2; i++) {
    for (int j = 0; j < n / 2; j++) {
      int x = n - 1 - i, y = n - 1 - j;
      if (p[i][j] == p[x][j] && p[x][j] == p[i][y] && p[i][y] == p[x][y])
        v[i][j] = true;
      if (!v[i][j])
        ans++;
    }
  }
  if (ans == 0)
    s++;
  c--;
  while (c) {
    int d;
    cin >> d;
    for (int i = 0; i < d; i++) {
      int x, y, z, w;
      cin >> x >> y;
      x--;
      y--;
      z = n - 1 - x;
      w = n - 1 - y;
      p[x][y] = (p[x][y] + 1) % 2;
      bool q = false;
      if (p[x][y] == p[x][w] && p[x][w] == p[z][y] && p[z][y] == p[z][w])
        q = true;
      bool u = v[min(x, z)][min(y, w)];
      if (u && !q)
        ans++;
      if (!u && q)
        ans--;
      v[min(x, z)][min(y, w)] = q;
    }
    if (ans == 0)
      s++;
    c--;
  }
  cout << s << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
#define F first
#define S second
typedef long long ll;
typedef pair<ll, ll> P;
int c, n, ans, s;
int p[2005][2005];
bool v[1005][1005];
int main(void) {
  cin >> c >> n;
  for (int i = 0; i < n / 2; i++)
    for (int j = 0; j < n / 2; j++)
      v[i][j] = true;
  for (int i = 0; i < n; i++) {
    char f[1005];
    cin >> f;
    for (int j = 0; j < n; j++)
      if (f[j] == '1')
        p[i][j] = 1;
  }
  while (c) {
    int d;
    cin >> d;
    for (int i = 0; i < d; i++) {
      int x, y, z, w;
      cin >> x >> y;
      x--;
      y--;
      z = n - 1 - x;
      w = n - 1 - y;
      p[x][y] = (p[x][y] + 1) % 2;
      bool q = false;
      if (p[x][y] == p[x][w] && p[x][w] == p[z][y] && p[z][y] == p[z][w])
        q = true;
      bool u = v[min(x, z)][min(y, w)];
      if (u && !q)
        ans++;
      if (!u && q)
        ans--;
      v[min(x, z)][min(y, w)] = q;
    }
    if (ans == 0)
      s++;
    c--;
  }
  cout << s << endl;
}
Problem p00315 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int count(const vector<string> &P) {
  int N = P.size(), ret = 0;
  for (int r = 0; r < (N + 1) / 2; ++r) {
    for (int c = 0; c < (N + 1) / 2; ++c) {
      if ((P[r][c] == P[N - 1 - r][c]) and (P[r][c] == P[r][N - 1 - c]) and
          (P[r][c] == P[N - 1 - r][N - 1 - c]))
        continue;
      ++ret;
    }
  }
  return ret;
}

int check(vector<string> &P, int r, int c) {
  int N = P.size();
  bool prev = (P[r][c] == P[N - 1 - r][c]) and (P[r][c] == P[r][N - 1 - c]) and
              (P[r][c] == P[N - 1 - r][N - 1 - c]);
  P[r][c] = '0' + '1' - P[r][c];
  bool cur = (P[r][c] == P[N - 1 - r][c]) and (P[r][c] == P[r][N - 1 - c]) and
             (P[r][c] == P[N - 1 - r][N - 1 - c]);
  return prev - cur;
}

int main() {
  int C, N;
  cin >> C >> N;
  vector<string> P(N);
  for (int i = 0; i < N; ++i) {
    cin >> P[i];
  }
  int d = count(P);
  int ans = 0;
  if (not d)
    ++ans;
  for (int i = 0; i < C - 1; ++i) {
    int D;
    cin >> D;
    for (int j = 0; j < D; ++j) {
      int r, c;
      cin >> r >> c;
      --r, --c;
      // P[r][c] = '0' + '1' - P[r][c];
      d += check(P, r, c);
    }
    if (not d)
      ++ans;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

bool is_symmetry(const vector<string> &P) {
  int N = P.size();
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (P[i][j] != P[i][N - 1 - j])
        return false;
      if (P[i][j] != P[N - 1 - i][j])
        return false;
    }
  }
  return true;
}

int main() {
  int C, N;
  cin >> C >> N;
  vector<string> P(N);
  for (int i = 0; i < N; ++i) {
    cin >> P[i];
  }
  int ans = is_symmetry(P);
  for (int i = 0; i < C - 1; ++i) {
    int D;
    cin >> D;
    for (int j = 0; j < D; ++j) {
      int r, c;
      cin >> r >> c;
      --r, --c;
      P[r][c] = '0' + '1' - P[r][c];
    }
    ans += is_symmetry(P);
  }
  cout << ans << endl;
}
Problem p00316 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
constexpr int MOD = 1000000000 + 7;
constexpr int INF = 2000000000;
const double PI = acos(-1);

int Par[100001];
int Rank[100001];
int id[100001];

bool flag;

void init(int n) {
  for (int i = 0; i < n; i++) {
    Par[i] = i;
    Rank[i] = 0;
    id[i] = -1;
  }
}

int find(int x) {
  if (Par[x] == x) {
    return x;
  } else {
    return Par[x] = find(Par[x]);
  }
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (Rank[x] > Rank[y])
    swap(x, y);
  Par[x] = y;

  if (id[x] != -1 && id[y] != -1 && id[x] != id[y])
    flag = true;

  if (id[x] != -1) {
    id[y] = id[x];
  }
}

bool same(int x, int y) { return find(x) == find(y); }

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  init(n);
  int a, b, c;
  for (int i = 0; i < k; i++) {
    cin >> a >> b >> c;
    if (a == 1) {
      unite(b, c);
      if (flag) {
        cout << i + 1 << endl;
        return 0;
      }
    } else if (a == 2) {
      b = find(b);
      if (id[b] != -1 && id[b] != c) {
        cout << i + 1 << endl;
        return 0;
      }
      id[b] = c;
    }
  }
  cout << 0 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
constexpr int MOD = 1000000000 + 7;
constexpr int INF = 2000000000;
const double PI = acos(-1);

int Par[100000];
int Rank[100000];
int id[100000];

bool flag;

void init(int n) {
  for (int i = 0; i < n; i++) {
    Par[i] = i;
    Rank[i] = 0;
    id[i] = -1;
  }
}

int find(int x) {
  if (Par[x] == x) {
    return x;
  } else {
    return Par[x] = find(Par[x]);
  }
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (Rank[x] > Rank[y])
    swap(x, y);
  Par[x] = y;

  if (id[x] != -1 && id[y] != -1 && id[x] != id[y])
    flag = true;

  if (id[x] != -1) {
    id[y] = id[x];
  }
}

bool same(int x, int y) { return find(x) == find(y); }

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  init(n + m);
  int a, b, c;
  for (int i = 0; i < k; i++) {
    cin >> a >> b >> c;
    if (a == 1) {
      unite(b, c);
      if (flag) {
        cout << i + 1 << endl;
        return 0;
      }
    } else if (a == 2) {
      b = find(b);
      if (id[b] != -1 && id[b] != c) {
        cout << i + 1 << endl;
        return 0;
      }
      id[b] = c;
    }
  }
  cout << 0 << endl;
  return 0;
}
Problem p00316 (C++) — buggy submission, verdict: Runtime Error

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

int n, m, k;

vector<int> v;

void init(int n) {
  for (int i = 0; i <= n; ++i) {
    v.push_back(i);
  }
}

int root(int x) {
  if (v[x] == x)
    return x;
  return v[x] = root(v[x]);
}

bool same(int a, int b) {
  if (root(a) > root(b))
    swap(a, b);
  int ar = root(a);
  int br = root(b);

  if (ar > n && ar != br) {
    return 1;
  }

  v[ar] = br;
  return 0;
}

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

  init(n + m);

  for (int i = 1; i <= k; ++i) {
    int a, b, c;
    cin >> a >> b >> c;

    if (a == 2) {
      c += n;
    }

    if (same(b, c)) {
      cout << i << endl;
      return 0;
    }
  }

  cout << 0 << endl;
}

Error: 0

Fixed submission:

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

int n, m, k;

vector<int> v;

void init(int n) {
  v.resize(n);
  for (int i = 0; i <= n; ++i) {
    v[i] = i;
  }
}

int root(int x) {
  if (v[x] == x)
    return x;
  return v[x] = root(v[x]);
}

bool same(int a, int b) {
  if (root(a) > root(b))
    swap(a, b);
  int ar = root(a);
  int br = root(b);

  if (ar > n && ar != br) {
    return 1;
  }

  v[ar] = br;
  return 0;
}

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

  init(n + m);

  for (int i = 1; i <= k; ++i) {
    int a, b, c;
    cin >> a >> b >> c;

    if (a == 2) {
      c += n;
    }

    if (same(b, c)) {
      cout << i << endl;
      return 0;
    }
  }

  cout << 0 << endl;
}
Problem p00316 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
typedef pair<int, int> P;

#define pb push_back
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define dep(i, a, n) for (int i = (a); i >= (n); i--)
#define mod 1e9 + 7

__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int s[100001], v[100001], r[100001];

void init() {
  rep(i, 0, 100001) {
    s[i] = i;
    r[i] = 0;
  }
}

int find(int x) {
  if (s[x] == x) {
    return x;
  } else {
    return s[x] = find(s[x]);
  }
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (r[x] < r[y]) {
    s[x] = y;
    if (v[x])
      v[y] = v[x];
  } else {
    s[y] = x;
    if (r[x] == r[y])
      r[x]++;
    if (v[y])
      v[x] = v[y];
  }
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int a[200001], b[200001], c[200001];
  init();
  rep(i, 0, k) cin >> a[i] >> b[i] >> c[i];
  rep(i, 0, k) {
    if (a[i] == 1) {
      int bb = find(b[i]);
      int cc = find(c[i]);
      if (v[bb] && v[cc] && v[bb] != v[cc]) {
        cout << i + 1 << endl;
        return 0;
      }
      unite(bb, cc);
    } else {
      int bb = find(b[i]);
      if (v[bb] && v[bb] != c[i]) {
        cout << i + 1 << endl;
        return 0;
      }
      v[bb] = c[i];
    }
  }

  cout << 0 << endl;
}

Error: 0

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> P;

#define pb push_back
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define dep(i, a, n) for (int i = (a); i >= (n); i--)
#define mod 1e9 + 7

__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int s[100001], v[100001], r[100001];

void init() {
  rep(i, 0, 100001) {
    s[i] = i;
    r[i] = 0;
  }
}

int find(int x) {
  if (s[x] == x) {
    return x;
  } else {
    return s[x] = find(s[x]);
  }
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (r[x] < r[y]) {
    s[x] = y;
    if (v[x])
      v[y] = v[x];
  } else {
    s[y] = x;
    if (r[x] == r[y])
      r[x]++;
    if (v[y])
      v[x] = v[y];
  }
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int a[100001], b[100001], c[100001];
  init();
  rep(i, 0, k) cin >> a[i] >> b[i] >> c[i];
  rep(i, 0, k) {
    if (a[i] == 1) {
      int bb = find(b[i]);
      int cc = find(c[i]);
      if (v[bb] && v[cc] && v[bb] != v[cc]) {
        cout << i + 1 << endl;
        return 0;
      }
      unite(bb, cc);
    } else {
      int bb = find(b[i]);
      if (v[bb] && v[bb] != c[i]) {
        cout << i + 1 << endl;
        return 0;
      }
      v[bb] = c[i];
    }
  }

  cout << 0 << endl;
}
Problem p00317 (C++) — buggy submission, verdict: Runtime Error

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

int solve(const auto &word, auto slate) {
  auto s = lower_bound(begin(word), end(word), slate);
  if (s == end(word))
    return 0;
  else
    return (*s == slate) ? 1 : 0;
}

int star(const auto &word, auto slate) {
  slate.pop_back();
  auto b = lower_bound(begin(word), end(word), slate);
  auto e = upper_bound(begin(word), end(word), slate + "~");
  return e - b;
}

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

  vector<string> word(N);
  for (auto &i : word)
    cin >> i;
  sort(begin(word), end(word));

  auto rev = word;
  for (auto &i : rev)
    reverse(begin(i), end(i));
  sort(begin(rev), end(rev));

  for (auto i = 0; i < M; ++i) {
    string slate;
    cin >> slate;

    const auto *w = &word;
    if (slate.front() == '*') {
      reverse(begin(slate), end(slate));
      w = &rev;
    }

    int q;
    for (q = 0; q < slate.size(); ++q)
      if (slate[q] == '?')
        break;

    auto res = 0;

    if (q == slate.size()) {
      if (slate.back() == '*')
        res = star(*w, slate);
      else
        res = solve(*w, slate);
    } else {
      for (auto c = 'a'; c <= 'z'; ++c) {
        slate[q] = c;
        if (slate.back() == '*')
          res += star(*w, slate);
        else
          res += solve(*w, slate);
      }
    }

    cout << res << endl;
  }
}

Error: 0

Fixed submission:

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

pair<int, int> solve(const vector<string> &word, string slate, int b, int e,
                     int i) {
  if (slate.empty())
    return make_pair(b, e);
  int l = lower_bound(begin(word) + b, begin(word) + e, slate,
                      [&](auto e, auto v) { return e < v.substr(i); }) -
          begin(word);
  slate += "z";
  auto h = upper_bound(begin(word) + b, begin(word) + e, slate,
                       [&](auto v, auto e) { return v.substr(i) < e; }) -
           begin(word);
  return make_pair(l, h);
}

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

  vector<string> word(N);
  for (auto &i : word)
    cin >> i;
  sort(begin(word), end(word));

  auto rev = word;
  for (auto &i : rev)
    reverse(begin(i), end(i));
  sort(begin(rev), end(rev));

  for (auto i = 0; i < M; ++i) {
    string slate;
    cin >> slate;

    auto *w = &word;
    if (slate.front() == '*') {
      w = &rev;
      reverse(begin(slate), end(slate));
    }

    int q;
    for (q = 0; q < slate.size(); ++q)
      if (slate[q] == '?')
        break;

    auto former = slate.substr(0, q);
    if (former.back() == '*')
      former.pop_back();

    auto p = solve(*w, former, 0, w->size(), 0);

    if (q != slate.size()) {
      auto latter = slate.substr(q + 1);
      if (latter.back() == '*')
        latter.pop_back();

      p = solve(*w, latter, p.first, p.second, q + 1);
    }

    if (slate.back() == '*')
      cout << p.second - p.first << endl;
    else
      cout << min(1, p.second - p.first) << endl;
  }
}
Problem p00317 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int N, M, K, ans, qpt, pt, pt2;
string s, temp, w;
string we[500000], ww[500000];
void search() {
  if (s[s.size() - 1] == '*') {
    pt = lower_bound(ww, ww + N, temp) - ww;
    temp.push_back('z' + 1);
    pt2 = lower_bound(ww, ww + N, temp) - ww;
    ans += pt2 - pt;
    temp.erase(temp.end() - 1);
  } else if (s[0] == '*') {
    reverse(temp.begin(), temp.end());
    pt = lower_bound(we, we + N, temp) - we;
    temp.push_back('z' + 1);
    pt2 = lower_bound(we, we + N, temp) - we;
    ans += pt2 - pt;
    temp.erase(temp.end() - 1);
    reverse(temp.begin(), temp.end());
  } else {
    pt = lower_bound(ww, ww + N, temp) - ww;
    if (ww[pt] == temp)
      ans++;
  }
}
int main() {
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    cin >> w;
    ww[i] = w;
    reverse(w.begin(), w.end());
    we[i] = w;
  }
  sort(ww, ww + N);
  sort(we, we + N);
  for (int i = 0; i < M; i++) {
    cin >> s;
    qpt = -1;
    ans = 0;
    temp.clear();
    for (int j = 0; j < s.size(); j++) {
      if (s[j] != '*')
        temp.push_back(s[j]);
      if (s[j] == '?')
        qpt = temp.size() - 1;
    }
    if (qpt >= 0) {
      for (char j = 'a'; j <= 'z'; j++) {
        temp[qpt] = j;
        search();
      }
    } else {
      search();
    }
    cout << ans << endl;
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int N, M, K, ans, qpt, pt, pt2;
char w[50000][500];
string s, temp;
char *we[501][50000], *ww[50000];
void search() {
  if (s[0] != '*') {
    pt = lower_bound(ww, ww + N, temp) - ww;
    temp[temp.size() - 1]++;
    pt2 = lower_bound(ww, ww + N, temp) - ww;
    ans += pt2 - pt;
  }

  else {
    pt = lower_bound(we[temp.size()], we[temp.size()] + N, temp) -
         we[temp.size()];
    temp[temp.size() - 1]++;
    pt2 = lower_bound(we[temp.size()], we[temp.size()] + N, temp) -
          we[temp.size()];
    ans += pt2 - pt;
  }
}
int main() {
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    scanf("%s", w[i]);
    for (int j = strlen(w[i]); j >= 0; j--) {
      we[j][i] = w[i] + strlen(w[i]) - j;
    }
    ww[i] = w[i];
  }
  for (int i = 0; i < M; i++) {
    cin >> s;
    qpt = -1;
    ans = 0;
    temp.clear();
    for (int j = 0; j < s.size(); j++) {
      if (s[j] != '*')
        temp.push_back(s[j]);
      if (s[j] == '?')
        qpt = temp.size() - 1;
    }
    if (qpt >= 0) {
      for (char j = 'a'; j <= 'z'; j++) {
        temp[qpt] = j;
        search();
      }
    } else {
      search();
    }
    cout << ans << endl;
  }
}
Problem p00318 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i > 0; --i)
#define yesno(flg)                                                             \\
  if (flg) {                                                                   \\
    cout << "Yes" << endl;                                                     \\
  } else {                                                                     \\
    cout << "No" << endl;                                                      \\
  }
#define Vi vector<int>
#define Vd vector<double>
#define Vl vector<long long>
#define pb(a) push_back(a)
using namespace std;

typedef long long ll;
const int inf = 1000000000;
const int np = 1000000007;
struct edge {
  int myc, to, c, cost;
};
struct pos {
  int to, cost, color;
};
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

Vd x(100000);
Vd r(100000);
int n;
double rec(double high, double low) {
  if (high - low < 0.0001)
    return high;
  double maxp = 1000000;
  double minp = 0;
  double mid = (high + low) / 2;
  rep(i, n) {
    double s = r[i] * r[i] - mid * mid;
    s = sqrt(s);
    maxp = min(maxp, x[i] + s);
    minp = max(minp, x[i] - s);
  }
  if (maxp < minp) {
    return rec(mid, low);
  } else {
    return rec(high, mid);
  }
}
int main() {
  cin >> n;
  double minr = 1000000;
  rep(i, n) {
    cin >> x[i] >> r[i];
    minr = min(minr, r[i]);
  }

  printf("%0.4f\\n", rec(minr, 0));
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i > 0; --i)
#define yesno(flg)                                                             \\
  if (flg) {                                                                   \\
    cout << "Yes" << endl;                                                     \\
  } else {                                                                     \\
    cout << "No" << endl;                                                      \\
  }
#define Vi vector<int>
#define Vf vector<float>
#define Vl vector<long long>
#define pb(a) push_back(a)
using namespace std;

typedef long long ll;
const int inf = 1000000000;
const int np = 1000000007;
struct edge {
  int myc, to, c, cost;
};
struct pos {
  int to, cost, color;
};
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

Vf x(100000);
Vf r(100000);
int n;
float rec(float h, float l) {
  if (h - l < 0.001)
    return h;
  float maxp = 1000000.0f;
  float minp = 0.0f;
  float mid = (h + l) / 2;
  rep(i, n) {
    float s = r[i] * r[i] - mid * mid;
    s = sqrt(s);
    maxp = min(maxp, x[i] + s);
    minp = max(minp, x[i] - s);
    if (maxp < minp)
      break;
  }
  if (maxp < minp) {
    return rec(mid, l);
  } else {
    return rec(h, mid);
  }
}
int main() {
  cin >> n;
  float minr = 1000000;
  rep(i, n) {
    cin >> x[i] >> r[i];
    minr = min(minr, r[i]);
  }

  cout << rec(minr, 0) << endl;
  return 0;
}