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

#include <climits>
#include <iostream>
using namespace std;
// 完成版。

int tree[(1 << 18)];

void Update(int s, int t, int m, int left = 0, int right = (1 << 17),
            int key = 0) {
  if (t < left || right <= s)
    return;
  if (s <= left && right - 1 <= t) {
    tree[key] = max(tree[key], m);
    return;
  }
  Update(s, t, m, left, (left + right) / 2, 2 * key + 1);
  Update(s, t, m, (left + right) / 2, right, 2 * key + 2);
}

int Find(int i) {
  i += (1 << 17) - 1;
  int m = tree[i];
  while (i) {
    i = (i - 1) / 2;
    m = max(tree[i], m);
  };
  return m;
}

int main() {
  int i;
  for (i = 0; i < (1 << 18); i++)
    tree[i] = 0;
  int n, q;
  int x[100000];
  for (i = 0; i < 100000; i++)
    x[i] = INT_MAX;
  int s, t, value, m;
  m = 1;

  cin >> n >> q;
  int query;
  while (q) {
    q--;
    cin >> query;
    if (query) {
      cin >> i;
      cout << x[Find(i)] << endl;
    } else {
      cin >> s >> t >> value;
      x[m] = value;
      Update(s, t, m);
      m++;
    }
  };
  return 0;
}


Error: TLE

Fixed submission:

#include <climits> //INT_MAXを使うためのinclude
#include <iostream>
using namespace std;

int main() {
  int s[100000];
  int t[100000];
  int x[100000];
  int A[100000];
  int i, j, m, s_min, t_max, n, q, ask;

  cin >> n >> q;
  s_min = n;
  t_max = 0;
  m = 0;
  for (i = 0; i < n; i++)
    A[i] = INT_MAX;
  while (q) {
    q--;
    cin >> ask;
    if (ask) {
      cin >> i;
      // if(i < s_min || i > t_max){
      //     A[i] = INT_MAX;
      cout << A[i] << endl;
      //}else{
      //    for(j = m; j >= 0; j--){
      //        if(s[j] <= i && t[j] >= i){
      //            A[i] = x[j];
      //            cout << A[i] << endl;
      //            break;
      //        }
      //    }
      //}
    } else {
      cin >> s[m] >> t[m] >> x[m];
      if (s[m] < s_min)
        s_min = s[m];
      if (t[m] > t_max)
        t_max = t[m];
      for (i = s[m]; i <= t[m]; i++)
        A[i] = x[m];
      m++;
    }
  };
  return 0;
}
Problem p02349 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
const int INF = 2147483647;
struct segment_tree {
  int map[2000010];
  void build(int now, int l, int r) {
    map[now] = 0;
    if (l == r)
      return;
    int mid = (l + r) / 2;
    build(now * 2 + 1, l, mid);
    build(now * 2 + 2, mid + 1, r);
    return;
  }
  void change(int n, int now, int L, int R, int l, int r) {
    if (R < l || L > r)
      return;
    if (L >= l && R <= r) {
      map[now] += n;
      return;
    }
    map[now * 2 + 1] += map[now];
    map[now * 2 + 2] += map[now];
    map[now] = 0;
    int mid = (L + R) / 2;
    change(n, now * 2 + 1, L, mid, l, r);
    change(n, now * 2 + 2, mid + 1, R, l, r);
    return;
  }
  int find(int now, int L, int R, int n) {
    if (L == R)
      return map[now];
    map[now * 2 + 1] += map[now];
    map[now * 2 + 2] += map[now];
    map[now] = 0;
    int mid = (L + R) / 2;
    if (mid >= n)
      return find(now * 2 + 1, L, mid, n);
    else
      return find(now * 2 + 2, mid + 1, R, n);
  }
};
int main() {
  int n, m, k, l, r;
  segment_tree arr;
  scanf("%d%d", &n, &m);
  arr.build(0, 1, n);
  while (m--) {
    scanf("%d", &k);
    if (k == 0) {
      scanf("%d%d%d", &l, &r, &k);
      arr.change(k, 0, 1, n, l, r);
    } else {
      scanf("%d", &k);
      printf("%d\\n", arr.find(0, 1, n, k));
    }
  }
}

