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

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

#define for_(i, a, b) for (int i = (a); i < (b); ++i)
typedef long long lint;

struct Point {
  int x, y;
  Point operator-(const Point &p) const { return Point{x - p.x, y - p.y}; }
};

int m;
Point p[16];

int cross(Point &a, Point &b) { return a.x * b.y - a.y * b.x; }
bool is_para(Point &a, Point &b) { return cross(a, b) == 0; }

int calc(vector<int> &a, vector<int> &b) {
  int res = 0;
  for_(i, 0, m / 2) for_(j, i + 1, m / 2) {
    Point ai = p[a[i]] - p[b[i]];
    Point bi = p[a[j]] - p[b[j]];
    res += is_para(ai, bi);
  }
  return res;
}

int rec(int i, vector<int> &a, vector<int> &b, vector<bool> &used) {
  if (i == m)
    return calc(a, b);
  if (used[i])
    return rec(i + 1, a, b, used);

  int ret = 0;

  a.push_back(i);
  used[i] = true;

  for_(j, 0, m) {
    if (used[j])
      continue;
    b.push_back(j);
    used[j] = true;
    ret = max(ret, rec(i + 1, a, b, used));
    b.pop_back();
    used[j] = false;
  }

  a.pop_back();
  used[i] = false;

  return ret;
}

int main() {
  cin >> m;
  for_(i, 0, m) cin >> p[i].x >> p[i].y;
  vector<int> a, b;
  vector<bool> used(m, false);
  cout << rec(0, a, b, used) << endl;
}

Error: TLE

Fixed submission:

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

#define for_(i, a, b) for (int i = (a); i < (b); ++i)
typedef long long lint;

struct Point {
  int x, y;
  Point operator-(const Point &p) const { return Point{x - p.x, y - p.y}; }
};

int m;
Point p[16];

int cross(Point &a, Point &b) { return a.x * b.y - a.y * b.x; }
bool is_para(Point &a, Point &b) { return cross(a, b) == 0; }

int calc(vector<int> &a, vector<int> &b) {
  int res = 0;
  for_(i, 0, m / 2) for_(j, i + 1, m / 2) {
    Point ai = p[a[i]] - p[b[i]];
    Point bi = p[a[j]] - p[b[j]];
    res += is_para(ai, bi);
  }
  return res;
}

