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

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

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<int>> a(h, vector<int>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      a[i][j] = !a[i][j];
    }
  }
  for (int i = 1; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == 0) {
        continue;
      }
      a[i][j] += a[i - 1][j];
    }
    a[i].emplace_back(0);
  }
  int ans = 0;
  for (int i = 0; i < h; i++) {
    int tmp = 0;
    stack<pair<int, int>> st;
    for (int j = 0; j <= w; j++) {
      if (st.empty() || st.top().first < a[i][j]) {
        st.emplace(make_pair(a[i][j], j));
      } else {
        if (a[i][j] < st.top().first) {
          int target = j;
          while (!st.empty() && a[i][j] <= st.top().first) {
            int height = st.top().first;
            int pos = st.top().second;
            st.pop();
            int area = height * (j - pos);
            tmp = max(tmp, area);
            target = pos;
          }
          st.emplace(make_pair(a[i][j], target));
        }
      }
    }
    ans = max(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}

Error: 0
1 1 0 1 1 0 
0 2 1 2 2 0 
1 3 2 0 3 0 
2 4 3 0 4 0 


Fixed submission:

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

int main() {
  int h, w;
  cin >> h >> w;
  int64_t dp[h][w + 1];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> dp[i][j];
      dp[i][j] = !dp[i][j];
    }
    dp[i][w] = 0;
  }
  for (int i = 1; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (dp[i][j] == 0)
        continue;
      dp[i][j] += dp[i - 1][j];
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w + 1; j++)
      cerr << dp[i][j] << ' ';
    cerr << endl;
  }
  int64_t ans = 0;
  for (int i = 0; i < h; i++) {
    stack<int64_t> st;
    for (int j = 0; j < w + 1; j++) {
      if (st.empty()) {
        st.emplace(dp[i][j]);
        continue;
      }
      for (int64_t x = 2; !st.empty() && dp[i][j] < st.top(); x++) {
        st.pop();
        ans = max(ans, x * dp[i][j]);
      }
      st.emplace(dp[i][j]);
    }
  }
  cout << ans << endl;
}
Problem p02327 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int h, w;
  char c;
  int mp[1410][1410] = {0};
  int ans = 0;

  cin >> h >> w;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      scanf(" %c", &c);
      if (c == '1')
        mp[i][j] = 0;
      if (c == '0')
        mp[i][j] = mp[i - 1][j] + 1;
    }
  }

  int mx;
  int target;
  stack<pair<int, int>> st;
  for (int i = 1; i <= h; i++) {
    mx = 0;

    for (int j = 1; j <= w; j++) {

      if (mx < mp[i][j]) {
        st.push(make_pair(mp[i][j], j));
        mx = mp[i][j];
      }

      else if (mx > mp[i][j]) {
        target = j;
        while (!st.empty()) {
          pair<int, int> f = st.top();
          if (f.first < mp[i][j])
            break; // ...?
          st.pop();
          // Debug
          /*
          cout<<i<<" "<<j<<" "<<f.first<<" "<<f.second<<endl;
          cout<<f.first*(j-f.second)<<endl;
          */
          ans = max(ans, (f.first * (j - f.second)));
          target = f.second;
        }
        if (mp[i][j] > 0)
          st.push(make_pair(mp[i][j], target));
        mx = mp[i][j];
      }
    }

    while (!st.empty()) {
      pair<int, int> f = st.top();
      st.pop();
      // Debug
      // cout<<i<<"  "<<f.second<<endl;
      ans = max(ans, (f.first * (w + 1 - f.second)));
      //  cout<<ans<<endl;
    }
  }

  cout << ans << endl;
  return (0);
}

Error: 0

Fixed submission:

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