Error: 0

Fixed submission:

#include <stdio.h>
const int INF = 2147483647;
struct segment_tree {
  int map[200010];
  void build(int now, int l, int r) {
    map[now] = 0;
    if (l == r)
      return;
    int mid = (l + r) / 2;
    build(now * 2 + 1, l, mid);
    build(now * 2 + 2, mid + 1, r);
    return;
  }
  void change(int n, int now, int L, int R, int l, int r) {
    if (R < l || L > r)
      return;
    if (L >= l && R <= r) {
      map[now] += n;
      return;
    }
    map[now * 2 + 1] += map[now];
    map[now * 2 + 2] += map[now];
    map[now] = 0;
    int mid = (L + R) / 2;
    change(n, now * 2 + 1, L, mid, l, r);
    change(n, now * 2 + 2, mid + 1, R, l, r);
    return;
  }
  int find(int now, int L, int R, int n) {
    if (L == R)
      return map[now];
    map[now * 2 + 1] += map[now];
    map[now * 2 + 2] += map[now];
    map[now] = 0;
    int mid = (L + R) / 2;
    if (mid >= n)
      return find(now * 2 + 1, L, mid, n);
    else
      return find(now * 2 + 2, mid + 1, R, n);
  }
};
int main() {
  int n, m, k, l, r;
  segment_tree arr;
  scanf("%d%d", &n, &m);
  arr.build(0, 1, n);
  while (m--) {
    scanf("%d", &k);
    if (k == 0) {
      scanf("%d%d%d", &l, &r, &k);
      arr.change(k, 0, 1, n, l, r);
    } else {
      scanf("%d", &k);
      printf("%d\\n", arr.find(0, 1, n, k));
    }
  }
}
Problem p02349 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
int n, z;

pair<int, int> st[4 * 100000];

int op(int iz, int der) { return st[iz].first + st[der].first; }
void push(int v, int l, int r) {
  if (r - l == 1)
    return;
  int m = (r + l) / 2;
  st[2 * v].second += st[v].second;
  st[2 * v].first += st[v].second * (m - l);
  st[2 * v + 1].second += st[v].second;
  st[2 * v + 1].first += st[v].second * (r - m);
  st[v].second = 0;
}

void u(int v, int l, int r, int i, int j, int x) {
  push(v, l, r);
  if (j < l || i >= r) {
    return;
  } else if (i <= l && r - 1 <= j) {
    st[v].second += x;
    st[v].first += (r - l) * x;
  } else {
    int m = (r + l) / 2;
    u(2 * v, l, m, i, j, x);
    u(2 * v + 1, m, r, i, j, x);
    st[v].first = op(2 * v, 2 * v + 1);
  }
}

int q(int v, int l, int r, int x) {
  push(v, l, r);
  if (x < l || x >= r) {
    return 0;
  } else {
    if (r - l == 1) {
      return st[v].first;
    } else {
      int m = (r + l) / 2;
      return max(q(2 * v, l, m, x), q(2 * v + 1, m, r, x));
    }
  }
}

