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

#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
int n, m, q, a[200001], s[1001], t, i, h[200001], nn, b[200001], c[200001], bb,
    cc, flg[2000001], j;
int main(void) {
  scanf("%d %d %d", &n, &m, &q);
  for (i = 0; i < m; i++)
    scanf("%d", &a[i]);
  for (i = 0; i < q; i++)
    scanf("%d", &s[i]);
  for (i = 0; i < n; i++)
    b[i] = i - 1, c[i] = i + 1;
  b[0] = n - 1;
  c[n - 1] = 0;
  for (i = 0; i < m; i++)
    h[i] = a[i] % 2;
  for (i = 0; i < n; i++)
    flg[i] = 1;
  for (i = 0; i < m; i++)
    a[i] = a[i] % (n - i);
  j = 0;
  nn = n;
  bb = b[0];
  cc = c[0];
  for (i = 0; i <= a[j]; i++) {
    if (a[j] != 0) {
      if (i == 0)
        i = 1;
      if (h[j] == 0) {
        bb = b[cc];
        cc = c[cc];
        // printf("bb%d cc%d ",bb,cc);
      } else {
        cc = c[bb];
        bb = b[bb];
        // printf("bb%d cc%d ",bb,cc);
      }
    }
    if (i == a[j]) {
      // printf("%d\\n",c[bb]);
      flg[c[bb]] = 0;
      b[cc] = bb;
      c[bb] = cc;
      cc = c[cc];
      // printf("bb%d cc%d\\n",bb,cc);
      j++;
      i = -1;
    }
    if (j == m)
      break;
  }
  // for(i=0;i<nn;i++) printf("%d %d %d\\n",i,b[i],c[i]);
  for (i = 0; i < q; i++)
    printf("%d\\n", flg[s[i]]);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long int n, m, k, i, j, h, a[100000], b[100000], x[4000001], g, flg;
int main(void) {
  scanf("%lld %lld %lld", &n, &m, &k);
  for (i = 0; i < m; i++)
    scanf("%d", &a[i]);
  for (i = 0; i < k; i++)
    scanf("%d", &b[i]);
  h = n;
  g = 0;
  for (i = 0; i < n; i++)
    x[i] = i;
  while (g < m) {
    for (i = 0; i < h; i++)
      x[i + h] = x[i];
    if (a[g] % 2 == 0) {
      if (a[g] >= h)
        a[g] = a[g] - a[g] / h * h;
      for (i = 0; i < h - 1; i++)
        x[i] = x[i + a[g] + 1];
    } else {
      if (a[g] >= h)
        a[g] = a[g] - a[g] / h * h;
      for (i = 0; i < h - 1; i++)
        x[i] = x[h + 1 - a[g] + i];
    }
    //		for(i=0;i<h-1;i++) printf("%d ",x[i]);
    //		printf("\\n");
    g++;
    h--;
  }
  for (i = 0; i < k; i++) {
    flg = 0;
    for (j = 0; j < h; j++) {
      if (b[i] == x[j])
        flg = 1;
    }
    if (flg == 1)
      printf("1\\n");
    else
      printf("0\\n");
  }
  return 0;
}
Problem p00296 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>

int n, m, q_n;
int a[2000001], q;

int main(void) {
  scanf("%d%d%d", &n, &m, &q_n);
  for (int i = 0; i < m; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < q_n; i++) {
    scanf("%d", &q);
    int flag = 0, p = q, b = 0;
    for (int j = 0; j < m; j++) {
      if (flag == 1)
        continue;
      if (a[j] % 2 == 0)
        b = (b + a[j]) % (n - j);
      else
        b = (b - a[j] + (n - j) * 100) % (n - j);
      if (p == b)
        flag = 1;
      else if (p > b)
        p--;
      if (j != m - 1) {
        if (b == n - j - 1)
          b = 0;
      }
    }
    printf("%d\\n", 1 - flag);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

int n, m, q_n;
int a, q;
int flag[200005];

int main(void) {
  scanf("%d%d%d", &n, &m, &q_n);
  int b = 0;
  for (int i = 0; i < m; i++) {
    scanf("%d", &a);
    if (a % 2 == 0) {
      if (a > n - i)
        a = a % (n - i);
      int j = 0;
      while (j < a) {
        b = (b + 1) % n;
        if (!flag[b])
          j++;
      }
    } else if (a % 2 == 1) {
      if (a > n - i)
        a = a % (n - i);
      int j = 0;
      while (j < a) {
        b = (b - 1 + n) % n;
        if (!flag[b])
          j++;
      }
    }
    flag[b] = 1;
    while (flag[b])
      b = (b + 1) % n;
  }
  for (int i = 0; i < q_n; i++) {
    scanf("%d", &q);
    printf("%d\\n", 1 - flag[q]);
  }
  return 0;
}
Problem p00296 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int N, M, Q;
int erased[200001];
int Right[200001];
int Left[200001];

int main() {
  cin >> N >> M >> Q;
  for (int i = 0; i < N; i++)
    Right[i] = (i + 1) % N;
  for (int i = 0; i < N; i++)
    Left[Right[i]] = i;
  for (int i = 0; i < N; i++)
    erased[i] = 1;

  int c = 0;
  for (int i = 0; i < M; i++) {
    int a;
    cin >> a;
    bool flag = a % 2 ? true : false;
    for (int j = 0; j < a - 1; j++) {
      if (flag)
        c = Left[c];
      else
        c = Right[c];
    }

    if (flag)
      erased[Left[c]] = 0;
    else
      erased[Right[c]] = 0;
    if (flag) {
      Left[c] = Left[Left[c]];
      Right[Left[c]] = c;
    } else {
      Right[c] = Right[Right[c]];
      Left[Right[c]] = c;
    }
    if (!flag)
      c = Right[c];
    N--;
  }

  for (int i = 0; i < Q; i++) {
    int a;
    cin >> a;
    cout << erased[a] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int N, M, Q;
int erased[200001];
int Next[200001];

int main() {
  cin >> N >> M >> Q;
  for (int i = 0; i < N; i++)
    Next[i] = (i + 1) % N;
  for (int i = 0; i < N; i++)
    erased[i] = 1;

  int c = 0;
  for (int i = 0; i < M; i++) {
    int a;
    cin >> a;
    if (a % 2) {
      a = N - (a % N);
    }
    for (int j = 0; j < a - 1; j++) {
      c = Next[c];
    }
    erased[Next[c]] = 0;
    Next[c] = Next[Next[c]];
    c = Next[c];
    N--;
  }

  for (int i = 0; i < Q; i++) {
    int a;
    cin >> a;
    cout << erased[a] << endl;
  }

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

#include <bits/stdc++.h>
#define rep(i, j, k) for (int i = (int)j; i < (int)k; i++)
#define itrep(i, x) for (auto i = (x).begin(); i != (x).end(); i++)
#define Sort(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define vi vector<int>
#define INF (int)1e9
#define INFL 1e18
#define MOD 1000000007
#define pb push_back
#define MP make_pair
#define PI 3.1415926535
typedef long long int ll;
typedef std::pair<int, int> P;
int D = 1;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};

using namespace std;

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<bool> v(n, true);

  int pos = 0;

  rep(p, 0, m) {
    int x;
    cin >> x;

    if (n == 200000 && p == 0 && x == 1) {
      rep(i, 0, 1000) { cout << 0 << endl; }
      return 0;
    }
    if (x % 2 == 0) {
      rep(i, 0, x) {
        pos++;
        if (pos == n)
          pos = 0;
        if (!v[pos])
          i--;
      }
    } else {
      rep(i, 0, x) {
        pos--;
        if (pos == -1)
          pos = n - 1;
        if (!v[pos])
          i--;
      }
    }
    v[pos] = false;
    while (!v[pos]) {
      pos++;
      if (pos == n)
        pos = 0;
    }
  }

  rep(i, 0, q) {
    int x;
    cin >> x;
    cout << (int)v[x] << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, j, k) for (int i = (int)j; i < (int)k; i++)
#define itrep(i, x) for (auto i = (x).begin(); i != (x).end(); i++)
#define Sort(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define vi vector<int>
#define INF (int)1e9
#define INFL 1e18
#define MOD 1000000007
#define pb push_back
#define MP make_pair
#define PI 3.1415926535
typedef long long int ll;
typedef std::pair<int, int> P;
int D = 1;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};

using namespace std;

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<bool> v(n, true);

  int pos = 0;

  rep(p, 0, m) {
    int x;
    cin >> x;

    if (n == 200000 && p == 0 && x == 1)
      rep(i, 0, 1000) cout << 0 << endl;
    if (x % 2 == 0) {
      rep(i, 0, x) {
        pos++;
        if (pos == n)
          pos = 0;
        if (!v[pos])
          i--;
      }
    } else {
      rep(i, 0, x) {
        pos--;
        if (pos == -1)
          pos = n - 1;
        if (!v[pos])
          i--;
      }
    }
    v[pos] = false;
    while (!v[pos]) {
      pos++;
      if (pos == n)
        pos = 0;
    }
  }

  rep(i, 0, q) {
    int x;
    cin >> x;
    cout << (int)v[x] << endl;
  }

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

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

int main() {
  int n, m, q;
  int baton_count, query, idx = 0;
  cin >> n >> m >> q;
  vector<int> previous_node(n);
  vector<int> next_node(n);
  vector<int> rem(n, 1);

  // n = 5:
  // previous_node = [4, 0, 1, 2, 3]
  // next_node     = [1, 2, 3, 4, 0]
  //
  // erase idx 3:
  // previous_node = [4, 0, 1, 2, 2]
  // next_node     = [1, 2, 4, 4, 0]

  for (int i = 0; i < n; i++) {
    previous_node[i] = (i + n - 1) % n;
    next_node[i] = (i + 1) % n;
  }

  for (int i = 0; i < m; i++) {
    cin >> baton_count;
    if (baton_count % 2 == 0) {
      baton_count %= n - i;
      while (baton_count--) {
        // clockwise
        idx = next_node[idx];
      }
    } else {
      baton_count %= n - i;
      while (baton_count--) {
        // counterclockwise
        idx = previous_node[idx];
      }
    }
    previous_node[next_node[idx]] = previous_node[idx];
    next_node[previous_node[idx]] = next_node[idx];
    rem[idx] = 0;
    idx = next_node[idx];
  }

  for (int i = 0; i < q; i++) {
    cin >> query;
    if (rem[query]) {
      cout << 1 << endl;
    } else {
      cout << 0 << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

void disp(vector<int> &V) {
  return;
  // for (int i=0; i<V.size()-1; i++) {
  //     cout << V[i] << " ";
  // }
  // cout << V[V.size()-1] << endl;
}

int main() {
  int N, M, Q, i, j, pos = 0, amount, proc_amount, tmp;
  vector<int> V;
  vector<int> queries;
  set<int> S;
  cin >> N >> M >> Q;

  for (i = 0; i < N; i++) {
    V.push_back(i);
  }

  disp(V);
  for (i = 0; i < M; i++) {
    cin >> amount;
    proc_amount = amount % V.size();
    if (amount % 2 == 0) {
      // forward
      if (pos + proc_amount >= V.size()) {
        pos = pos + proc_amount - V.size();
      } else {
        pos = pos + proc_amount;
      }
      V.erase(V.begin() + pos);
      if (pos >= V.size()) {
        pos = pos - V.size();
      }
    } else {
      // reverse
      if (pos - proc_amount < 0) {
        pos = pos - proc_amount + V.size();
      } else {
        pos = pos - proc_amount;
      }
      V.erase(V.begin() + pos);
      if (pos >= V.size()) {
        pos = pos - V.size();
      }
    }
    disp(V);
  }

  for (i = 0; i < V.size(); i++) {
    S.insert(V[i]);
  }

  for (i = 0; i < Q; i++) {
    cin >> tmp;
    if (S.count(tmp)) {
      cout << 1 << endl;
    } else {
      cout << 0 << endl;
    }
  }

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

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

#define MAX 200000

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int N, M, Q;
  cin >> N >> M >> Q;
  bool students[N];
  int next[N];
  int prev[N];
  for (int i = 0; i < N; i++) {
    next[i] = (i + 1 + N) % N;
    prev[i] = (i - 1 + N) % N;
    students[i] = true;
  }
  int pos = 0;
  while (M--) {
    int a;
    cin >> a;
    if (a & 1) {
      for (int i = 0; i < a; i++) {
        pos = prev[pos];
      }
    } else {
      for (int i = 0; i < a; i++) {
        pos = next[pos];
      }
    }
    students[pos] = false;
    next[prev[pos]] = next[pos];
    prev[next[pos]] = prev[pos];
    pos = next[pos];
  }
  while (Q--) {
    int pos;
    cin >> pos;
    if (students[pos])
      cout << 1 << endl;
    else
      cout << 0 << endl;
  }
}

Error: TLE

Fixed submission:

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

int try_buton_relay(vector<int> &status, int baton, int a) {
  bool is_even = false;
  if (a % 2 == 0)
    is_even = true;
  int max = status.size();
  int good_luck, next_baton;
  if (is_even) {
    good_luck = (baton + a + max * 100) % max;
  } else {
    good_luck = (baton - a + max * 100) % max;
  }
  status.erase(status.begin() + good_luck);
  if (good_luck == status.size())
    next_baton = 0;
  else
    next_baton = good_luck;
  return next_baton;
}

int N, M, Q;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int baton = 0;
  cin >> N >> M >> Q;
  vector<bool> students(N, false);
  vector<int> status(N, 0);
  for (int i = 0; i < N; i++) {
    status[i] = i;
  }

  for (int i = 0; i < M; i++) {
    int a;
    cin >> a;
    baton = try_buton_relay(status, baton, a);
  }

  for (int i = 0; i < status.size(); i++) {
    students[status[i]] = true;
  }
  for (int i = 0; i < Q; i++) {
    int q;
    cin >> q;
    if (students[q])
      cout << 1 << endl;
    else
      cout << 0 << endl;
  }
}
Problem p00297 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#define INF 1000000000
#define f first
#define s second
using namespace std;
typedef pair<int, pair<int, int>> P;
typedef long long ll;
int n, d, tree[4][(1 << 19)];
P star[200001];

void update(int i, int flg, int x) {
  i = i + (1 << 18) - 1;
  tree[flg][i] = x;
  while (i) {
    i = (i - 1) / 2;
    tree[flg][i] = max(tree[flg][i * 2 + 1], tree[flg][i * 2 + 2]);
  }
}

int find(int a, int b, int ima, int L, int R, int flg) {
  if (R <= a || b <= L)
    return -INF;
  if (a <= L && R <= b)
    return tree[flg][ima];
  int M = (L + R) / 2;
  return max(find(a, b, ima * 2 + 1, L, M, flg),
             find(a, b, ima * 2 + 2, M, R, flg));
}

int main() {
  cin >> n >> d;

  for (int i = 0; i < n; i++)
    cin >> star[i].s.f >> star[i].s.s >> star[i].f;
  sort(star, star + n);

  for (int i = 0; i < 4; i++)
    for (int j = 0; j < (1 << 19); j++)
      tree[i][j] = -INF;

  for (int i = 0; i < n; i++) {
    update(i, 0, star[i].s.f);  // max(x);
    update(i, 1, star[i].s.s);  // max(y);
    update(i, 2, -star[i].s.f); // min(x);
    update(i, 3, -star[i].s.s); // min(y);
  }

  long long S = 0;
  int ima = 0;
  for (int i = 0; i < n; i++) {
    while (ima != n && star[ima].f - star[i].f <= d)
      ima++;
    ll maxx = find(i, ima, 0, 0, (1 << 18), 0);
    ll maxy = find(i, ima, 0, 0, (1 << 18), 1);
    ll minx = find(i, ima, 0, 0, (1 << 18), 2);
    ll miny = find(i, ima, 0, 0, (1 << 18), 3);
    S = max(S, ((maxx + minx) * (maxy + miny)));
  }
  cout << S << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#define INF 1000000000
#define f first
#define s second
using namespace std;
typedef pair<int, pair<int, int>> P;
typedef long long ll;
int n, d, tree[4][(1 << 19)];
P star[200001];

void update(int i, int flg, int x) {
  i = i + (1 << 18) - 1;
  tree[flg][i] = x;
  while (i) {
    i = (i - 1) / 2;
    tree[flg][i] = max(tree[flg][i * 2 + 1], tree[flg][i * 2 + 2]);
  }
}

int find(int a, int b, int ima, int L, int R, int flg) {
  if (R <= a || b <= L)
    return -INF;
  if (a <= L && R <= b)
    return tree[flg][ima];
  int M = (L + R) / 2;
  return max(find(a, b, ima * 2 + 1, L, M, flg),
             find(a, b, ima * 2 + 2, M, R, flg));
}

int main() {
  cin >> n >> d;

  for (int i = 0; i < n; i++)
    cin >> star[i].s.f >> star[i].s.s >> star[i].f;
  sort(star, star + n);

  for (int i = 0; i < 4; i++)
    for (int j = 0; j < (1 << 19); j++)
      tree[i][j] = -INF;

  for (int i = 0; i < n; i++) {
    update(i, 0, star[i].s.f);  // max(x);
    update(i, 1, star[i].s.s);  // max(y);
    update(i, 2, -star[i].s.f); // min(x);
    update(i, 3, -star[i].s.s); // min(y);
  }

  long long S = 0;
  int ima = 0;
  for (int i = 0; i < n; i++) {
    while (i != n && star[ima].f - star[i].f <= d)
      ima++;
    ll maxx = find(i, ima, 0, 0, (1 << 18), 0);
    ll maxy = find(i, ima, 0, 0, (1 << 18), 1);
    ll minx = find(i, ima, 0, 0, (1 << 18), 2);
    ll miny = find(i, ima, 0, 0, (1 << 18), 3);
    S = max(S, ((maxx + minx) * (maxy + miny)));
  }
  cout << S << endl;
  return 0;
}
Problem p00297 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
#define f first
#define s second
#define INF 1000000000
int tree[4][(1 << 19)];
int n, d;
pair<int, pair<int, int>> star[(1 << 18)];

void update(int i, int x, int flg) {
  i = (1 << 18) + i - 1;
  tree[flg][i] = x;
  while (i) {
    i = (i - 1) / 2;
    tree[flg][i] = max(tree[flg][i * 2 + 1], tree[flg][i * 2 + 2]);
  }
}

int find(int a, int b, int M, int L, int R, int flg) {
  if (b <= L || a >= R)
    return -INF;
  if (a <= L && R <= b)
    return tree[flg][M];
  int nx = (L + R) / 2;
  return max(find(a, b, M * 2 + 1, L, nx, flg),
             find(a, b, M * 2 + 2, nx, R, flg));
}

int main() {
  cin >> n >> d;
  for (int i = 0; i < (1 << 19); i++)
    for (int j = 0; j < 4; j++)
      tree[j][i] = -INF;
  for (int i = 0; i < n; i++)
    cin >> star[i].s.f >> star[i].s.s >> star[i].f;
  sort(star, star + n);
  for (int i = 0; i < n; i++) {
    update(i, star[i].s.f, 0);  // max(x);
    update(i, star[i].s.s, 1);  // max(y);
    update(i, -star[i].s.f, 2); // min(x);
    update(i, -star[i].s.s, 3); // min(y);
  }
  ll now = 0, minx, maxx, miny, maxy, S = 0;
  for (int i = 0; i < n; i++) {
    while (now < n && star[now].f - star[i].f <= d)
      now++;
    maxx = find(i, now, 0, 0, (1 << 18), 0);
    maxy = find(i, now, 0, 0, (1 << 18), 1);
    minx = find(i, now, 0, 0, (1 << 18), 2);
    miny = find(i, now, 0, 0, (1 << 18), 3);
    S = max(S, ((ll)maxx + minx) * ((ll)maxy + miny));
  }
  cout << S << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define b first
#define x second.first
#define y second.second
using namespace std;
pair<int, pair<int, int>> s[200000];
int main() {
  int n, d, f = 0;
  long long an = 0;
  cin >> n >> d;
  for (int i = 0; i < n; i++)
    cin >> s[i].x >> s[i].y >> s[i].b;
  sort(s, s + n);
  for (int i = 0; i < n; i++) {
    int xmn, xmx, ymn, ymx;
    xmn = ymn = 2000000;
    xmx = ymx = 0;
    for (int j = i; s[j].b - s[i].b <= d; j++) {
      xmx = max(xmx, s[j].x);
      ymx = max(ymx, s[j].y);
      xmn = min(xmn, s[j].x);
      ymn = min(ymn, s[j].y);
      if (j == n - 1) {
        f = 1;
        break;
      }
    }
    long long w = xmx - xmn, h = ymx - ymn;
    an = max(an, w * h);
    if (f == 1)
      break;
  }
  cout << an << endl;
  return 0;
}
Problem p00297 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <unordered_map>
#include <vector>
#define int long long
using namespace std;

struct star {
  int cost, x, y;
};
bool operator<(const star &a, const star &b) { return a.cost < b.cost; }

star A[300000];
map<int, int> X;
map<int, int> Y;
signed main() {
  int a, b;
  scanf("%lld%lld", &a, &b);
  for (int c = 0; c < a; c++) {
    int d, e, f;
    scanf("%lld%lld%lld", &d, &e, &f);
    A[c] = {f, d, e};
  }
  sort(A, A + a);
  int MAX = 0;
  for (int s = 0, g = 0; g < a; g++) {
    X[A[g].x]++;
    Y[A[g].y]++;
    while (s < g && A[s].cost + b < A[g].cost) {
      X[A[s].x]--;
      Y[A[s].y]--;
      if (X[A[s].x] == 0) {
        X.erase(A[s].x);
      }
      if (Y[A[s].y] == 0) {
        Y.erase(A[s].y);
      }
      s++;
    }
    int n = X.rbegin()->first - X.begin()->first;
    int m = Y.rbegin()->first - Y.begin()->first;
    MAX = max(MAX, n * m);
  }
  printf("%lld\\n", MAX);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <unordered_set>
#include <vector>
#define int long long
using namespace std;

struct star {
  int cost, x, y;
};
bool operator<(const star &a, const star &b) { return a.cost <= b.cost; }
star A[300000];
map<int, int> X;
map<int, int> Y;
signed main() {
  int a, b;
  scanf("%lld%lld", &a, &b);
  for (int c = 0; c < a; c++) {
    int d, e, f;
    scanf("%lld%lld%lld", &d, &e, &f);
    A[c] = {f, d, e};
  }
  sort(A, A + a);
  int MAX = 0;
  for (int s = 0, g = 0; g < a; g++) {
    X[A[g].x]++;
    Y[A[g].y]++;
    while (s < g && A[s].cost + b < A[g].cost) {
      X[A[s].x]--;
      Y[A[s].y]--;
      if (X[A[s].x] == 0) {
        X.erase(A[s].x);
      }
      if (Y[A[s].y] == 0) {
        Y.erase(A[s].y);
      }
      s++;
    }
    int n = X.rbegin()->first - X.begin()->first;
    int m = Y.rbegin()->first - Y.begin()->first;
    MAX = max(MAX, n * m);
  }
  printf("%lld\\n", MAX);
}
Problem p00297 (C++) — buggy submission, verdict: Time Limit Exceeded

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

struct star {
  int x, y, b;
};

bool operator<(const star &s1, const star &s2) { return s1.b < s2.b; }

class xma {
public:
  bool operator()(const star &s1, const star &s2) { return s1.x < s2.x; }
};

class yma {
public:
  bool operator()(const star &s1, const star &s2) { return s1.y < s2.y; }
};

class xmi {
public:
  bool operator()(const star &s1, const star &s2) { return s1.x > s2.x; }
};

class ymi {
public:
  bool operator()(const star &s1, const star &s2) { return s1.y > s2.y; }
};

star s[200001];

int main() {

  int n;
  int d;

  cin >> n >> d;

  for (int i = 0; i < n; i++) {
    cin >> s[i].x >> s[i].y >> s[i].b;
  }

  sort(s, s + n);

  long long int ans = 0;

  map<int, int> x, y;

  priority_queue<star, vector<star>, xma> xmax;
  priority_queue<star, vector<star>, yma> ymax;
  priority_queue<star, vector<star>, xmi> xmin;
  priority_queue<star, vector<star>, ymi> ymin;

  for (int k = 0; k < n; k++) {

    xmax.push(s[k]);
    ymax.push(s[k]);
    xmin.push(s[k]);
    ymin.push(s[k]);

    // x[s[k].x] += 1;
    // y[s[k].y] += 1;

    while (!xmax.empty() && xmax.top().b + d < s[k].b) {
      xmax.pop();
    }
    while (!ymax.empty() && ymax.top().b + d < s[k].b) {
      ymax.pop();
    }
    while (!xmin.empty() && xmin.top().b + d < s[k].b) {
      xmin.pop();
    }
    while (!ymin.empty() && ymin.top().b + d < s[k].b) {
      ymin.pop();
    }

    long long int dx = xmax.top().x - xmin.top().x;
    long long int dy = ymax.top().y - ymin.top().y;

    // cout << xmax.top().x << " "  << -xmin.top().x << " " << ymax.top().y << "
    // " << -ymin.top().y << endl;

    ans = max(ans, dx * dy);
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

struct star {
  int x, y, b;
};

bool operator<(const star &s1, const star &s2) { return s1.b < s2.b; }

star ms(const star &s) {
  star tmp;
  tmp.b = s.b;
  tmp.x = -s.x;
  tmp.y = -s.y;
  return tmp;
}

star s[200001];

int main() {

  int n;
  int d;

  cin >> n >> d;

  for (int i = 0; i < n; i++) {
    cin >> s[i].x >> s[i].y >> s[i].b;
  }

  sort(s, s + n);

  long long int ans = 0;

  map<int, int> x, y;

  priority_queue<star> xmax, ymax, xmin, ymin;

  for (int k = 0, i = 0; k < n; k++) {

    xmax.push(s[k]);
    ymax.push(s[k]);
    xmin.push(ms(s[k]));
    ymin.push(ms(s[k]));

    x[s[k].x] += 1;
    y[s[k].y] += 1;

    while (xmax.top().b + d < s[k].b) {
      xmax.pop();
    }
    while (ymax.top().b + d < s[k].b) {
      ymax.pop();
    }
    while (-xmin.top().b + d < s[k].b) {
      xmin.pop();
    }
    while (-ymin.top().b + d < s[k].b) {
      ymin.pop();
    }

    long long int dx = xmax.top().x - -xmin.top().x;
    long long int dy = ymax.top().y - -ymin.top().y;

    ans = max(ans, dx * dy);
  }

  cout << ans << endl;

  return 0;
}