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

#include <algorithm>
#include <iostream>
using namespace std;
int n, m, a[55], b[55], c[55], dp[55][55][55][55];
bool vis[55][55][55][55];
int rec(int pos, int rem, int lv2, int lv1) {
  if (pos == n)
    return 0;
  if (vis[pos][rem][lv2][lv1])
    return dp[pos][rem][lv2][lv1];
  int ret = -999999999;
  for (int i = 0; i <= rem; i++) {
    int res = rec(pos + 1, rem - i, i, lv2);
    int cs = c[pos];
    for (int j = 1; j <= 8; j++) {
      for (int k = 0; k <= j; k++) {
        int l = j - k;
        if (k <= lv2 && l <= lv1) {
          cs = max(cs, a[pos] * k + b[pos] * l);
        }
      }
    }
    ret = max(ret, res + cs);
  }
  vis[pos][rem][lv2][lv1] = true;
  return dp[pos][rem][lv2][lv1] = ret;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i] >> c[i];
  int ret = -999999999;
  for (int i = 0; i <= m; i++)
    ret = max(ret, rec(0, m - i, i, 0));
  cout << ret << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int n, m, a[55], b[55], c[55], dp[55][55][55][55];
bool vis[55][55][55][55];
int rec(int pos, int rem, int lv2, int lv1) {
  if (pos == n)
    return 0;
  if (vis[pos][rem][lv2][lv1])
    return dp[pos][rem][lv2][lv1];
  int ret = -999999999;
  for (int i = 0; i <= rem; i++) {
    int res = rec(pos + 1, rem - i, i, lv2);
    int cs = c[pos];
    for (int j = 1; j <= 8; j++) {
      for (int k = 0; k <= j; k++) {
        int l = j - k;
        if (k <= lv2 && l <= lv1) {
          cs = max(cs, a[pos] * k + b[pos] * l);
        }
      }
    }
    ret = max(ret, res + cs);
  }
  return dp[pos][rem][lv2][lv1] = ret;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i] >> c[i];
  int ret = -999999999;
  for (int i = 0; i <= m; i++)
    ret = max(ret, rec(0, m - i, i, 0));
  cout << ret << endl;
  return 0;
}
Problem p01538 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int main() {

  int n;

  scanf("%d", &n);
  int x;
  for (int i = 0; i < n; i++) {
    scanf("%d", &x);
    int c = 0;
    int d = 10;
    int w = 0, y = 0;
    int max = 0;
    while (x / 10 != 0) {
      for (int j = 0; j < 6; j++) {
        w = x / d;
        y = x % d;
        d *= 10;
        if (j == 0)
          max = w * y;
        else if (w * y > max)
          max = w * y;
      }
      x = max;
      c++;
      d = 10;
    }
    printf("%d\\n", c);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int main() {

  int n;
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    int x;
    for (int i = 0; i < n; i++) {
      scanf("%d", &x);
      int c = 0;
      int d = 10;
      int w = 0, y = 0;
      int max = 0;
      while (x / 10 != 0) {
        for (int j = 0; j < 6; j++) {
          w = x / d;
          y = x % d;
          d *= 10;
          if (j == 0)
            max = w * y;
          else if (w * y > max)
            max = w * y;
        }
        x = max;
        c++;
        d = 10;
      }
      printf("%d\\n", c);
    }
  }
  return 0;
}
Problem p01538 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF 100000000
#define EPS 1e-9

using namespace std;

typedef long long ll;

vector<int> ans_list;

bool dp[1000001];

int main() {

  int Q;
  cin >> Q;

  memset(dp, 0, sizeof(dp));

  while (Q) {
    Q--;
    int N;
    cin >> N;
    int ans = 0;
    memset(dp, 0, sizeof(dp));

    while (N / 10 > 0) {
      if (dp[N]) {
        ans = -1;
        break;
      }
      int tmp = 0;
      for (int i = 1; N / pow(10, i) > 0; i++) {
        tmp = max(tmp, (N / (int)pow(10, i)) * (N % (int)pow(10, i)));
      }
      dp[N] = false;
      N = tmp;
      ans++;
    }

    ans_list.push_back(ans);
  }

  for (int i = 0; i < ans_list.size(); i++) {
    cout << ans_list[i] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF 100000000
#define EPS 1e-9

using namespace std;

typedef long long ll;

vector<int> ans_list;

bool dp[1000000];

int main() {

  int Q;
  cin >> Q;

  memset(dp, 0, sizeof(dp));

  while (Q) {
    Q--;
    int N;
    cin >> N;
    int ans = 0;
    memset(dp, 0, sizeof(dp));

    while (N / 10 > 0) {
      if (dp[N]) {
        ans = -1;
        break;
      }
      int tmp = 0;
      for (int i = 1; N / pow(10, i) > 0; i++) {
        tmp = max(tmp, (N / (int)pow(10, i)) * (N % (int)pow(10, i)));
      }
      dp[N] = false;
      N = tmp;
      ans++;
    }

    ans_list.push_back(ans);
  }

  for (int i = 0; i < ans_list.size(); i++) {
    cout << ans_list[i] << endl;
  }

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

#include <cstdlib>
#include <iostream>
#include <set>
#include <sstream>
using namespace std;

int main() {
  int q;
  cin >> q;
  while (q--) {
    string s;
    cin >> s;
    int size = s.size();
    int res, ans = 0;
    bool f = true;
    set<int> set1;
    set1.insert(atoi(s.c_str()));
    while (size > 1) {
      res = 0;
      for (int i = 0; i < size - 1; i++) {
        string a = s.substr(0, i + 1);
        string b = s.substr(i + 1);
        res = max(atoi(a.c_str()) * atoi(b.c_str()), res);
      }
      if (set1.count(res)) {
        f = false;
        break;
      }
      set1.insert(res);
      stringstream ss;
      ss << res;
      s = ss.str();
      size = s.size();
      ans++;
    }
    cout << (f ? ans : -1) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <sstream>
using namespace std;

int main() {
  int q;
  cin >> q;
  while (q--) {
    string s;
    cin >> s;
    int size = s.size();
    int res, ans = 0;
    while (size > 1) {
      res = 0;
      for (int i = 0; i < size - 1; i++) {
        string a = s.substr(0, i + 1);
        string b = s.substr(i + 1);
        res = max(atoi(a.c_str()) * atoi(b.c_str()), ans);
      }
      stringstream ss;
      ss << res;
      s = ss.str();
      size = s.size();
      ans++;
    }
    cout << ans << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

int Q, N, logn, r, l, turn;

int main() {
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> N;
    if (N == 0)
      logn = 1;
    else
      logn = int(log10(N)) + 1; // N????????°?????????
    if (logn == 1)
      cout << 0 << endl;
    else {
      turn = 0;
      while (logn != 1) {
        int max = 0;
        for (int j = 0; j < logn - 1; j++) {
          r = N % int(pow(10, j + 1) + 0.1);
          l = N / int(pow(10, j + 1) + 0.1);
          if (max < r * l) {
            max = r * l;
          }
        }
        N = max;
        if (N == 0)
          logn = 1;
        else
          logn = int(log10(N)) + 1; // N????????°?????????
        turn++;
      }
      cout << turn << endl;
    }
  }
  return 0;
}

Error: 2

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

int Q, N, logn, r, l, turn;

int main() {
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> N;
    logn = int(log10(N)) + 1; // N????????°?????????
    if (logn == 1)
      cout << 0 << endl;
    else {
      turn = 0;
      while (logn != 1) {
        int max = 0;
        for (int j = 0; j < logn - 1; j++) {
          r = N % int(pow(10, j + 1) + 0.1);
          l = N / int(pow(10, j + 1) + 0.1);
          if (max < r * l) {
            max = r * l;
          }
        }
        N = max;
        logn = int(log10(N)) + 1; // N????????°?????????
        turn++;
      }
      return turn;
    }
  }
  return 0;
}
Problem p01538 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int num, cnt = 0;
    cin >> num;
    while (1) {
      int max = 0;
      if (num / 10 == 0)
        break;
      for (int j = 10; num / j > 0; j *= 10) {
        int a = num / j, b = num % j;
        if (a * b > max)
          max = a * b;
      }
      num = max;
      cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MAX 7777777
int main() {
  int n, m, t = 0;
  bool a[MAX];
  while (cin >> n >> m, n) {
    t = 0;
    memset(a, false, sizeof(a));
    for (int j = n; j < MAX; j++) {
      if (!a[j]) {
        for (int k = j; k < MAX; k += j) {
          a[k] = true;
        }
        t++;
      }
      if (m == 1)
        break;
      if (m != 1 && t == m)
        break;
    }
    for (int i = n; i < MAX; i++) {
      if (!a[i]) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p01538 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;

int main(void) {
  int q, n;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> n;
    int cnt = 0;
    while (n >= 10) {
      int tmp = -1;
      string ns = to_string(n);
      for (int i = 0; i < ns.size() - 1; i++) {
        // cerr << ns.substr(0, i+1) << " " << ns.substr(i+1) << endl;
        tmp = max(tmp, stoi(ns.substr(0, i + 1)) * stoi(ns.substr(i + 1)));
      }
      // cerr << tmp << endl;
      n = tmp;
      cnt++;
    }
    cout << cnt << endl;
  }

  return 0;
}

// EOF

Error: 0
9 9
8 1
1 23
12 3
3 6
1 8


Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;

int main(void) {
  int q, n;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> n;
    int cnt = 0;
    while (n >= 10) {
      int tmp = -1;
      string ns = to_string(n);
      for (int i = 0; i < ns.size() - 1; i++) {
        cerr << ns.substr(0, i + 1) << " " << ns.substr(i + 1) << endl;
        tmp = max(tmp, stoi(ns.substr(0, i + 1)) * stoi(ns.substr(i + 1)));
      }
      // cerr << tmp << endl;
      n = tmp;
      cnt++;
    }
    cout << cnt << endl;
  }

  return 0;
}

// EOF
Problem p01538 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
int e, n, c, k, i;
using namespace std;
int main() {
  cin >> e;
  while (e--) {
    cin >> n;
    c = 0;
    while (n > 9) {
      int m = 0, b = 1;
      for (c++, i = 0; i < 5; i++) {
        b *= 10;
        k = (n / b) * (n % b);
        if (k > m)
          m = k;
      }
      n = m;
    }
    cout << c << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a, b, i, c, p, w, x, m, j, k, t;
  cin >> n;
  while (n--) {
    cin >> a;
    c = 0;
    while (1) {
      for (m = 0, t = b = a, i = -1; a; a /= 10)
        i++;
      if (!i)
        break;
      for (a = b, j = 1; j <= i; j++) {
        w = 0;
        a = b = t;
        for (k = 0; k != j; k++) {
          int o = 1;
          p = a % 10;
          for (x = 1; x <= k; x++)
            o *= 10;
          w += o * p;
          a /= 10;
        }
        while (x--)
          b /= 10;
        if (w)
          m = max(m, w * b);
        else
          m = b;
      }
      a = m;
      c++;
    }
    cout << c << endl;
  }
}
Problem p01538 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
#define pb push_back
#define mt make_tuple
#define all(a) (a).begin(), (a).end()
#define frep(i, a, n) for (int i = (a); i < (n); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define debug(x) cout << #x << " : " << x << " ------------ " << endl;
using namespace std;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n, count = 0;
    cin >> n;
    map<int, bool> h;
    while (1) {
      if (floor(n / 10) == 0)
        break;
      string s = to_string(n);
      vector<int> p;
      frep(i, 1, s.length()) {
        int tmp = stoi(s.substr(0, i)) * stoi(s.substr(i));
        p.pb(tmp);
        if (tmp == 0)
          break;
      }
      int m = *max_element(all(p));
      if (h[m]) {
        count = -1;
        break;
      }
      n = m;
      h[m] = true;
      ++count;
    }

    cout << count << endl;
  }
  return EXIT_SUCCESS;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
#define pb push_back
#define mt make_tuple
#define all(a) (a).begin(), (a).end()
#define frep(i, a, n) for (int i = (a); i < (n); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define debug(x) cout << #x << " : " << x << " ------------ " << endl;
using namespace std;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n, count = 0;
    cin >> n;
    map<int, bool> h;
    while (1) {
      if (floor(n / 10) == 0)
        break;
      string s = to_string(n);
      vector<int> p;
      frep(i, 1, s.length()) {
        int tmp = stoi(s.substr(0, i)) * stoi(s.substr(i));
        if (tmp == 0)
          break;
        p.pb(tmp);
      }
      int m = *max_element(all(p));
      if (h[m]) {
        count = -1;
        break;
      }
      n = m;
      h[m] = true;
      ++count;
    }

    cout << count << endl;
  }
  return EXIT_SUCCESS;
}
Problem p01539 (C++) — buggy submission, verdict: Time Limit Exceeded

// 16
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <queue>

using namespace std;

struct S {
  int x, y, t, p;
  bool operator<(S a) const { return p > a.p; }
};

int main() {
  int sx, sy, gx, gy;
  int n;
  cin >> sx >> sy >> gx >> gy >> n;
  bool f[201][201] = {};
  while (n--) {
    int x, y;
    cin >> x >> y;
    f[y + 100][x + 100] = true;
  }
  int lx, ly;
  cin >> lx >> ly;
  int p[201][201][6];
  fill(p[0][0], p[201][0], 1 << 30);
  priority_queue<S> que;
  S is = {sx, sy, 0, 0};
  que.push(is);
  while (!que.empty()) {
    S c = que.top();
    que.pop();
    if (p[c.y + 100][c.x + 100][c.t] <= c.p)
      continue;
    p[c.y + 100][c.x + 100][c.t] = c.p;
    int dy[][7] = {{1, 0, -1, -1, -1, 0, 0}, {1, 1, 0, -1, 0, 1, 0}};
    int dx[] = {0, 1, 1, 0, -1, -1, 0};
    for (int l = 0; l < 7; l++) {
      int ny = c.y + dy[abs(c.x) % 2][l];
      int nx = c.x + dx[l];
      if (-ly <= ny && ny <= ly && -lx <= nx && nx <= lx &&
          !f[ny + 100][nx + 100]) {
        S n = {nx, ny, (c.t + 1) % 6, c.p + (abs(c.x * c.y * c.t) % 6 != l)};
        que.push(n);
      }
    }
  }
  int r = *min_element(p[gy + 100][gx + 100], p[gy + 100][gx + 101]);
  cout << ((r != 1 << 30) ? r : -1) << endl;
  return 0;
}

Error: TLE

Fixed submission:

// 16
#include <algorithm>
#include <cstdlib>
#include <iostream>

using namespace std;

int main() {
  int sx, sy, gx, gy;
  int n;
  cin >> sx >> sy >> gx >> gy >> n;
  bool f[201][201] = {};
  while (n--) {
    int x, y;
    cin >> x >> y;
    f[y + 100][x + 100] = true;
  }
  int lx, ly;
  cin >> lx >> ly;
  int dp[201][201][6];
  fill(dp[0][0], dp[201][0], 1 << 30);
  dp[sy + 100][sx + 100][0] = 0;
  for (;;) {
    bool u = false;
    for (int t = 0; t < 6; t++) {
      for (int i = -ly; i <= ly; i++) {
        for (int j = -lx; j <= lx; j++) {
          for (int k = 0; k < 6; k++) {
            static int dy[][7] = {{1, 0, -1, -1, -1, 0, 0},
                                  {1, 1, 0, -1, 0, 1, 0}};
            static int dx[] = {0, 1, 1, 0, -1, -1, 0};
            for (int l = 0; l < 7; l++) {
              int ny = i + dy[abs(j) % 2][l];
              int nx = j + dx[l];
              if (-ly <= ny && ny <= ly && -lx <= nx && nx <= lx &&
                  !f[ny + 100][nx + 100]) {
                int n = dp[i + 100][j + 100][t] + (abs(i * j * t) % 6 != l);
                if (dp[ny + 100][nx + 100][(t + 1) % 6] > n) {
                  dp[ny + 100][nx + 100][(t + 1) % 6] = n;
                  u = true;
                }
              }
            }
          }
        }
      }
    }
    if (!u)
      break;
  }
  int r = *min_element(dp[gy + 100][gx + 100], dp[gy + 100][gx + 101]);
  cout << ((r != 1 << 30) ? r : -1) << endl;
  return 0;
}
Problem p01540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2426&lang=jp
using namespace std;
typedef pair<int, int> pii;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> treasure_x(n), x(n);
  vector<int> treasure_y(n), y(n);

  for (int i = 0; i < n; i++) {
    cin >> treasure_x[i] >> treasure_y[i];
    x[i] = treasure_x[i];
    y[i] = treasure_y[i];
  }

  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  y.erase(unique(y.begin(), y.end()), y.end());

  vector<vector<int>> dp(n + 2, vector<int>(n + 2, 0));
  for (int i = 0; i < n; i++) {
    int xind = lower_bound(x.begin(), x.end(), treasure_x[i]) - x.begin();
    int yind = lower_bound(y.begin(), y.end(), treasure_y[i]) - y.begin();
    dp[xind + 1][yind + 1]++;
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dp[i + 1][j + 1] += dp[i][j + 1] + dp[i + 1][j] - dp[i][j];
    }
  }

  for (int i = 0; i < m; i++) {
    int ans = 0;
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    int x1ind = lower_bound(x.begin(), x.end(), x1) - x.begin();
    int y1ind = lower_bound(y.begin(), y.end(), y1) - y.begin();
    int x2ind = lower_bound(x.begin(), x.end(), x2 + 1) - x.begin();
    int y2ind = lower_bound(y.begin(), y.end(), y2 + 1) - y.begin();

    cout << dp[x2ind][y2ind] - dp[x2ind][y1ind] - dp[x1ind][y2ind] +
                dp[x1ind][y1ind]
         << endl;
  }
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2426&lang=jp
using namespace std;
typedef pair<int, int> pii;

int main() {
  // cin.tie(0); ios::sync_with_stdio(false);
  int n, m;
  scanf("%d %d", &n, &m); // cin >> n >> m;
  vector<pii> treasure(n);
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &treasure[i].first, &treasure[i].second);
    // cin >> treasure[i].first >> treasure[i].second;
  }
  for (int i = 0; i < m; i++) {
    int ans = 0;
    int x1, y1, x2, y2;
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
    for (int j = 0; j < n; j++) {
      if (treasure[j].first < x1)
        continue;
      if (treasure[j].first > x2)
        continue;
      if (treasure[j].second < y1)
        continue;
      if (treasure[j].second > y2)
        continue;
      ans++;
    }
    printf("%d\\n", ans);
  }
}
Problem p01540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = int(s); i <= int(e); i++)
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define INF 1000000001

int s[5010][5010];

int main() {

  int n, m, x1, y1, x2, y2, x3, y3;
  vector<int> x, y, X, Y;

  cin >> n >> m;

  rep(i, n) {

    cin >> x1 >> y1;

    x.push_back(x1);
    y.push_back(y1);
  }

  X = x;
  Y = y;

  x.push_back(-INF);
  x.push_back(INF);
  y.push_back(-INF);
  y.push_back(INF);

  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  y.erase(unique(y.begin(), y.end()), y.end());

  rep(i, n) {

    int dx = lower_bound(x.begin(), x.end(), X[i]) - x.begin();
    int dy = lower_bound(y.begin(), y.end(), Y[i]) - y.begin();

    s[dy][dx]++;
  }

  REP(i, 1, n)
  REP(j, 1, n) s[i][j] += s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1];

  rep(i, m) {

    cin >> x2 >> y2 >> x3 >> y3;

    int dx2 = lower_bound(x.begin(), x.end(), x2) - x.begin();
    int dy2 = lower_bound(y.begin(), y.end(), y2) - y.begin();
    int dx3 = upper_bound(x.begin(), x.end(), x3) - x.begin() - 1;
    int dy3 = upper_bound(y.begin(), y.end(), y3) - y.begin() - 1;

    cout << s[dy3][dx3] - s[dy2 - 1][dx3] - s[dy3][dx2 - 1] +
                s[dy2 - 1][dx2 - 1]
         << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = int(s); i <= int(e); i++)
#define rep(i, n) for (int i = 0; i < int(n); i++)

int main() {

  int n, m, x1, y1, x2, y2, x3, y3;

  vector<pair<int, int>> p;
  vector<pair<int, int>> sw;
  vector<pair<int, int>> ne;

  cin >> n >> m;

  rep(i, n) {

    cin >> x1 >> y1;

    p.push_back(make_pair(x1, y1));
  }

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

  vector<int> c(m, 0);
  rep(i, m) {

    cin >> x2 >> y2 >> x3 >> y3;

    rep(j, n) {

      if (p[j].first < x2)
        continue;
      if (x2 <= p[j].first && p[j].first <= x3 && y2 <= p[j].second &&
          p[j].second <= y3)
        c[i]++;
      if (x3 < p[j].first)
        break;
    }
  }

  rep(i, m) cout << c[i] << endl;

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

#include <stdio.h>
#include <stdlib.h>

typedef struct pair {
  int x;
  int y;
} pair;

int cmpx(const void *a, const void *b) {
  pair *c1 = (pair *)a;
  pair *c2 = (pair *)b;
  return c1->x - c2->x;
}
int main(void) {
  int sx, sy, gx, gy, n, m, count, i, j;
  pair t[5000];
  scanf("%d%d", &n, &m);
  for (i = 0; i < n; i++)
    scanf("%d%d", &t[i].x, &t[i].y);
  qsort(t, n, sizeof(t[0]), cmpx);
  for (i = 0; i < m; i++) {
    scanf("%d%d%d%d", &sx, &sy, &gx, &gy);
    count = 0;
    for (j = 0; j < n && t[j].x <= gx; j++)
      if (sx <= t[j].x && sy <= t[j].y && gx >= t[j].x && gy >= t[j].y)
        count++;
    printf("%d\\n", count);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int main(void) {
  int tx[5000], ty[5000], sx, sy, gx, gy, n, m, count, i, j;
  scanf("%d%d", &n, &m);
  for (i = 0; i < n; i++)
    scanf("%d%d", &tx[i], &ty[i]);
  for (i = 0; i < m; i++) {
    scanf("%d%d%d%d", &sx, &sy, &gx, &gy);
    count = 0;
    for (j = 0; j < n; j++)
      if (sx <= tx[j] && sy <= ty[j] && gx >= tx[j] && gy >= ty[j])
        count++;
    printf("%d\\n", count);
  }
  return 0;
}