int main() {
  cin >> n >> z;
  int a, b, c, d;
  while (z--) {
    cin >> a;
    if (a == 0) {
      cin >> b >> c >> d;
      u(1, 1, n + 1, min(b, c), max(b, c), d);
    } else {
      cin >> b;
      cout << q(1, 1, n + 1, b) << '\\n';
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
int n, z;

pair<int, int> st[4 * 100000];

int op(int iz, int der) { return st[iz].first + st[der].first; }
void push(int v, int l, int r) {
  int m = (r + l) / 2;
  st[2 * v].second += st[v].second;
  st[2 * v].first += st[v].second * (m - l);
  st[2 * v + 1].second += st[v].second;
  st[2 * v + 1].first += st[v].second * (r - m);
  st[v].second = 0;
}

void u(int v, int l, int r, int i, int j, int x) {
  push(v, l, r);
  if (j < l || i >= r) {
    return;
  } else if (i <= l && r - 1 <= j) {
    st[v].second += x;
    st[v].first += (r - l) * x;
  } else {
    int m = (r + l) / 2;
    u(2 * v, l, m, i, j, x);
    u(2 * v + 1, m, r, i, j, x);
    st[v].first = op(2 * v, 2 * v + 1);
  }
}

int q(int v, int l, int r, int x) {
  push(v, l, r);
  if (x < l || x >= r) {
    return 0;
  } else {
    if (r - l == 1) {
      return st[v].first;
    } else {
      int m = (r + l) / 2;
      return max(q(2 * v, l, m, x), q(2 * v + 1, m, r, x));
    }
  }
}

int main() {
  cin >> n >> z;
  int a, b, c, d;
  while (z--) {
    cin >> a;
    if (a == 0) {
      cin >> b >> c >> d;
      u(1, 1, n + 1, min(b, c), max(b, c), d);
    } else {
      cin >> b;
      cout << q(1, 1, n + 1, b) << '\\n';
    }
  }
  return 0;
}
Problem p02350 (C++) — buggy submission, verdict: Runtime Error

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

int n, q;

struct Node {
  int min = 0x7fffffff, lazy = -1;
} seg[800005];

void update(int u, int l, int r, int ql, int qr, int v) {
  if (seg[u].lazy != -1) {
    seg[u << 1].lazy = seg[u << 1 | 1].lazy = seg[u].lazy;
    seg[u].lazy = -1;
  }

  if (l == ql && r == qr) {
    seg[u].lazy = v;
    return;
  }

  int m = (l + r) >> 1;

  if (qr <= m)
    update(u << 1, l, m, ql, qr, v);
  else if (ql > m)
    update(u << 1 | 1, m + 1, r, ql, qr, v);
  else
    update(u << 1, l, m, ql, m, v), update(u << 1 | 1, m + 1, r, m + 1, qr, v);

  int lm = seg[u << 1].lazy != -1 ? seg[u << 1].lazy : seg[u << 1].min;
  int rm =
      seg[u << 1 | 1].lazy != -1 ? seg[u << 1 | 1].lazy : seg[u << 1 | 1].min;

  seg[u].min = min(lm, rm);
}

int query(int u, int l, int r, int ql, int qr) {
  if (seg[u].lazy != -1)
    return seg[u].lazy;

  if (l == ql && r == qr)
    return seg[u].min;

  int m = (l + r) >> 1;
  if (qr <= m)
    return query(u << 1, l, m, ql, qr);
  else if (ql > m)
    return query(u << 1 | 1, m + 1, r, ql, qr);
  else
    return min(query(u << 1, l, m, ql, m),
               query(u << 1 | 1, m + 1, r, m + 1, qr));
}

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

  for (int i = 0; i < q; i++) {
    int o;
    cin >> o;
    if (o == 0) {
      // update
      int l, r, v;
      cin >> l >> r >> v;
      update(1, 0, n - 1, l, r, v);
    } else {
      int l, r;
      cin >> l >> r;
      cout << query(1, 0, n - 1, l, r) << endl;
    }
  }
}


Error: 0

Fixed submission:

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

int n, q;

struct Node {
  int min = 0x7fffffff, lazy = -1;
} seg[400005];

void update(int u, int l, int r, int ql, int qr, int v) {
  if (seg[u].lazy != -1) {
    seg[u << 1].lazy = seg[u << 1 | 1].lazy = seg[u].lazy;
    seg[u].lazy = -1;
  }

  if (l == ql && r == qr) {
    seg[u].lazy = v;
    return;
  }

  int m = (l + r) >> 1;

  if (qr <= m)
    update(u << 1, l, m, ql, qr, v);
  else if (ql > m)
    update(u << 1 | 1, m + 1, r, ql, qr, v);
  else
    update(u << 1, l, m, ql, m, v), update(u << 1 | 1, m + 1, r, m + 1, qr, v);

  int lm = seg[u << 1].lazy != -1 ? seg[u << 1].lazy : seg[u << 1].min;
  int rm =
      seg[u << 1 | 1].lazy != -1 ? seg[u << 1 | 1].lazy : seg[u << 1 | 1].min;

  seg[u].min = min(lm, rm);
}

int query(int u, int l, int r, int ql, int qr) {
  if (seg[u].lazy != -1)
    return seg[u].lazy;

  if (l == ql && r == qr)
    return seg[u].min;

  int m = (l + r) >> 1;
  if (qr <= m)
    return query(u << 1, l, m, ql, qr);
  else if (ql > m)
    return query(u << 1 | 1, m + 1, r, ql, qr);
  else
    return min(query(u << 1, l, m, ql, m),
               query(u << 1 | 1, m + 1, r, m + 1, qr));
}

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

  for (int i = 0; i < q; i++) {
    int o;
    cin >> o;
    if (o == 0) {
      // update
      int l, r, v;
      cin >> l >> r >> v;
      update(1, 0, n - 1, l, r, v);
    } else {
      int l, r;
      cin >> l >> r;
      cout << query(1, 0, n - 1, l, r) << endl;
    }
  }
}
Problem p02350 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

constexpr int MAX_N = 100000, INF = 2147483647;

int n, q;

int seg[MAX_N * 4], lazy[MAX_N * 4];

bool flag[MAX_N * 4];

void init(int size) {

  n = 1;

  while (n < size)
    n *= 2;

  for (int i = 0; i < n * 2 - 1; ++i) {
    seg[i] = INF;
    lazy[i] = INF;
  }
}

void eval(int k, int l, int r) {

  if (flag[k]) {

    seg[k] = lazy[k];

    if (r - l > 1) {
      lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k];
      flag[k * 2 + 1] = flag[k * 2 + 2] = true;
    }
  }
  flag[k] = false;
}

