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

#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;

int a[100002], b[100002];
int main() {
  int n, k, ans = inf;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

  int l = 0, r = 0, sum = 0;
  while (r <= n) {
    if (sum == k)
      ans = min(ans, r - l);
    if (sum < k) {
      if (a[++r] <= k && b[a[r]]++ == 0)
        sum++;
    } else {
      if (a[++l] <= k && --b[a[l]] == 0)
        sum--;
    }
  }

  if (ans == inf)
    printf("0\\n");
  else
    printf("%d\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;

int a[100002], b[100002];
int main() {
  int n, k, ans = inf;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

  int l = 0, r = 0, sum = 0;
  while (r <= n) {
    if (sum == k)
      ans = min(ans, r - l);
    if (sum < k)
      if (a[++r] <= k && b[a[r]]++ == 0)
        sum++;
      else if (a[++l] <= k && --b[a[l]] == 0)
        sum--;
  }

  if (ans == inf)
    printf("0\\n");
  else
    printf("%d\\n", ans);
  return 0;
}
Problem p02356 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999

using namespace std;

typedef pair<int, int> P;

int dy[4] = {0, 0, 1, -1};
int dx[4] = {1, -1, 0, 0};

ll a[100100], x[510];
int main() {
  a[0] = 0;
  int N, Q;
  cin >> N >> Q;
  REP(i, N) {
    ll c;
    cin >> c;
    a[i + 1] = a[i] + c;
  }
  REP(i, Q) cin >> x[i];
  REP(k, Q) {
    ll ans = 0;
    ll sum = 0;
    int right = 1;
    FOR(left, 0, N) {
      while (right < N + 1 && (a[right] - a[left]) <= x[k]) {
        right++;
      }
      ans += (right - left - 1);
      if (right == (left + 1))
        right++;
    }
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999

using namespace std;

typedef pair<int, int> P;

int dy[4] = {0, 0, 1, -1};
int dx[4] = {1, -1, 0, 0};

ll a[100100], x[510];
int main() {
  a[0] = 0;
  int N, Q;
  cin >> N >> Q;
  REP(i, N) {
    ll c;
    cin >> c;
    a[i + 1] = a[i] + c;
  }
  REP(i, Q) cin >> x[i];
  REP(k, Q) {
    ll ans = 0;
    REP(i, N) {
      FOR(j, i + 1, N + 1) {
        if ((a[j] - a[i]) <= x[k])
          ans++;
        else
          break;
      }
    }
    cout << ans << endl;
  }
}
Problem p02356 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define rep(i, n) REP(i, 0, n)
#define REP(i, k, n) for (int i = k; i < n; i++)
#define mmax(start, end) *max_element(start, end)
#define mmin(start, end) *min_element(start, end)
#define argmax max_element
#define argmin min_element
#define push push_back
#define pop pop_back
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
typedef string str;

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

template <class T = ll> T in() {
  T _in;
  cin >> _in;
  return _in;
}

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

  int N, Q;
  ll a[100000], x[500];

  cin >> N >> Q;
  rep(i, N) { cin >> a[i]; }
  rep(i, Q) { cin >> x[i]; }

  rep(i, Q) {
    ll ans = 0;
    ll lim = x[i];
    // 2重ループベースのしゃくとり [left, right)
    int right = 0;
    ll sum = 0;
    rep(left, N) {
      while (right < N && (sum + a[right] <= lim)) {
        sum += a[right];
        if (sum <= lim) {
          right++;
          // cout << left+1 << " " << right+1 << endl;
        } else
          break;
      }

      ans += right - left;
      // cout << left + 1 << " " << right + 1 << endl;

      if (right == left)
        right++; // 追いついたらrightを進める
      else
        sum -= a[left];
    }
    cout << ans << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

#define rep(i, n) REP(i, 0, n)
#define REP(i, k, n) for (int i = k; i < n; i++)
#define mmax(start, end) *max_element(start, end)
#define mmin(start, end) *min_element(start, end)
#define argmax max_element
#define argmin min_element
#define push push_back
#define pop pop_back
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
typedef string str;

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

template <class T = ll> T in() {
  T _in;
  cin >> _in;
  return _in;
}

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

  int N, Q;
  ll a[100000], x[500];

  cin >> N >> Q;
  rep(i, N) { cin >> a[i]; }
  rep(i, Q) { cin >> x[i]; }

  rep(i, Q) {
    ll ans = 0;
    ll lim = x[i];
    // 2重ループベースのしゃくとり [left, right)
    rep(left, N) {
      ll sum = 0;
      for (int right = left + 1; right != N + 1;) {
        sum += a[right - 1];
        if (sum <= lim) {
          ans++;
          right++;
        } else
          break;
      }
    }
    cout << ans << endl;
  }

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

#include <algorithm>
#include <array>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) ((a) > (b) ? (b) : (a))
#define max(a, b) ((a) > (b) ? (a) : (b))

using namespace std;

typedef pair<int, int> pii;

long long int a[100010];

int main() {
  long long int N, Q;
  scanf("%lld %lld", &N, &Q);

  for (int i = 0; i < N; i++) {
    scanf("%lld", &a[i]);
  }
  for (int i = 0; i < Q; i++) {
    long long int q;
    scanf("%lld", &q);

    long long int l = 0, t = 0, c = 0;

    for (long long int r = 0; r < N; r++) {
      t += a[r];
      while (t > q) {
        t -= a[l];
        l++;
      }
      c += (r - l + 1);
    }

    printf("%lld\\n", c);
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) ((a) > (b) ? (b) : (a))
#define max(a, b) ((a) > (b) ? (a) : (b))

using namespace std;

typedef pair<int, int> pii;

int a[100010];

int main() {
  int N, Q;
  scanf("%d %d", &N, &Q);

  for (int i = 0; i < N; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 0; i < Q; i++) {
    int q;
    scanf("%lld", &q);

    long long int l = 0, t = 0, c = 0;

    for (int r = 0; r < N; r++) {
      t += a[r];
      while (t > q) {
        t -= a[l];
        l++;
      }
      c += (r - l + 1);
    }

    printf("%lld\\n", c);
  }

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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int INF = INT_MAX / 3;
typedef unsigned long long ull;

ull NumberofWindow(ull N, vector<ull> &a_list, ull S) {
  ull i = 0;
  ull j = 0;
  ull tmpS = a_list[0];
  ull res = 0;
  while (j < N) {
    while ((tmpS <= S) && (i < N - 1)) {
      i++;
      tmpS += a_list[i];
    }
    if (i == N - 1) {
      if (tmpS > S) {
        res += i - j;
      } else {
        res += i - j + 1;
      }
    } else {
      res += i - j;
    }
    tmpS -= a_list[j];
    j++;
  }
  return res;
}

int main() {

  int N, Q;
  ull x;
  cin >> N >> Q;
  vector<ull> a_list(N);
  for (int i = 0; i < N; i++) {
    cin >> a_list[i];
  }
  /*
  ull N, Q;
  ull x;
  ifstream ifs("DSL_3_C-in21.txt");
  ifs >> N >> Q;
  cout << N << Q << endl;
  vector<ull> a_list(N);
  for (int i=0;i<N;i++){
          ifs >> a_list[i];
  }
  */
  for (int i = 0; i < Q; i++) {
    cin >> x;
    cout << NumberofWindow(N, a_list, x) << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int INF = INT_MAX / 3;
typedef unsigned long long ull;

ull NumberofWindow(ull N, vector<ull> &a_list, ull S) {
  ull i = 0;
  ull j = 0;
  ull tmpS = a_list[0];
  ull res = 0;
  while (j < N) {
    while ((tmpS <= S) && (i < N - 1)) {
      i++;
      tmpS += a_list[i];
    }
    if (i == N - 1) {
      if (tmpS > S) {
        res += i - j;
      } else {
        res += i - j + 1;
      }
    } else {
      res += i - j;
    }
    tmpS -= a_list[j];
    j++;
  }
  return res;
}

int main() {
  /*
  int N, Q;
  ull x;
  cin >> N >> Q;
  vector<ull> a_list(N);
  for (int i=0;i<N;i++){
          cin >> a_list[i];
  }
  */
  ull N, Q;
  ull x;
  ifstream ifs("DSL_3_C-in21.txt");
  ifs >> N >> Q;
  cout << N << Q << endl;
  vector<ull> a_list(N);
  for (int i = 0; i < N; i++) {
    ifs >> a_list[i];
  }
  for (int i = 0; i < Q; i++) {
    ifs >> x;
    cout << NumberofWindow(N, a_list, x) << endl;
  }
  return 0;
}
Problem p02356 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
#define REP(i, n) for (int i = 0; i < n; ++i)

int main() {
  int n, Q;
  cin >> n >> Q;
  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  REP(j, Q) {
    ll x;
    cin >> x;
    ll res = 0;
    int right = 0;
    ll sum = 0;
    REP(left, n) {
      while (right < n && sum + a[right] <= x) {
        sum += a[right];
        ++right;
      }
      res += right - left;
      if (right == left)
        ++right;
      else
        sum -= a[left];
    }
    cout << res << endl;
  }
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
#define REP(i, n) for (int i = 0; i < n; ++i)

int main() {
  int n, Q;
  cin >> n >> Q;
  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  REP(j, Q) {
    ll x;
    cin >> x;
    ll res = 0;
    REP(left, n) {
      ll sum = 0;
      int right = left;
      while (right < n && sum + a[right] <= x) {
        sum += a[right];
        ++right;
      }
      res += right - left;
    }
    cout << res << endl;
  }
}
Problem p02356 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;
long long p[100005], N, Q, A;
int main() {
  cin >> N >> Q;
  for (int i = 0; i < N; i++)
    scanf("%lld", &p[i]);
  for (int i = 0; i < Q; i++) {
    long long sum = 0, R = 0;
    cin >> A;
    long long cnt = 0;
    for (int i = 0; i < N; i++) {
      while (R < N) {
        if (sum + p[R] > A)
          break;
        sum += p[R];
        R++;
      }
      if (sum > 0) {
        sum -= p[i];
      }
      cnt += R - i;
      if (R < i + 1)
        R = i + 1;
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
long long p[100005], N, Q, A;
int main() {
  cin >> N >> Q;
  for (int i = 0; i < N; i++)
    scanf("%lld", p[i]);
  for (int i = 0; i < Q; i++) {
    long long sum = 0, R = 0;
    cin >> A;
    long long cnt = 0;
    for (int i = 0; i < N; i++) {
      while (R < N) {
        if (sum + p[R] > A)
          break;
        sum += p[R];
        R++;
      }
      if (sum > 0) {
        sum -= p[i];
      }
      cnt += R - i;
      if (R < i + 1)
        R = i + 1;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p02357 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

multiset<int> s;
int a[1000010];
int main() {

  int N, L;
  cin >> N >> L;

  for (int i = 0; i < L; i++) {
    cin >> a[i];
    s.insert(a[i]);
  }

  cout << *s.begin();

  for (int i = L; i < N; i++) {
    cin >> a[i];
    s.insert(a[i]);
    s.erase(s.find(a[i - L]));
    cout << ' ' << *s.begin();
  }

  cout << '\\n';

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MAX 100005
#define ll long long
#define pii pair<int, int>

int arr[MAX];

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  vector<int> ans;
  multiset<int> s;
  for (int i = 1; i <= k; i++)
    s.insert(arr[i]);
  ans.push_back(*s.begin());
  for (int i = k + 1; i <= n; i++) {
    s.erase(s.find(arr[i - k]));
    s.insert(arr[i]);
    ans.push_back(*s.begin());
  }
  cout << ans[0];
  for (int i = 1; i < ans.size(); i++)
    cout << " " << ans[i];
  cout << '\\n';

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

#define _USE_MATH_DEFINES

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

typedef pair<long long int, long long int> P;
long long int INF = 1e18;

long long int a[1100000], deq[1100000];

int main() {

  int n, L;
  cin >> n >> L;

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

  int s = 0, t = 0; // 先頭と末尾

  for (int i = 0; i < n; i++) {
    while (s < t && a[deq[t - 1]] >= a[i]) {
      t--;
    }
    deq[t] = i;
    t++;
    if (i - L + 1 >= 0) {
      // cout << deq[s] << " " << i - L + 1 << endl;
      printf("%d%c", a[deq[s]], i == n - 1 ? '\\n' : ' ');
      if (deq[s] == i - L + 1) {
        s++;
      }
    } else {
      continue;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#define _USE_MATH_DEFINES

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

typedef pair<long long int, long long int> P;
long long int INF = 1e18;

long long int a[110000], deq[110000];

int main() {

  int n, L;
  cin >> n >> L;

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

  int s = 0, t = 0; // 先頭と末尾

  for (int i = 0; i < n; i++) {
    while (s < t && a[deq[t - 1]] >= a[i]) {
      t--;
    }
    deq[t] = i;
    t++;
    if (i - L + 1 >= 0) {
      // cout << deq[s] << " " << i - L + 1 << endl;
      printf("%d%c", a[deq[s]], i == n - 1 ? '\\n' : ' ');
      if (deq[s] == i - L + 1) {
        s++;
      }
    } else {
      continue;
    }
  }

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

#include <bits/stdc++.h>

using namespace std;

multiset<int> s;

int main() {

  int N, L;
  scanf("%d%d", &N, &L);

  int arr[N];

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

    scanf("%d", &arr[i]);
    if (i < L)
      s.insert(arr[i]);
  }

  for (int i = L; i <= N; i++) {

    printf("%d", *s.begin());

    s.erase(s.find(arr[i - L]));

    s.insert(arr[i]);

    if (i < N)
      printf(" ");
  }

  printf("\\n");
  return 0;
}


Error: 0

Fixed submission:

// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define MAX 100005

using namespace std;

int minT[4 * MAX];
int A[MAX];

void buildMIN(int node, int st, int end) {
  if (st == end)
    minT[node] = st;
  else {
    int med = (st + end) / 2;
    buildMIN(2 * node, st, med);
    buildMIN(2 * node + 1, med + 1, end);
    if (A[minT[2 * node]] < A[minT[2 * node + 1]])
      minT[node] = minT[2 * node];
    else
      minT[node] = minT[2 * node + 1];
  }
}

int queryMIN(int node, int st, int end, int i, int j) {
  if (st > j || end < i)
    return -1;
  if (st >= i && end <= j)
    return minT[node];
  else {
    int med = (st + end) / 2;
    int left = queryMIN(2 * node, st, med, i, j);
    int right = queryMIN(2 * node + 1, med + 1, end, i, j);
    if (left == -1)
      return right;
    if (right == -1)
      return left;
    if (A[left] < A[right])
      return left;
    else
      return right;
  }
}

int main() {
  int n, k;
  // priority_queue<int> q;
  cin >> n >> k;
  // vector<int> l;
  // int x;
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
  }
  buildMIN(1, 1, n);
  bool f = true;
  for (int i = 1; i + k - 1 <= n; i++) {
    int s = A[queryMIN(1, 1, n, i, i + k - 1)];
    if (f) {
      cout << s;
      f = false;
    } else
      cout << " " << s;
  }
  cout << "\\n";
}
Problem p02357 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define endl '\\n'
typedef long long ll;

int main() {
  ll pos = 0;
  ll n, l, temp, p;
  cin >> n >> l;
  vector<ll> elems;
  set<pair<ll, ll>> res;
  pair<ll, ll> temp1;
  for (ll i = 0; i < n; i++) {
    cin >> temp;
    elems.push_back(temp);
  }
  for (ll i = 0; i < l; i++) {
    res.insert(make_pair(elems[i], i));
  }
  temp1 = *(res.begin());
  if (n == l) {
    cout << temp1.first << endl;
    return 0;
  }

  else
    cout << temp1.first << " ";
  res.erase(make_pair(elems[pos], pos));
  pos++;
  for (ll i = l; i < n; i++) {
    res.insert(make_pair(elems[i], i));
    temp1 = *(res.begin());
    if (i != n - 1)
      cout << temp1.first << " ";
    else
      cout << temp1.first << endl;
    res.erase(make_pair(elems[pos], pos));
    pos++;
  }
  return 0;
}


Error: 0

Fixed submission:

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

const int MAXN = 1e6 + 2;
int n, l, v[2 * MAXN], N;

int rmq(int a, int b) {
  a += N;
  b += N;
  int ans = INT_MAX;
  while (a <= b) {
    if (a % 2 == 1)
      ans = min(ans, v[a++]);
    if (b % 2 == 0)
      ans = min(ans, v[b--]);
    a /= 2;
    b /= 2;
  }
  return ans;
}

int main() {
  //	freopen(".in", "r", stdin);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> l;
  N = 1 << (int(log2(n)));
  if (N < n)
    N *= 2;

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

  for (int i = n; i < N; i++)
    v[N + i] = INT_MAX;

  for (int i = 2 * N - 1; i > 0; i -= 2)
    v[i / 2] = min(v[i], v[i - 1]);

  for (int i = 0; i < n - l; i++)
    cout << rmq(i, i + l - 1) << " ";
  cout << rmq(n - l, n - 1) << '\\n';

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

#include <iostream>
#include <set>

using namespace std;

int main() {

  int N, L, a[1000000];

  cin >> N >> L;

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

  // solve
  multiset<int> ms;

  for (int i = 0; i < L; i++) {
    ms.insert(a[i]);
  }

  cout << *ms.begin();

  for (int i = L; i < N; i++) {
    ms.erase(ms.find(a[i - L]));
    ms.insert(a[i]);
    cout << ' ' << *ms.begin();
  }

  cout << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <set>

using namespace std;

int main() {

  int N, L, a[1000000];

  cin >> N >> L;

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

  // solve
  multiset<int> ms;

  for (int i = 0; i < L; i++) {
    ms.insert(a[i]);
  }

  cout << *ms.begin();

  for (int i = L; i < N; i++) {
    int c = ms.erase(a[i - L]);
    for (int j = 0; j < c - 1; j++) {
      ms.insert(a[i - L]);
    }
    ms.insert(a[i]);
    cout << ' ' << *ms.begin();
  }

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

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using ll = long long;

template <typename T> T min(T a, T b) { return (a < b) ? a : b; }

template <typename T> class SegTree {
  static const int MAXN = 1000010;
  T INF;
  int n;
  // T A[MAXN*4];
  T *A;

public:
  SegTree(int size, T INF) : INF(INF) {
    n = 1;
    while (n < size) {
      n *= 2;
    }
    A = new T[MAXN * 4];
    for (int i = 0; i < MAXN * 4; i++)
      A[i] = INF;
  }

  T query(int a, int b) { return _query(a, b, 0, 0, n); }

  T _query(int a, int b, int k, int l, int r) {
    if (a >= r || b <= l) {
      return INF;
    }
    if (l >= a && r <= b) {
      return A[k];
    }
    T v1 = _query(a, b, 2 * k + 1, l, (l + r) / 2);
    T v2 = _query(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(v1, v2);
  }

  void update(int _idx, int value) {
    int offset = n - 1;
    int idx = _idx + offset;
    A[idx] = value;
    while (idx > 0) {
      idx = (idx - 1) / 2;
      A[idx] = min(A[idx * 2 + 1], A[idx * 2 + 2]);
    }
  }
};

int main(void) {
  const int INF = 1000000010;
  int N, L;
  cin >> N >> L;
  SegTree<int> rmq(N, INF);
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    rmq.update(i, a);
  }

  for (int i = 0; i <= N - L; i++) {
    if (i == 0) {
      cout << rmq.query(i, i + L);
    } else {
      cout << " " << rmq.query(i, i + L);
    }
  }
  cout << '\\n';
}


Error: 0

Fixed submission:

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using ll = long long;

template <typename T> T min(T a, T b) { return (a < b) ? a : b; }

template <typename T> class SegTree {
  static const int MAXN = 100010;
  T INF;
  int n;
  T A[MAXN * 4];

public:
  SegTree(int size, T INF) : INF(INF) {
    n = 1;
    while (n < size) {
      n *= 2;
    }
    for (int i = 0; i < MAXN * 4; i++)
      A[i] = INF;
  }

  T query(int a, int b) { return _query(a, b, 0, 0, n); }

  T _query(int a, int b, int k, int l, int r) {
    if (a >= r || b <= l) {
      return INF;
    }
    if (l >= a && r <= b) {
      return A[k];
    }
    T v1 = _query(a, b, 2 * k + 1, l, (l + r) / 2);
    T v2 = _query(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(v1, v2);
  }

  void update(int _idx, int value) {
    int offset = n - 1;
    int idx = _idx + offset;
    A[idx] = value;
    while (idx > 0) {
      idx = (idx - 1) / 2;
      A[idx] = min(A[idx * 2 + 1], A[idx * 2 + 2]);
    }
  }
};

int main(void) {
  const int INF = 1000000010;
  int N, L;
  cin >> N >> L;
  SegTree<int> rmq(N, INF);
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    rmq.update(i, a);
  }

  for (int i = 0; i <= N - L; i++) {
    if (i == 0) {
      cout << rmq.query(i, i + L);
    } else {
      cout << " " << rmq.query(i, i + L);
    }
  }
  cout << '\\n';
}
Problem p02357 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <set>

using namespace std;

typedef pair<int, int> P;

int main() {
  set<P> s;
  int N, L;

  cin >> N >> L;

  int a[N];

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

  for (int i = 0; i <= N - 1; i++) {
    s.insert(P(a[i], i));
    if (0 <= i - L)
      s.erase(P(a[i - L], i - L));
    if (L <= i + 1) {
      P p = *s.begin();
      if (i != N - 1) {
        cout << p.first << " ";
      } else {
        cout << p.first << endl;
      }
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

#define MAX 1000000001

int main() {
  int N, L;
  cin >> N >> L;
  int a[N];
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }

  for (int i = 0; i <= N - L; i++) {
    int min = MAX;
    for (int j = i; j < i + L; j++) {
      if (a[j] < min)
        min = a[j];
    }
    if (i != N - L) {
      cout << min << " ";
    } else {
      cout << min << endl;
    }
  }

  return 0;
}