int main() {
  int h, w;
  char c;
  int mp[1010][1010] = {0};
  int ans = 0;

  cin >> h >> w;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      scanf(" %c", &c);
      if (c == '1')
        mp[i][j] = 0;
      if (c == '0')
        mp[i][j] = mp[i - 1][j] + 1;
    }
  }

  int mx;
  int target;
  stack<pair<int, int>> st;
  for (int i = 1; i <= h; i++) {
    mx = 0;

    for (int j = 1; j <= w; j++) {

      if (mx < mp[i][j]) {
        st.push(make_pair(mp[i][j], j));
        mx = mp[i][j];
      }

      else if (mx > mp[i][j]) {
        target = j;
        while (!st.empty()) {
          pair<int, int> f = st.top();
          if (f.first < mp[i][j])
            break; // ...?
          st.pop();
          // Debug
          /*
          cout<<i<<" "<<j<<" "<<f.first<<" "<<f.second<<endl;
          cout<<f.first*(j-f.second)<<endl;
          */
          ans = max(ans, (f.first * (j - f.second)));
          target = f.second;
        }
        if (mp[i][j] > 0)
          st.push(make_pair(mp[i][j], target));
        mx = mp[i][j];
      }
    }

    while (!st.empty()) {
      pair<int, int> f = st.top();
      st.pop();
      // Debug
      // cout<<i<<"  "<<f.second<<endl;
      ans = max(ans, (f.first * (w + 1 - f.second)));
      //  cout<<ans<<endl;
    }
  }

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

#include <bits/stdc++.h>
#define mo 1510
using namespace std;
int d[mo], a[mo][mo], l[mo], h[mo];
int main() {
  int n, m;
  while (cin >> n >> m) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++)
        cin >> a[i][j];

    int maxs = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++)
        if (!a[i][j])
          d[j]++;
        else
          d[j] = 0;

      for (int j = 1; j <= m; j++) {
        l[j] = j;
        while (l[j] > 1 && d[j] <= d[l[j] - 1])
          l[j] = l[l[j] - 1];
      }

      for (int j = m; j > 0; j--) {
        h[j] = j;
        while (h[j] < m && d[j] <= d[h[j] + 1])
          h[j] = h[h[j] + 1];
      }
      for (int j = 1; j <= m; j++)
        maxs = max(maxs, d[j] * (h[j] - l[j] + 1));
    }
    cout << maxs << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int tag[505][505];
int dp[505][505];
int main() {
  int n, m;
  while (cin >> n >> m) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        cin >> tag[i][j];
        if (!tag[i][j])
          dp[i][j] = dp[i][j - 1] + 1;
      }
    }

    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        // cout<<dp[i][j]<<' ';
      }
      // cout<<endl;
    }
    int k, maxs = 0, l, x;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        x = dp[i][j];
        k = i;
        l = 1;
        while (k > 0) {
          x = min(x, dp[k][j]);
          maxs = max(maxs, x * l);
          l++;
          k--;
        }
      }
    }
    cout << maxs << endl;
  }
}
Problem p02327 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

size_t get_largest_rectangle(vector<vector<int>> &g) {
  size_t h = g.size(), w = g[0].size();

  vector<vector<int>> dp(h, vector<int>(w + 1));
  for (size_t i = 0; i < h; ++i)
    for (size_t j = 0; j < w; ++j)
      dp[i][j] = !g[i][j];

  for (size_t i = 1; i < h; ++i)
    for (size_t j = 0; j < w; ++j)
      if (dp[i][j])
        dp[i][j] += dp[i - 1][j];

  size_t max_area = 0;
  for (size_t i = 0; i < h; ++i)
    for (size_t j = 0; j < w; ++j) {
      if (!dp[i][j])
        continue;

      int min_height = dp[i][j];
      for (size_t k = j + 1; k <= w; ++k) {
        if (max_area < (k - j) * min_height)
          max_area = (k - j) * min_height;

        if (!dp[i][k])
          break;

        if (min_height > dp[i][k])
          min_height = dp[i][k];
      }
    }

  return max_area;
}