void update(int a, int b, int v, int k, int l, int r) {

  eval(k, l, r);

  if (r <= a || b <= l)
    return;

  if (a <= l && r <= b) {

    lazy[k] = v;

    flag[k] = true;

    eval(k, l, r);

    return;
  }

  update(a, b, v, k * 2 + 1, l, (l + r) / 2);

  update(a, b, v, k * 2 + 2, (l + r) / 2, r);

  seg[k] = std::min(seg[k * 2 + 1], seg[k * 2 + 2]);
}

int query(int a, int b, int k, int l, int r) {

  eval(k, l, r);

  if (r <= a || b <= l)
    return INF;

  if (a <= l && r <= b)
    return seg[k];

  return std::min(query(a, b, k * 2 + 1, l, (l + r) / 2),
                  query(a, b, k * 2 + 2, (l + r) / 2, r));
}

int main() {

  std::cin >> n >> q;

  init(n);

  int com, s, t, x;

  for (int i = 0; i < q; ++i) {

    std::cin >> com >> s >> t;

    if (com == 0) {
      std::cin >> x;
      update(s, t + 1, x, 0, 0, n);
    } else
      std::cout << query(s, t + 1, 0, 0, n) << std::endl;
  }

  return 0;
}

Error: 0
sh: 1: pause: not found


Fixed submission:

#include <algorithm>
#include <iostream>

constexpr int MAX_N = 100000, INF = 2147483647;

int n, q;

int seg[MAX_N * 4], lazy[MAX_N * 4];

bool flag[MAX_N * 4];

void init(int size) {

  n = 1;

  while (n < size)
    n *= 2;

  for (int i = 0; i < n * 2 - 1; ++i) {
    seg[i] = INF;
    lazy[i] = INF;
  }
}