int main() {
  cin >> m;
  for_(i, 0, m) cin >> p[i].x >> p[i].y;

  set<int> hash_set;
  int x, y, k = m / 2, S = (1 << m) - 1, ans = 0;
  for (int T = (1 << k) - 1; T < (1 << m);
       x = T & -T, y = T + x, T = (((T & ~y) / x) >> 1) | y) {
    if (hash_set.count(T) || hash_set.count(S ^ T))
      continue;

    vector<int> a, b;
    for_(i, 0, m) {
      if (T >> i & 1)
        a.push_back(i);
      else
        b.push_back(i);
    }

    do {
      ans = max(ans, calc(a, b));
    } while (next_permutation(b.begin(), b.end()));

    hash_set.insert(T);
    hash_set.insert(S ^ T);
  }

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

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

const int N = 16;
int x[N], y[N], n;
long long ans;
int chk[N];
vector<int> a[N];
map<pair<int, int>, int> M;

void dfs(int step) {
  if (step == n / 2) {
    long long rlt = 0;
    for (int i = 0; i < N / 2; i++)
      a[i].clear();
    for (int i = 0; i < n; i++)
      a[chk[i]].push_back(i);
    M.clear();
    for (int i = 0; i < n / 2 + 1; i++)
      if (a[i].size() == 2) {
        int dx = abs(x[a[i][0]] - x[a[i][1]]);
        int dy = (y[a[i][0]] - y[a[i][1]]) *
                 ((x[a[i][0]] - x[a[i][1]] < 0) ? -1 : 1);
        int gcd = __gcd(dx, dy);
        dx /= gcd, dy /= gcd;
        M[make_pair(dx, dy)]++;
      }
    for (map<pair<int, int>, int>::iterator it = M.begin(); it != M.end(); it++)
      rlt += 1LL * it->second * (it->second - 1) / 2;
    ans = max(ans, rlt);
    return;
  }
  for (int i = 0; i < n; i++) {
    if (chk[i] != -1)
      continue;
    chk[i] = step;
    for (int j = i + 1; j < n; j++) {
      if (chk[j] != -1)
        continue;
      chk[j] = step;
      dfs(step + 1);
      chk[j] = -1;
    }
    chk[i] = -1;
    break;
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  memset(chk, -1, sizeof chk);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i];
  dfs(0);
  cout << ans << endl;
  return 0;
}


Error: -8

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
#define D(x) cout << #x " = " << (x) << endl
#define MAX 100000

int health[MAX + 5];
const int LIMIT = 1000000000;

int main() {
  freopen("rm.txt", "r", stdin);

  int n, t, i, st = 0;

  scanf("%d %d", &n, &t);
  for (i = 1; i <= n; i++)
    scanf("%d", &health[i]);
  int prev_st_time;

  int prev_ans;
  for (i = 1; i <= n; i++) {
    if (i == 1) {
      printf("%d\\n", prev_ans = t / health[i] + 1);
      prev_st_time = t / health[i] * health[i];
    } else {

      if (t <= st) {
        printf("%d\\n", prev_ans = 0);
        continue;
      }

      if (health[i] >= health[i - 1]) {
        printf("%d\\n", prev_ans = (t - st) / health[i] + 1);
        prev_st_time = st + (t - st) / health[i] * health[i];
      } else {
        int elapsed = t - prev_st_time;
        if (elapsed < health[i])
          printf("%d\\n", prev_ans = max(0, prev_ans - 1));
        else
          printf("%d\\n", prev_ans);

        prev_st_time = prev_st_time + health[i - 1];
      }
    }
    st += health[i];
    if (st > LIMIT)
      st = LIMIT + 1;
  }

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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

ll table[100001], calc_table[100001];

int main() {

  int N;
  ll T;
  scanf("%d %lld", &N, &T);

  table[0] = 0;
  for (int i = 1; i <= N; i++) {
    scanf("%lld", &table[i]);
    table[i] += table[i - 1];
  }

  printf("%lld\\n", T / table[1] + 1);

  if (N == 1) {
    return 0;
  }

  calc_table[0] = 0;
  calc_table[1] = 2 * table[1];

  ll diff;

  for (int i = 2; i <= N; i++) {
    if (calc_table[i - 1] >= table[i]) {
      calc_table[i] = calc_table[i - 1] + (table[i] - table[i - 1]);
    } else {
      calc_table[i] = table[i] + (table[i] - table[i - 1]);
    }
    if (table[i] > T) {
      printf("1\\n");
      continue;
    }

    diff = calc_table[i] - table[i];
    printf("%lld\\n", (T - table[i]) / diff + 2);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

ll table[100001], calc_table[100001], ans[100001];

int main() {

  int N;
  ll T;
  scanf("%d %lld", &N, &T);

  table[0] = 0;
  for (int i = 1; i <= N; i++) {
    scanf("%lld", &table[i]);
  }

  calc_table[0] = 0;

  for (int i = 0; i <= N; i++) {
    calc_table[i] = table[i];
    calc_table[i] += table[i - 1];
  }
  int right = N;

  int item = 1;
  ans[1] = T / table[1] + 1;

  int calc_left, calc_right, m, next_right;

  while (true) {

    calc_left = 1, calc_right = right, m = (calc_left + calc_right) / 2;
    next_right = -1;

    while (calc_left <= calc_right) {
      if (calc_table[m] > T) {
        next_right = m - 1;
        calc_right = m - 1;
      } else {
        calc_left = m + 1;
      }
      m = (calc_left + calc_right) / 2;
    }

    if (next_right != -1) {
      for (int i = next_right + 1; i <= right; i++)
        ans[i] = item;
      if (next_right == 1) {
        break;
      } else {
        right = next_right;
      }
    }

    calc_table[1] = table[1] * (item + 1);
    for (int i = 2; i <= right; i++) {
      if (calc_table[i] <= calc_table[i - 1]) {
        calc_table[i] = calc_table[i - 1] + table[i];
      } else {
        calc_table[i] += table[i];
      }
    }

    item++;
  }

  for (int i = 1; i <= N; i++) {
    printf("%lld\\n", ans[i]);
  }

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

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 100010

int main() {
  int n;
  ll t, h[SIZE];

  scanf("%d%lld", &n, &t);

  ll start = 0;
  ll maxh = 0;
  ll sum = 0;
  ll l = 0;

  for (int i = 0; i < n; i++) {
    scanf("%lld", h + i);

    if (i > 0 && maxh <= h[i]) {
      ll pos = (t - start) / h[l];
      printf("%lld\\n", max(pos + 1, 1LL));
      ll subsum = 0;

      for (int j = l + 1; j < i; j++) {
        subsum += h[j];

        while (t - start < subsum + pos * h[l])
          pos--;

        printf("%lld\\n", max(pos + 1, 1LL));
      }

      l = i;
      start += sum;
      maxh = h[i];
      sum = h[i];
    } else {
      sum += h[i];
      if (i == 0)
        maxh = h[0];
    }
  }

  ll pos = (t - start) / h[l];
  printf("%lld\\n", max(pos + 1, 1LL));
  ll subsum = 0;

  for (int j = l + 1; j < n; j++) {
    subsum += h[j];

    while (t - start < subsum + pos * h[l])
      pos--;

    printf("%lld\\n", max(pos + 1, 1LL));
  }

  return 0;
}


Error: 0
i = 1


Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 100010

int main() {
  int n;
  ll t, h[SIZE];

  scanf("%d%lld", &n, &t);

  ll start = 0;
  ll maxh = 0;
  ll sum = 0;
  ll l = 0;

  for (int i = 0; i < n; i++) {
    scanf("%lld", h + i);

    if (i > 0 && maxh <= h[i]) {
      debug(i);
      ll pos = (t - start) / h[l];
      printf("%lld\\n", max(pos + 1, 1LL));
      ll subsum = 0;

      for (int j = l + 1; j < i; j++) {
        subsum += h[j];

        while (t - start < subsum + pos * h[l])
          pos--;

        printf("%lld\\n", max(pos + 1, 1LL));
      }

      l = i;
      start += sum;
      maxh = h[i];
      sum = h[i];
    } else {
      sum += h[i];
      if (i == 0)
        maxh = h[0];
    }
  }

  ll pos = (t - start) / h[l];
  printf("%lld\\n", max(pos + 1, 1LL));
  ll subsum = 0;

  for (int j = l + 1; j < n; j++) {
    subsum += h[j];

    while (t - start < subsum + pos * h[l])
      pos--;

    printf("%lld\\n", max(pos + 1, 1LL));
  }

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <ostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef unsigned long long ll;
using namespace std;
const int maxn = 100000 + 7;
ll num[maxn];
ll stu[maxn];
ll sum[maxn];
int main() {
  int n;
  ll t;
  memset(sum, 0, sizeof(sum));
  scanf("%d%lld", &n, &t);
  ll maxx = 0;
  sum[0] = 0;
  ll pos;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &stu[i]);
    sum[i] = sum[i - 1] + stu[i];
    maxx = max(maxx, stu[i]);
    if (i == 1) {
      num[i] = (t - sum[i - 1]) / stu[i] + 1;
      continue;
    }
    if (t >= sum[i]) {
      ll time = t - sum[i];
      num[i] = time / maxx + 2;
    } else {
      num[i] = 1;
      continue;
    }
  }
  for (int i = 1; i <= n; i++) {
    printf("%lld\\n", num[i]);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

#define FRER() freopen("i.txt", "r", stdin);

using namespace std;

int main() {
  // FRER();

  int n, t, num, mx = 0, ans;
  scanf("%d %d", &n, &t);
  while (n--) {
    scanf("%d", &num);
    if (t < num)
      ans = 1;
    else if (num >= mx) {
      mx = num;
      ans = t / num + 1;
    } else {
      if (t % mx >= num)
        ans = t / mx + 2;
      else
        ans = t / mx + 1;
    }
    t -= num;
    printf("%d\\n", ans);
    if (ans == 1)
      break;
  }
  if (n == 0)
    return 0;
  while (n--) {
    scanf("%d", &num);
    puts("1");
  }
  return 0;
}
Problem p00959 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
const int maxn = 1e5 + 10;
int a[maxn], b[maxn];

int main() {
  int t, n;
  scanf("%d%d", &n, &t);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int m_ax = a[1];
  for (int i = 1; i <= n; i++) {
    m_ax = max(m_ax, a[i]);
    b[i] = t / m_ax + 1;
    if (t % m_ax >= a[i])
      b[i]++;
    t -= a[i];
    if (t < 0)
      break;
  }
  for (int i = 1; i <= n; i++)
    cout << max(1, b[i]) << endl;
  return 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int n, t;
const int maxn = 100005;
vector<long long> a;
int h[maxn];
long long id[maxn];

int main() {
  scanf("%d%d", &n, &t);
  for (int i = 1; i <= n; i++)
    scanf("%d", &h[i]);
  int cnt = (t / h[1]) + 1;
  a.push_back(0);
  for (int i = 1; i <= cnt; i++) {
    a.push_back(h[1] * i);
  }
  id[1] = cnt;
  int w;
  for (int i = 2; i <= n; i++) // ren
  {
    w = 0;
    for (int j = 1; j <= cnt; j++) // jiancha
    {
      int t1 = a[j];
      a[j] = max(t1, w) + h[i];
      w = a[j];
      if (w > t) {
        id[i] = j;
        break;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    printf("%d\\n", id[i]);
  }
}
Problem p00959 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long int LLI;
typedef pair<int, int> PII;

#define _ ios_base::sync_with_stdio(0);
#define debug
#define x first
#define y second
#define MXN 100005

const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;

int main() {
  _ int n, t, k, mx;
  LLI sum;
  while (cin >> n >> t) {
    mx = -1;
    sum = 0;
    for (int i = 0; i < n; ++i) {
      cin >> k;
      mx = max(mx, k);
      sum += k;
      if (sum <= t)
        cout << int(1 + ceil((double)(t + 0.5 - sum) / (double)mx)) << "\\n";
      else
        cout << "1\\n";
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

const int maxN = 110;

#define FORU(i, l, r) for (int i = l; i <= r; ++i)
#define FORD(i, r, l) for (int i = r; i >= l; --i)
#define REPU(i, r) for (int i = 0; i < r; ++i)
#define F first
#define S second
#define PB push_back
#define LL long long
#define BIT(x, i) ((x >> i) & 1)

int res[maxN], a[maxN], n, t;

int main() {
  cin >> n >> t;
  int s = 0, p = 0, ss = 0, c = 0, maxa = 0;
  FORU(i, 1, n) {
    cin >> a[i];
    if (a[i] > maxa) {
      res[i] = ((t - s) / a[i]) + 1;
      c = s + (res[i] - 1) * a[i];
      maxa = a[i];
      p = s;
      ss = 0;
    } else {
      ss += a[i];
      res[i] = res[i - 1] - 1;
      if (c + a[i] <= t)
        ++res[i], c += a[i];
      else
        c = p + maxa * (res[i] - 1) + ss;
    }
    s += a[i];
  }
  FORU(i, 1, n) cout << res[i] << endl;
}
Problem p00961 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
// dp[i]:前i个位置染完的最小花费
// dp[0] = 0, dp[others] = INF
// if(a[i] == b[i]) dp[i] = dp[i-1]
// else dp[i] = min{dp[j] + cost(j+1, i)|max(0, i-k) <= j < i}
// cost(x, y):ceil((sum[i]-sum[j+1])/2)+1=(sum[i]-sum[j+1]+1)/2+1
// dp[i]=min{(2*dp[j]-sum[j+1]+sum[i]+3)/2|max(0, i-k) <= j < i}
// f[j] = 2*dp[j] - sum[j+1]//用长度为k的单调队列维护
// dp[i]=min{(f[j]+sum[i]+3)/2}
// f[i] = 2*dp[i]-sum[i+1]入队
const int maxn = 5e5 + 10;
const int INF = 0x3f3f3f3f;
int n, k;
char s[maxn], t[maxn];
int sum[maxn];
int dp[maxn];
struct Queue {
  struct Node {
    int pos, val;
  } node[maxn];
  int l, r;
  void init() { l = 1, r = 0; }
  void update(int pos, int val) {
    while (l <= r && val < node[r].val)
      r--;
    node[++r] = Node{pos, val};
    while (l <= r && node[r].pos - node[l].pos >= k)
      l++;
  }
  int query() {
    if (l > r)
      return INF;
    return node[l].val;
  }
} que;
int main() {
  scanf("%d%d", &n, &k);
  scanf("%s%s", s + 1, t + 1);
  sum[0] = 0, t[0] = '#';
  for (int i = 1; i <= n; i++)
    sum[i] = sum[i - 1] + (t[i] != t[i - 1]);
  dp[0] = 0;
  que.init();
  que.update(0, 2 * dp[0] - sum[1]);
  for (int i = 1; i <= n; i++) {
    dp[i] = INF;
    if (s[i] == t[i])
      dp[i] = dp[i - 1];
    else {
      // cout << i << ":" << que.query() << endl;
      dp[i] = min(dp[i], (que.query() + sum[i] + 3) / 2);
    }
    que.update(i, 2 * dp[i] - sum[i + 1]);
    // cout << dp[i] << "test" << endl;
  }
  printf("%d\\n", dp[n]);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define LL long long
#define N 100100
#define inf 1000000007
using namespace std;

char s[N], t[N];
int f[N], pre[N][2], tmp[N][2], cnt[N];
int n, k, l, r;

struct node1 {
  int p[N], v[N];
  int l, r;
  void init() {
    p[1] = v[1] = 0;
    l = r = 1;
  }

  void ins(int x, int y) {
    while (l <= r && y <= v[r])
      r--;
    p[++r] = x;
    v[r] = y;
  }

  int get(int x) {
    while (l <= r && x - p[l] > k)
      l++;
    return v[l];
  }
} que[2];

int main() {
  scanf("%d %d\\n", &n, &k);
  scanf("%s%s", t + 1, s + 1);
  for (int c = 0; c <= 1; c++) {
    char ch;
    if (c == 0)
      ch = 'W';
    else
      ch = 'B';
    for (int i = 1; i <= n; i++) {
      cnt[i] = cnt[i - 1];
      if (s[i] == ch && s[i - 1] != s[i])
        cnt[i]++;
      pre[i][c] = cnt[i];
      tmp[i][c] = (s[i] == ch && s[i + 1] == ch);
    }
  }
  que[0].init();
  que[1].init();
  f[0] = 0;
  for (int i = 1; i <= n; i++) {
    f[i] = inf;
    if (s[i] == t[i])
      f[i] = f[i - 1];
    for (int c = 0; c <= 1; c++)
      f[i] = min(f[i], que[c].get(i) + 1 + pre[i][c]);
    for (int c = 0; c <= 1; c++)
      que[c].ins(i, f[i] - pre[i][c] + tmp[i][c]);
  }
  printf("%d\\n", f[n]);
}
Problem p00961 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;

struct SegNode {
  int v;

  SegNode(int v) : v(v) {}

  SegNode operator*(const SegNode &r) const { return (v < r.v ? *this : r); }
} e(INF);

struct SegmentTree {
  int sz;
  vector<SegNode> seg;

  SegmentTree(int n) {
    sz = 1;
    while (sz < n)
      sz <<= 1;
    seg.assign(2 * sz - 1, e);
  }

  void update(int k, const SegNode &x) {
    k += sz - 1;
    seg[k] = x;
    while (k > 0) {
      k = (k - 1) >> 1;
      seg[k] = seg[2 * k + 1] * seg[2 * k + 2];
    }
  }

  SegNode query(int a, int b, int k, int l, int r) {
    if (a >= r || b <= l)
      return (e);
    if (a <= l && r <= b)
      return (seg[k]);
    return (query(a, b, 2 * k + 1, l, (l + r) >> 1) *
            query(a, b, 2 * k + 2, (l + r) >> 1, r));
  }

  SegNode query(int a, int b) { return (query(a, b, 0, 0, sz)); }
};

int main() {
  int N, K;
  string S, T;

  cin >> N >> K;

  cin >> S;
  cin >> T;

  vector<int> sweep(N + 1);
  for (int i = 0; i + 1 < N; i++) {
    if (T[i] != T[i + 1])
      sweep[i + 1] = 1;
    sweep[i + 1] += sweep[i];
  }

  SegmentTree seg(N);
  vector<int> dp(N + 1, INF);
  dp[0] = 0;
  seg.update(0, 0);
  for (int i = 1; i <= N; i++) {
    dp[i] = dp[i - 1] + (S[i - 1] != T[i - 1]);
    int ret = seg.query(i - K, i - 1).v;
    dp[i] = min(dp[i], (ret + 3 + sweep[i - 1]) / 2);
    seg.update(i, dp[i] * 2 - sweep[i]);
  }

  cout << dp.back() << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;

int main() {
  int N, K;
  string S, T;

  cin >> N >> K;

  cin >> S;
  cin >> T;

  vector<int> sweep(N + 1);
  for (int i = 0; i + 1 < N; i++) {
    if (T[i] != T[i + 1])
      sweep[i + 1] = 1;
    sweep[i + 1] += sweep[i];
  }

  vector<int> dp(N + 1, INF);
  dp[0] = 0;
  for (int i = 1; i <= N; i++) {
    dp[i] = dp[i - 1] + (S[i - 1] != T[i - 1]);
    for (int j = i - 2; j >= 0 && i - j <= K; j--) {
      dp[i] = min(dp[i], (dp[j] * 2 + sweep[i - 1] - sweep[j] /*sum*/ + 3) / 2);
    }
  }

  cout << dp.back() << endl;
}
Problem p00963 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

static const double EPS = 1e-8;
const double DTR = (2 * acos(-1)) / 360;

enum ans {
  ABC,
  ACD,
  ADB,
  BCD,
};

int solve() {
  string xy;
  cin >> xy;
  int d, l;
  scanf("%d%d", &d, &l);
  double base = xy == "BC" ? -60 : xy == "CD" ? 0 : xy == "DB" ? 60 : 0;
  double theta = (base + d) * DTR;
  double y = l * sin(theta);
  double x = l * cos(theta);
  double z60 = x + sqrt(3) / 3.0 * y;
  double z120 = -x + sqrt(3) / 3.0 * y;
  z60 = fmod(fmod(z60, 2.0) + 2.0 + EPS, 2.0);
  z120 = fmod(fmod(z120, 2.0) + 2.0 + EPS, 2.0);
  y = (z60 + z120) * sqrt(3) / 2.0;
  if (z60 < 1.0) {
    if (z120 < 1.0) {
      if (y < sqrt(3) / 2.0) {
        return ADB;
      } else {
        return BCD;
      }
    } else {
      if (y < sqrt(3)) {
        return ABC;
      } else {
        return ACD;
      }
    }
  } else {
    if (z120 < 1.0) {
      if (y < sqrt(3)) {
        return ACD;
      } else {
        return ABC;
      }
    } else {
      if (y < 3.0 * sqrt(3) / 2.0) {
        return BCD;
      } else {
        return ADB;
      }
    }
  }
}

int main() {
  int a = solve(), b = solve();
  puts(a == b ? "YES" : "NO");
  return 0;
}

Error: 0
y = 0.5
y = 2.23205
y = 1
y = 1


Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

static const double EPS = 1e-8;
const double DTR = (2 * acos(-1)) / 360;

enum ans {
  ABC,
  ACD,
  ADB,
  BCD,
};

int solve() {
  string xy;
  cin >> xy;
  int d, l;
  scanf("%d%d", &d, &l);
  double base = xy == "BC" ? -60 : xy == "CD" ? 0 : xy == "DB" ? 60 : 0;
  double theta = (base + d) * DTR;
  double y = l * sin(theta);
  double x = l * cos(theta);
  double z60 = x + sqrt(3) / 3.0 * y;
  double z120 = -x + sqrt(3) / 3.0 * y;
  z60 = fmod(fmod(z60, 2.0) + 2.0 + EPS, 2.0);
  z120 = fmod(fmod(z120, 2.0) + 2.0 + EPS, 2.0);
  cerr << "y = " << y << endl;
  y = (z60 + z120) * sqrt(3) / 2.0;
  cerr << "y = " << y << endl;
  if (z60 < 1.0) {
    if (z120 < 1.0) {
      if (y < sqrt(3) / 2.0) {
        return ADB;
      } else {
        return BCD;
      }
    } else {
      if (y < sqrt(3)) {
        return ABC;
      } else {
        return ACD;
      }
    }
  } else {
    if (z120 < 1.0) {
      if (y < sqrt(3)) {
        return ACD;
      } else {
        return ABC;
      }
    } else {
      if (y < 3.0 * sqrt(3) / 2.0) {
        return BCD;
      } else {
        return ADB;
      }
    }
  }
}

int main() {
  int a = solve(), b = solve();
  puts(a == b ? "YES" : "NO");
  return 0;
}