int main() {
  size_t h, w;
  scanf("%zu %zu", &h, &w);

  vector<vector<int>> g(h, vector<int>(w));
  for (size_t i = 0; i < h; ++i)
    for (size_t j = 0; j < w; ++j)
      scanf("%d", &g[i][j]);

  printf("%zu\\n", get_largest_rectangle(g));
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

size_t get_largest_rectangle(vector<vector<int>> &g) {
  size_t h = g.size(), w = g[0].size();

  vector<vector<size_t>> dp(h, vector<size_t>(w + 1));
  size_t max_area = 0;
  for (size_t i = 0; i < h; ++i)
    for (size_t j = 0; j < w; ++j)
      dp[i][j] = !g[i][j];

  for (size_t i = 1; i < h; ++i)
    for (size_t j = 0; j < w; ++j)
      if (dp[i][j])
        dp[i][j] += dp[i - 1][j];

  for (size_t i = 0; i < h; ++i)
    for (size_t j = 0; j < w; ++j) {
      if (!dp[i][j])
        continue;

      size_t min_height = dp[i][j];
      for (size_t k = j + 1; k <= w; ++k) {
        if (max_area < (k - j) * min_height)
          max_area = (k - j) * min_height;

        if (!dp[i][k])
          break;

        if (min_height > dp[i][k])
          min_height = dp[i][k];
      }
    }

  return max_area;
}

int main() {
  size_t h, w;
  scanf("%zu %zu", &h, &w);

  vector<vector<int>> g(h, vector<int>(w));
  for (size_t i = 0; i < h; ++i)
    for (size_t j = 0; j < w; ++j)
      scanf("%d", &g[i][j]);

  printf("%zu\\n", get_largest_rectangle(g));
  return 0;
}
Problem p02327 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stack>
using namespace std;

struct Rectangle {
  int height;
  int pos;
};

#include <stdio.h>
#define SIZ 1400
int m[SIZ + 1][SIZ + 1];
int st[SIZ];
char z[9999999];
int get() {
  static int input_count = 0;
  int r = 0;
  for (; '0' <= z[input_count] && z[input_count] <= '9';)
    r = r * 10 + z[input_count++] - '0';
  input_count++;
  return r;
}
int main() {
  fread(z, 1, sizeof(z), stdin);
  int i, j, k, y = get(), x = get(), r, ptr;
  for (j = 0; j < y; j++)
    for (r = i = 0; i < x; m[j][i] = r, i++) {
      int t = get();
      r = t == 0 ? r + 1 : 0;
    }
  r = 0;
  /*
          for(i=0;i<x;i++)for(ptr=j=0;j<=y;j++){
                  //printf("%d %d %d %d\\n",i,j,ptr,m[j][i]);
                  if(!ptr || st[ptr-1]<m[j][i])st[ptr++]=m[j][i];
                  else{
                          for(;ptr && st[ptr-1]>m[j][i];){
                                  int _y=(j-(ptr-1)),_x=st[--ptr];
                                  //printf("%d %d\\n",_x,_y);
                                  if(r<_x*_y)r=_x*_y;
                          }
                          st[ptr++]=m[j][i];
                  }
          }
  */
  Rectangle rect;
  for (int i = 0; i < x; i++) {
    stack<Rectangle> stk;
    for (int j = 0; j <= y; j++) {
      rect.height = m[j][i];
      rect.pos = j;
      if (stk.empty()) {
        stk.push(rect);
      } else {
        if (stk.top().height < rect.height) {
          stk.push(rect);
        } else if (stk.top().height > rect.height) {
          int target = j;
          while (!stk.empty() && stk.top().height >= rect.height) {
            Rectangle pre = stk.top();
            stk.pop();
            int area = pre.height * (j - pre.pos);
            r = max(r, area);
            target = pre.pos;
          }
          rect.pos = target;
          stk.push(rect);
        }
      }
    }
  }

  printf("%d\\n", r);
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#define SIZ 1400
int m[SIZ][SIZ];
char z[9999999];
int get() {
  static int input_count = 0;
  int r = 0;
  for (; '0' <= z[input_count] && z[input_count] <= '9';)
    r = r * 10 + z[input_count++] - '0';
  input_count++;
  return r;
}
int main() {
  fread(z, 1, sizeof(z), stdin);
  int i, j, k, y = get(), x = get(), r, M = 0;
  for (j = 0; j < y; j++)
    for (r = i = 0; i < x; m[j][i] = r, i++) {
      int t = get();
      r = t == 0 ? r + 1 : 0;
    }
  for (r = i = 0; i < x; i++)
    for (j = 0; j < y; j++)
      for (M = x, k = j; k < y; k++) {
        if (M > m[k][i])
          M = m[k][i];
        int z = k - j + 1;
        if (r < M * z)
          r = M * z;
      }
  printf("%d\\n", r);
  return 0;
}
Problem p02327 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;

#define MAX_H 1510
#define MAX_W 1510

int H, W;
int mat[MAX_H][MAX_W];

int maximum_rectangle() {
  int res = 0, h[MAX_W] = {0};
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      h[j] = (mat[i][j] == 0 ? h[j] + 1 : 0);
    }
    stack<pii> st;
    st.push(pii(0, W));
    for (int j = 0; j <= W; j++) {
      int k = j;
      while (h[j] < st.top().first) {
        k = st.top().second;
        res = max(res, st.top().first * (j - k));
        st.pop();
      }
      if (st.top().first < h[j]) {
        st.push(pii(h[j], k));
      }
    }
  }
  return res;
}