void eval(int k, int l, int r) {

  if (flag[k]) {

    seg[k] = lazy[k];

    if (r - l > 1) {
      lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k];
      flag[k * 2 + 1] = flag[k * 2 + 2] = true;
    }
  }
  flag[k] = false;
}

void update(int a, int b, int v, int k, int l, int r) {

  eval(k, l, r);

  if (r <= a || b <= l)
    return;

  if (a <= l && r <= b) {

    lazy[k] = v;

    flag[k] = true;

    eval(k, l, r);

    return;
  }

  update(a, b, v, k * 2 + 1, l, (l + r) / 2);

  update(a, b, v, k * 2 + 2, (l + r) / 2, r);

  seg[k] = std::min(seg[k * 2 + 1], seg[k * 2 + 2]);
}

int query(int a, int b, int k, int l, int r) {

  eval(k, l, r);

  if (r <= a || b <= l)
    return INF;

  if (a <= l && r <= b)
    return seg[k];

  return std::min(query(a, b, k * 2 + 1, l, (l + r) / 2),
                  query(a, b, k * 2 + 2, (l + r) / 2, r));
}

int main() {

  std::cin >> n >> q;

  init(n);

  int com, s, t, x;

  for (int i = 0; i < q; ++i) {

    std::cin >> com >> s >> t;

    if (com == 0) {
      std::cin >> x;
      update(s, t + 1, x, 0, 0, n);
    } else
      std::cout << query(s, t + 1, 0, 0, n) << std::endl;
  }

  system("pause");

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

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

int main(void) {
  int n, s;
  int sum = 0, start = 0, end = 0, num = 1;
  int swl = 0;
  cin >> n >> s;
  vector<int> a;
  a.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sum = a[0];
  while (start < n) {
    // cout<<start<<" to "<<end<<" sum="<<sum<<" num="<<num<<endl;
    if (sum >= s) {
      if (num < swl || swl == 0)
        swl = num;
      sum -= a[start];
      start++;
      num--;
    } else if (end < n - 1) {
      end++;
      sum += a[end];
      num++;
    } else
      break;
  }

  cout << swl << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main(void) {
  int n;
  int s;
  int swl = 0;
  bool f = false;
  cin >> n >> s;
  vector<int> a, sum;
  a.resize(n);
  sum.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - i; j++) {
      sum[j] += a[j + i];
      if (sum[j] >= s) {
        swl = i + 1;
        f = true;
        break;
      }
    }
    if (f == true)
      break;
  }
  cout << swl << endl;
  return 0;
}
Problem p02354 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
long long i, a, N, s, A = INT_MAX, l, r, S[100002];
int main() {
  scanf("%ld%ld", &N, &s);
  for (; i < N; i++) {
    scanf("%ld", &a);
    S[i + 1] += S[i] + a;
  }
  for (;;) {
    if (S[r] - S[l] >= s) {
      A = std::min(A, r - l);
      l++;
    } else
      r++;
    if (r > N) {
      if (A == INT_MAX)
        A = 0;
      printf("%ld\\n", A);
      return 0;
    }
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
long long i, a, N, s, A = INT_MAX, l, r, S[100002];
int main() {
  scanf("%ll%ll", &N, &s);
  for (; i < N; i++) {
    scanf("%ll", &a);
    S[i + 1] += S[i] + a;
  }
  for (;;) {
    if (S[r] - S[l] >= s) {
      A = std::min(A, r - l);
      l++;
    } else
      r++;
    if (r > N) {
      if (A == INT_MAX)
        A = 0;
      printf("%ll\\n", A);
      return 0;
    }
  }
}
Problem p02354 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#define INF 999999999

using namespace std;

int n, s, ans;
int a;
int d[105000];

int main() {
  scanf("%d %d", &n, &s);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a);
    d[i] = d[i - 1] + a;
  }

  if (d[n] < s) {
    printf("0\\n");
    return 0;
  }

  ans = n;
  for (int i = 0; d[i] + s <= d[n]; i++) {
    int j = lower_bound(d + i, d + n, d[i] + s) - d;
    ans = min(ans, j - i);
  }
  printf("%d\\n", ans);

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#define INF 999999999

using namespace std;

int n, s, ans;
int a[105000];
int d[105000];

int main() {
  scanf("%d %d", &n, &s);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    d[i] = d[i - 1] + a[i];
    if (a[i] >= s)
      ans = 1;
  }

  if (d[n] < s) {
    printf("0\\n");
    return 0;
  }

  if (d[n] == s) {
    printf("%d\\n", n);
    return 0;
  }

  if (ans == 1) {
    printf("1\\n");
    return 0;
  }

  int i = 2;
  while (i <= n) {
    for (int j = i; j <= n; j++) {
      int cur = d[j] - d[j - i];
      if (cur >= s) {
        printf("%d\\n", i);
        return 0;
      }
    }
    i++;
  }
  return 0;
}
Problem p02354 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int A[100100];

int main() {
  int n, s, ma = 0;
  scanf("%d%d", &n, &s);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  int ans = INT_MAX, S = 0, l = 0, r = -1;
  while (true) {
    while (r < n && S < s)
      S += A[++r];
    // printf("%d %d %d\\n",r,l,S);
    if (r >= n)
      break;
    while (S >= s) {
      ans = min(ans, r - l + 1);
      S -= A[l++];
    }
  }
  if (ans == INT_MAX)
    ans = 0;
  // if(ma>=s) ans=1;
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int A[100100];

int main() {
  int n, s, ma = 0;
  scanf("%d%d", &n, &s);
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
    ma = max(ma, A[i]);
  }
  int ans = INT_MAX, S = 0, l = 0, r = -1;
  while (true) {
    while (r < n && S < s)
      S += A[++r];
    // printf("%d %d %d\\n",r,l,S);
    if (r >= n)
      break;
    ans = min(ans, r - l + 1);
    while (l < r && S >= s)
      S -= A[l++];
  }
  if (ans == INT_MAX)
    ans = 0;
  // if(ma>=s) ans=1;
  printf("%d\\n", ans);
  return 0;
}
Problem p02354 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int N, S, sum = 0, ans = 1e9, k;
int a[100000];
int main() {

  scanf("%d %d", &N, &S);
  for (int i = 0; i < N; i++) {
    scanf("%d", &a[i]);
    sum += a[i];
    while (sum - a[k] >= S) {
      sum -= a[k];
      k++;
    }
    if (sum >= S)
      ans = min(ans, i - k + 1);
  }
  if (ans == 1e9)
    ans = 0;
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

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

int N, S, sum, ans = 1e9;
int a[100000];
int main() {
  scanf("%d %d", &N, &S);
  for (int i = 0; i < N; i++) {
    scanf("%d", &a[i]);
    sum = 0;
    for (int j = i; j >= 0; j--) {
      sum += a[j];
      if (i - j + 1 >= ans)
        break;

      if (S <= sum) {
        ans = i - j + 1;
        break;
      }
    }
  }
  if (ans == 1e9)
    ans = 0;
  printf("%d\\n", ans);
  return 0;
}
Problem p02354 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  int n, s;
  int ans = 1000000, sum = 0, cnt = 0, j = 0;
  vector<int> a;

  cin >> n >> s;
  a.resize(n);

  for (int i = 0; i < n; i++)
    cin >> a[i];

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

    while (sum < s && j < n) {
      sum += a[j];
      cnt++;
      j++;
    }

    if (sum >= s)
      ans = min(ans, cnt);

    sum -= a[i];
    cnt--;
  }

  (ans != 1000000) ? cout << ans << endl : cout << 0 << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {

  int n, s;
  int ans = 1000000;
  vector<int> a;

  cin >> n >> s;
  a.resize(n);

  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < n; i++) {
    int sum = 0, cnt = 0;
    for (int j = i; j < n; j++) {
      sum += a[j];
      cnt++;
      if (sum >= s) {
        ans = min(ans, cnt);
        break;
      }
    }
  }
  (ans != 1000000) ? cout << ans << endl : cout << 0 << endl;

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