int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> mat[i][j];
    }
  }
  cout << maximum_rectangle() << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;

#define MAX_H 1510
#define MAX_W 1510

int H, W;
char mat[MAX_H][MAX_W];

int maximum_rectangle() {
  int res = 0, h[MAX_W] = {0};
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      h[j] = (mat[i][j] == '.' ? h[j] + 1 : 0);
    }
    stack<pii> st;
    st.push(pii(0, W));
    for (int j = 0; j <= W; j++) {
      int k = j;
      while (h[j] < st.top().first) {
        k = st.top().second;
        res = max(res, st.top().first * (j - k));
        st.pop();
      }
      if (st.top().first < h[j]) {
        st.push(pii(h[j], k));
      }
    }
  }
  return res;
}

int main() {
  while (cin >> H >> W, H) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> mat[i][j];
      }
    }
    cout << maximum_rectangle() << endl;
  }
  return 0;
}
Problem p02328 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long ll;
typedef pair<ll, ll> mp;

// ll M=1000000007;

//
//???????????°?????????????´¢???????????????
//

int main(void) {
  int N;
  cin >> N;
  ll h[100001];
  rep(i, N) cin >> h[i];
  ll left[100001], right[100001];
  ll max = 0;
  int j = 0;
  rep(i, N) { //?????????????´¢
    j = i - 1;
    while (true) {
      if (j == -1) {
        left[i] = 0;
        break;
      }
      if (h[j] >= h[i]) {
        if (j == left[j]) {
          j--;
        } else {
          j = left[j];
        }
      } else {
        left[i] = j + 1;
        break;
      }
    }
  }
  irep(i, N) { //?????????????´¢
    j = i + 1;
    while (true) {
      if (j == N) {
        right[i] = N - 1;
        break;
      }
      if (h[j] >= h[i]) {
        if (j == right[j]) {
          j++;
        } else {
          j = right[j];
        }
      } else {
        right[i] = j - 1;
        break;
      }
    }
  }
  rep(i, N) {
    if (max < (right[i] - left[i] + 1) * h[i])
      max = (right[i] - left[i] + 1) * h[i];
  }
  cout << max << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long ll;
typedef pair<ll, ll> mp;

// ll M=1000000007;

int main(void) {
  ll N;
  cin >> N;
  ll h[100001];
  rep(i, N) cin >> h[i];
  ll left, right, max = 0;
  rep(i, N) {
    right = N - 1;
    left = 0;
    if (i != N) {
      reg(j, i + 1, N - 1) {
        if (h[i] > h[j]) {
          right = j - 1;
          break;
        }
      }
    }
    if (i != 0) {
      ireg(j, 0, i - 1) {
        if (h[i] > h[j]) {
          left = j + 1;
          break;
        }
      }
    }
    if (max < (right - left + 1) * h[i])
      max = (right - left + 1) * h[i];
  }
  cout << max << endl;
  return 0;
}
Problem p02329 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(n) for (int i = 0; i < n; i++)
#define repp(j, n) for (int j = 0; j < n; j++)
#define reppp(i, m, n) for (int i = m; i < n; i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define MOD 1000000007
#define EPS 1e-9

using namespace std;

typedef long long ll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pii;
struct edge {
  int from, to;
  ll cost;
};

int main() {
  int n;
  ll v, ans = 0;
  cin >> n >> v;

  ll a[4][n];
  rep(4) repp(j, n) cin >> a[i][j];
  vector<ll> vec;
  rep(n) {
    repp(j, n) { vec.push_back(a[0][i] + a[1][j]); }
  }

  sort(all(vec));

  rep(n) {
    repp(j, n) {
      ll x = v - a[2][i] - a[3][j];
      ans += (ll)(upper_bound(all(vec), x) - lower_bound(all(vec), x));
    }
  }

  cout << ans << endl;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(n) for (int i = 0; i < n; i++)
#define repp(j, n) for (int j = 0; j < n; j++)
#define reppp(i, m, n) for (int i = m; i < n; i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define MOD 1000000007
#define EPS 1e-9

using namespace std;

typedef long long ll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pii;
struct edge {
  int from, to;
  ll cost;
};

int main() {
  int n;
  ll v;
  cin >> n >> v;

  ll a;
  map<ll, int> mp[2];
  mp[1][0] = 1;
  rep(3) {
    mp[i % 2].clear();
    repp(j, n) {
      cin >> a;
      if (a > v)
        continue;
      for (auto itr = mp[1 - i % 2].begin(); itr != mp[1 - i % 2].end();
           ++itr) {
        // cerr << itr->first << ":" << itr->second <<endl;
        if (itr->first <= v - a)
          mp[i % 2][itr->first + a] += itr->second;
        // cerr << mp[i%2][itr->first + a[j]] << endl;
      }
    }
  }

  ll ans = 0;
  repp(j, n) {
    cin >> a;
    if (mp[0][v - a])
      ans += mp[0][v - a];
  }

  cout << ans << endl;
}
Problem p02329 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  int n, V, ans = 0;
  cin >> n >> V;
  int a[1000], b[1000], c[1000], d[1000];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  for (int i = 0; i < n; i++)
    cin >> c[i];
  for (int i = 0; i < n; i++)
    cin >> d[i];
  vector<int> v;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      v.push_back(c[i] + d[j]);
  sort(v.begin(), v.end());

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      int t = V - (a[i] + b[j]);
      int l = -1, m, r = v.size();
      while (r - l > 1) {
        m = (r + l) / 2;
        if (v[m] >= t)
          r = m;
        else
          l = m;
      }
      int tr = r;
      t++;
      l = -1, r = v.size();
      while (r - l > 1) {
        m = (r + l) / 2;
        if (v[m] >= t)
          r = m;
        else
          l = m;
      }
      ans += r - tr;
    }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  int n, V, ans = 0;
  cin >> n >> V;
  int a[1000], b[1000], c[1000], d[1000];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  for (int i = 0; i < n; i++)
    cin >> c[i];
  for (int i = 0; i < n; i++)
    cin >> d[i];
  vector<int> v;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      v.push_back(c[i] + d[j]);
  sort(v.begin(), v.end());

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      int t = V - (a[i] + b[j]);
      int l = -1, m, r = v.size();
      while (r - l > 1) {
        m = (r + l) / 2;
        if (v[m] >= t)
          r = m;
        else
          l = m;
      }
      for (int i = r; i < v.size(); i++)
        if (v[i] == t)
          ans++;
        else
          break;
    }
  cout << ans << endl;
  return 0;
}
Problem p02329 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <map>
#include <stdio.h>
using namespace std;
typedef long long int ll;
const int N = 1e3 + 10;
int main() {
  int n;
  ll v, a[N], temp, ans = 0;
  map<ll, ll> m;
  scanf("%d%lld", &n, &v);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &temp);
    for (int j = 0; j < n; j++) {
      if (m.find(v - temp - a[j]) != m.end())
        m[v - temp - a[j]]++;
      else
        m.insert({v - temp - a[j], 1});
    }
  }
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &temp);
    for (int j = 0; j < n; j++)
      if (m.find(temp + a[j]) != m.end())
        ans += m[temp + a[j]];
  }
  printf("%lld\\n", ans);
}


Error: TLE

Fixed submission:

#include <stdio.h>
#include <unordered_set>
using namespace std;
typedef long long int ll;
const int N = 1e3 + 10;
int main() {
  int n;
  ll v, a[N], temp, ans = 0;
  unordered_multiset<ll> s;
  scanf("%d%lld", &n, &v);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &temp);
    for (int j = 0; j < n; j++)
      s.insert(v - a[j] - temp);
  }
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &temp);
    for (int j = 0; j < n; j++)
      ans += s.count(a[j] + temp);
  }
  printf("%lld\\n", ans);
}