#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
  int N, S, sum = 0, Ans = 100001;
  cin >> N >> S;
  vector<int> values(N);
  for (int i = 0; i < N; i++) {
    cin >> values[i];
  }
  auto ipt = values.begin();
  auto jpt = values.begin();
  while (jpt != values.end()) {
    sum += *jpt;
    jpt++;
    while (sum >= S) {
      Ans = min(Ans, int(jpt - ipt));
      sum -= *ipt;
      ipt++;
    }
    if (Ans == 1)
      break;
  }
  if (Ans == 100001)
    cout << 0 << endl;
  else
    cout << Ans << endl;
}

Error: 0

Fixed submission:

#include <functional>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
  int N, S, sum = 0, Ans = 100001;
  cin >> N >> S;
  vector<int> values(N);
  for (int i = 0; i < N; i++) {
    cin >> values[i];
  }
  for (auto ipt = values.begin();
       int(ipt - values.begin()) != int(values.end() - values.begin()); ipt++) {
    for (auto jpt = ipt;
         int(jpt - values.begin()) != int(values.end() - values.begin());
         jpt++) {
      sum += *jpt;
      while (sum >= S) {
        Ans = min(Ans, int(jpt - ipt) + 1);
        sum -= *ipt;
        ipt++;
      }
    }
  }
  if (Ans == 100001)
    cout << 0 << endl;
  else
    cout << Ans << endl;
}
Problem p02354 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <climits>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N, S;
  cin >> N >> S;
  vector<long long> Sum(N + 1, 0);
  for (int i = 1; i <= N; i++) {
    int a;
    cin >> a;
    Sum[i] += Sum[i - 1] + a;
  }
  long long ans = LLONG_MAX;
  int left = 0, right = 0;
  while (true) {
    if (Sum[right] - Sum[left] >= S) {
      ans = (ans < right - left) ? ans : right - left;
      left++;
    } else
      right++;
    if (right > N || left > N) {
      if (ans == LLONG_MAX)
        ans = 0;
      cout << ans << endl;
      return 0;
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N, S;
  cin >> N >> S;
  vector<long long> Sum(N + 1, 0);
  for (int i = 1; i <= N; i++) {
    int a;
    cin >> a;
    Sum[i] += Sum[i - 1] + a;
  }
  int len = 1;
  while (true) {
    if (len == N + 1) {
      cout << 0 << endl;
      return 0;
    }
    for (int i = 0; i + len <= N; i++) {
      if (Sum[i + len] - Sum[i] >= S) {
        cout << len << endl;
        return 0;
      }
    }
    len++;
  }
}
Problem p02355 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> counts(k + 1, 0);
  vector<int> v(n);

  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }

  int total = 0;

  int ans = 1000000000;
  for (int l = 0, r = 0; r < n; r++) {
    if (v[r] > k) {
      continue;
    }
    counts[v[r]]++;
    if (counts[v[r]] == 1) {
      total++;
    }
    while (l < r && (v[l] > k || (total == k && counts[v[l]] > 1))) {
      if (v[l] <= k) {
        counts[v[l]]--;
      }
      l++;
    }
    if (total == k) {
      ans = min(ans, r - l + 1);
    }
  }

  cout << (total == k ? ans : 0) << endl;

  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> counts(k + 1, 0);
  vector<int> v(n);

  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }

  int total = 0;

  int ans = 1000000000;
  for (int l = 0, r = 0; r < n; r++) {
    if (v[r] > k) {
      continue;
    }
    counts[v[r]]++;
    if (counts[v[r]] == 1) {
      total++;
    }
    while (l < r && (v[l] > k || (total == k && counts[v[l]] > 1))) {
      counts[v[l]]--;
      l++;
    }
    if (total == k) {
      ans = min(ans, r - l + 1);
    }
  }

  cout << (total == k ? ans : 0) << endl;

  return 0;
}