prithivMLmods/Coder-Stat huggingface dataset:
Problem p03707 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

int64_t &f(int *i) { return *(int64_t *)i; }

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

  int n, m, q;
  cin >> n >> m >> q;

  char s[3][2001] = {};
  int k = 1;
  vector<int[2001]> v(n + 1);
  vector<int[2001][2]> e(n + 1);
  for (int i = 0; i < n; i++) {
    cin >> s[k];
    int t = 0;
    for (int j = 0; j < m; j++) {
      v[i + 1][j + 1] = v[i][j + 1] + (t += (s[k][j] -= '0'));
      f(e[i + 1][j + 1]) = f(e[i][j + 1]) + f(e[i + 1][j]) - f(e[i][j]) +
                           (s[k][j] & s[k][j - 1]) +
                           ((int64_t)(s[k][j] & s[k ^ 3][j]) << 32);
    }
    k ^= 3;
  }

  for (int i = 0; i < q; i++) {
    int x0, y0, x1, y1;
    scanf("%d%d%d%d", &y0, &x0, &y1, &x1);
    int x2 = x0 - 1, y2 = y0 - 1;

    cout << (v[y1][x1] - v[y1][x2] - v[y2][x1] + v[y2][x2]) -
                (e[y1][x1][0] - e[y1][x0][0] - e[y2][x1][0] + e[y2][x0][0]) -
                (e[y1][x1][1] - e[y1][x2][1] - e[y0][x1][1] + e[y0][x2][1])
         << '\n';
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

int64_t &f(int *i) { return *(int64_t *)i; }

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

  char s[3][2001] = {};
  int k = 1;
  vector<int[2001]> v(n + 1);
  vector<int[2001][2]> e(n + 1);
  for (int i = 0; i < n; i++) {
    cin >> s[k];
    int t = 0;
    for (int j = 0; j < m; j++) {
      v[i + 1][j + 1] = v[i][j + 1] + (t += (s[k][j] -= '0'));
      f(e[i + 1][j + 1]) = f(e[i][j + 1]) + f(e[i + 1][j]) - f(e[i][j]) +
                           (s[k][j] & s[k][j - 1]) +
                           ((int64_t)(s[k][j] & s[k ^ 3][j]) << 32);
    }
    k ^= 3;
  }

  for (int i = 0; i < q; i++) {
    int x0, y0, x1, y1;
    scanf("%d%d%d%d", &y0, &x0, &y1, &x1);
    int x2 = x0 - 1, y2 = y0 - 1;

    cout << (v[y1][x1] - v[y1][x2] - v[y2][x1] + v[y2][x2]) -
                (e[y1][x1][0] - e[y1][x0][0] - e[y2][x1][0] + e[y2][x0][0]) -
                (e[y1][x1][1] - e[y1][x2][1] - e[y0][x1][1] + e[y0][x2][1])
         << '\n';
  }

  return 0;
}
Problem p03707 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define y1 fvck_y1
using namespace std;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

int B1[4010][4010], B2[4010][4010];
char sd[10000];
int n, m, q;
int main() {
  freopen("read.in", "r", stdin);
  n = read();
  m = read();
  q = read();
  for (int i = 1; i <= n; i++) {
    scanf("%s", sd);
    for (int j = 0; j < m; j++)
      B1[i][j + 1] = sd[j] - '0';
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (j != m)
        B2[i * 2 - 1][j * 2] = B1[i][j] & B1[i][j + 1];
      if (i != n)
        B2[i * 2][j * 2 - 1] = B1[i][j] & B1[i + 1][j];
    }
  }
  for (int i = 1; i < n * 2; i++)
    for (int j = 1; j < m * 2; j++)
      B2[i][j] += B2[i - 1][j] + B2[i][j - 1] - B2[i - 1][j - 1];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      B1[i][j] += B1[i - 1][j] + B1[i][j - 1] - B1[i - 1][j - 1];
  for (int i = 1; i <= q; i++) {
    int x1 = read(), y1 = read(), x2 = read(), y2 = read();
    int X = B1[x2][y2] + B1[x1 - 1][y1 - 1] - B1[x1 - 1][y2] - B1[x2][y1 - 1];
    int Y = B2[x2 * 2 - 1][y2 * 2 - 1] + B2[x1 * 2 - 2][y1 * 2 - 2] -
            B2[x1 * 2 - 2][y2 * 2 - 1] - B2[x2 * 2 - 1][y1 * 2 - 2];
    printf("%d\n", X - Y);
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define y1 fvck_y1
using namespace std;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

int B1[4010][4010], B2[4010][4010];
char sd[10000];
int n, m, q;
int main() {
  // freopen("read.in","r",stdin);
  n = read();
  m = read();
  q = read();
  for (int i = 1; i <= n; i++) {
    scanf("%s", sd);
    for (int j = 0; j < m; j++)
      B1[i][j + 1] = sd[j] - '0';
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (j != m)
        B2[i * 2 - 1][j * 2] = B1[i][j] & B1[i][j + 1];
      if (i != n)
        B2[i * 2][j * 2 - 1] = B1[i][j] & B1[i + 1][j];
    }
  }
  for (int i = 1; i < n * 2; i++)
    for (int j = 1; j < m * 2; j++)
      B2[i][j] += B2[i - 1][j] + B2[i][j - 1] - B2[i - 1][j - 1];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      B1[i][j] += B1[i - 1][j] + B1[i][j - 1] - B1[i - 1][j - 1];
  for (int i = 1; i <= q; i++) {
    int x1 = read(), y1 = read(), x2 = read(), y2 = read();
    int X = B1[x2][y2] + B1[x1 - 1][y1 - 1] - B1[x1 - 1][y2] - B1[x2][y1 - 1];
    int Y = B2[x2 * 2 - 1][y2 * 2 - 1] + B2[x1 * 2 - 2][y1 * 2 - 2] -
            B2[x1 * 2 - 2][y2 * 2 - 1] - B2[x2 * 2 - 1][y1 * 2 - 2];
    printf("%d\n", X - Y);
  }

  return 0;
}
Problem p03707 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 1010
using namespace std;
char a[N][N];
int s[N][N], sh[N][N], sv[N][N];
int main() {
  int n, m, x1, y1, x2, y2, q;
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 1; i <= n; i++) {
    scanf("%s", a[i] + 1);
    for (int j = 1; j <= m; j++) {
      s[i][j] = s[i - 1][j] + s[i][j - 1] + (a[i][j] - '0') - s[i - 1][j - 1];
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < m; j++) {
      sh[i][j] = sh[i - 1][j] + sh[i][j - 1] - sh[i - 1][j - 1];
      if (a[i][j] == '1' && a[i][j + 1] == '1') {
        sh[i][j]++;
      }
    }
  }
  for (int i = 1; i < n; i++) {
    for (int j = 1; j <= m; j++) {
      sv[i][j] = sv[i - 1][j] + sv[i][j - 1] - sv[i - 1][j - 1];
      if (a[i][j] == '1' && a[i + 1][j] == '1') {
        sv[i][j]++;
      }
    }
  }
  while (q--) {
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    x1--, y1--;
    int ans = s[x2][y2] - s[x1][y2] - s[x2][y1] + s[x1][y1];
    ans -= sh[x2][y2 - 1] - sh[x2][y1] - sh[x1][y2 - 1] + sh[x1][y1];
    ans -= sv[x2 - 1][y2] - sv[x2 - 1][y1] - sv[x1][y2] + sv[x1][y1];
    printf("%d\n", ans);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define N 2010
using namespace std;
char a[N][N];
int s[N][N], sh[N][N], sv[N][N];
int main() {
  int n, m, x1, y1, x2, y2, q;
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 1; i <= n; i++) {
    scanf("%s", a[i] + 1);
    for (int j = 1; j <= m; j++) {
      s[i][j] = s[i - 1][j] + s[i][j - 1] + (a[i][j] - '0') - s[i - 1][j - 1];
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < m; j++) {
      sh[i][j] = sh[i - 1][j] + sh[i][j - 1] - sh[i - 1][j - 1];
      if (a[i][j] == '1' && a[i][j + 1] == '1') {
        sh[i][j]++;
      }
    }
  }
  for (int i = 1; i < n; i++) {
    for (int j = 1; j <= m; j++) {
      sv[i][j] = sv[i - 1][j] + sv[i][j - 1] - sv[i - 1][j - 1];
      if (a[i][j] == '1' && a[i + 1][j] == '1') {
        sv[i][j]++;
      }
    }
  }
  while (q--) {
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    x1--, y1--;
    int ans = s[x2][y2] - s[x1][y2] - s[x2][y1] + s[x1][y1];
    ans -= sh[x2][y2 - 1] - sh[x2][y1] - sh[x1][y2 - 1] + sh[x1][y1];
    ans -= sv[x2 - 1][y2] - sv[x2 - 1][y1] - sv[x1][y2] + sv[x1][y1];
    printf("%d\n", ans);
  }
  return 0;
}
Problem p03707 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};
const int dc[] = {1, 2, 3, 4};

const int MaxN = 2e2 + 22;

int a[MaxN][MaxN];
int dp[MaxN][MaxN][5];

int n, m, q;

void dfs(int x, int y, int c = 0) {
  if (!a[x][y])
    return;
  a[x][y] = 0;
  ++dp[x][y][c];
  for (int i = 0; i < 4; ++i)
    dfs(x + dx[i], y + dy[i], dc[i]);
}

void precalc() {
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (a[i][j])
        dfs(i, j);

  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      for (int k = 0; k <= 4; ++k)
        dp[i][j][k] += dp[i - 1][j][k] + dp[i][j - 1][k] - dp[i - 1][j - 1][k];
}

int get(int x, int y, int xx, int yy, int k) {
  if (y > yy || x > xx)
    return 0;
  return dp[xx][yy][k] - dp[x - 1][yy][k] - dp[xx][y - 1][k] +
         dp[x - 1][y - 1][k];
}

int solve(int x, int y, int xx, int yy) {
  return get(x, y, xx, yy, 0) + get(x, y, x, yy, 4) + get(xx, y, xx, yy, 1) +
         get(x, y, xx, y, 3) + get(x, yy, xx, yy, 2);
}

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m >> q;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      char q;
      cin >> q;
      a[i][j] = q - '0';
    }
  precalc();
  while (q-- > 0) {
    int x, y, xx, yy;
    cin >> x >> y >> xx >> yy;
    cout << solve(x, y, xx, yy) << '\n';
  }
  return 0;
}


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

const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};
const int dc[] = {1, 2, 3, 4};

const int MaxN = 2e3 + 23;

int a[MaxN][MaxN];
int dp[MaxN][MaxN][5];

int n, m, q;

void dfs(int x, int y, int c = 0) {
  if (!a[x][y])
    return;
  a[x][y] = 0;
  ++dp[x][y][c];
  for (int i = 0; i < 4; ++i)
    dfs(x + dx[i], y + dy[i], dc[i]);
}

void precalc() {
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (a[i][j])
        dfs(i, j);

  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      for (int k = 0; k <= 4; ++k)
        dp[i][j][k] += dp[i - 1][j][k] + dp[i][j - 1][k] - dp[i - 1][j - 1][k];
}

int get(int x, int y, int xx, int yy, int k) {
  if (y > yy || x > xx)
    return 0;
  return dp[xx][yy][k] - dp[x - 1][yy][k] - dp[xx][y - 1][k] +
         dp[x - 1][y - 1][k];
}

int solve(int x, int y, int xx, int yy) {
  return get(x, y, xx, yy, 0) + get(x, y, x, yy, 4) + get(xx, y, xx, yy, 1) +
         get(x, y, xx, y, 3) + get(x, yy, xx, yy, 2);
}

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m >> q;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      char q;
      cin >> q;
      a[i][j] = q - '0';
    }
  precalc();
  while (q-- > 0) {
    int x, y, xx, yy;
    cin >> x >> y >> xx >> yy;
    cout << solve(x, y, xx, yy) << '\n';
  }
  return 0;
}
Problem p03707 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
using namespace std;

const int MAX = 1000 + 10;

int rec[MAX][MAX];
char arr[MAX];

int sa[MAX][MAX];
int sb[MAX][MAX];
int sc[MAX][MAX];

int main() {

  int n, m, q;
  scanf("%d %d %d", &n, &m, &q);

  for (int i = 0; i < n; i++) {
    scanf("%s", arr);
    for (int j = 0; j < m; j++) {
      if (arr[j] == '1')
        rec[i + 1][j + 1] = 1;
      else
        rec[i + 1][j + 1] = 0;
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (rec[i][j] == 1 && rec[i + 1][j] == 1) {
        sb[i][j] = 1;
      }
      if (rec[i][j] == 1 && rec[i][j + 1] == 1) {
        sc[i][j] = 1;
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      sa[i][j] = rec[i][j] + sa[i - 1][j] + sa[i][j - 1] - sa[i - 1][j - 1];
      sb[i][j] = sb[i][j] + sb[i - 1][j] + sb[i][j - 1] - sb[i - 1][j - 1];
      sc[i][j] = sc[i][j] + sc[i - 1][j] + sc[i][j - 1] - sc[i - 1][j - 1];
    }
  }

  while (q--) {
    int x1, y1, x2, y2;
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);

    int ca = sa[x2][y2] - sa[x2][y1 - 1] - sa[x1 - 1][y2] + sa[x1 - 1][y1 - 1];
    int cb = sb[x2 - 1][y2] - sb[x2 - 1][y1 - 1] - sb[x1 - 1][y2] +
             sb[x1 - 1][y1 - 1];
    int cc = sc[x2][y2 - 1] - sc[x2][y1 - 1] - sc[x1 - 1][y2 - 1] +
             sc[x1 - 1][y1 - 1];
    printf("%d\n", ca - cb - cc);
  }

  return 0;
}

Fixed submission:
#include <cstdio>
using namespace std;

const int MAX = 2000 + 10;

int rec[MAX][MAX];
char arr[MAX];

int sa[MAX][MAX];
int sb[MAX][MAX];
int sc[MAX][MAX];

int main() {

  int n, m, q;
  scanf("%d %d %d", &n, &m, &q);

  for (int i = 0; i < n; i++) {
    scanf("%s", arr);
    for (int j = 0; j < m; j++) {
      if (arr[j] == '1')
        rec[i + 1][j + 1] = 1;
      else
        rec[i + 1][j + 1] = 0;
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (rec[i][j] == 1 && rec[i + 1][j] == 1) {
        sb[i][j] = 1;
      }
      if (rec[i][j] == 1 && rec[i][j + 1] == 1) {
        sc[i][j] = 1;
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      sa[i][j] = rec[i][j] + sa[i - 1][j] + sa[i][j - 1] - sa[i - 1][j - 1];
      sb[i][j] = sb[i][j] + sb[i - 1][j] + sb[i][j - 1] - sb[i - 1][j - 1];
      sc[i][j] = sc[i][j] + sc[i - 1][j] + sc[i][j - 1] - sc[i - 1][j - 1];
    }
  }

  while (q--) {
    int x1, y1, x2, y2;
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);

    int ca = sa[x2][y2] - sa[x2][y1 - 1] - sa[x1 - 1][y2] + sa[x1 - 1][y1 - 1];
    int cb = sb[x2 - 1][y2] - sb[x2 - 1][y1 - 1] - sb[x1 - 1][y2] +
             sb[x1 - 1][y1 - 1];
    int cc = sc[x2][y2 - 1] - sc[x2][y1 - 1] - sc[x1 - 1][y2 - 1] +
             sc[x1 - 1][y1 - 1];
    printf("%d\n", ca - cb - cc);
  }

  return 0;
}
Problem p03708 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define ll long long
#define M 200010
#define mmp make_pair
using namespace std;
ll read() {
  ll nm = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    nm = nm * 10 + c - '0';
  return nm * f;
}
ll l, r, maxx, poww[M];
bool vis[M];
int main() {
  l = read(), r = read();
  poww[0] = 1;
  for (int i = 1; i <= 60; i++)
    poww[i] = poww[i - 1] << 1;
  int d = 60;
  while ((l & poww[d]) == (r & poww[d]))
    d--;
  l &= poww[d + 1] - 1, r &= poww[d + 1] - 1;
  int k = d - 1;
  while (k >= 0 && !(r & poww[k]))
    k--;
  ll l1 = l, r1 = poww[d] + poww[k + 1] - 1, l2 = poww[d] + l,
     r2 = poww[d + 1] - 1;
  l2 = max(l2, r1 + 1);
  cout << r1 - l1 + 1 + max(0ll, r2 - l2 + 1) << "\n";
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define ll long long
#define M 200010
#define mmp make_pair
using namespace std;
ll read() {
  ll nm = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    nm = nm * 10 + c - '0';
  return nm * f;
}
ll l, r, maxx, poww[M];
bool vis[M];
int main() {
  l = read(), r = read();
  if (l == r) {
    cout << 1;
    return 0;
  }
  poww[0] = 1;
  for (int i = 1; i <= 60; i++)
    poww[i] = poww[i - 1] << 1;
  int d = 60;
  while ((l & poww[d]) == (r & poww[d]))
    d--;
  l &= poww[d + 1] - 1, r &= poww[d + 1] - 1;
  int k = d - 1;
  while (k >= 0 && !(r & poww[k]))
    k--;
  ll l1 = l, r1 = poww[d] + poww[k + 1] - 1, l2 = poww[d] + l,
     r2 = poww[d + 1] - 1;
  l2 = max(l2, r1 + 1);
  cout << r1 - l1 + 1 + max(0ll, r2 - l2 + 1) << "\n";
  return 0;
}
Problem p03708 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

long long a, b;

long long solve();

int main() {
  cin >> a >> b;
  cout << solve() << endl;
  return 0;
}

long long solve() {
  long long res = 0, dig = 0, x, cnt = 0;
  while ((1LL << (dig + 1)) <= b)
    ++dig;
  while ((a >> dig & 1) == (b >> dig & 1)) {
    a &= (1LL << dig) - 1;
    b &= (1LL << dig) - 1;
    --dig;
  }
  if (b == 0)
    return 1;
  res = (1LL << dig) - a;
  x = b - (1LL << dig);
  while (x)
    x >>= 1, ++cnt;
  res += min(1LL << dig, res + (1LL << cnt));
  return res;
}

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

long long a, b;

long long solve();

int main() {
  cin >> a >> b;
  cout << solve() << endl;
  return 0;
}

long long solve() {
  long long res = 0, dig = 0, x, cnt = 0;
  while ((1LL << (dig + 1)) <= b)
    ++dig;
  while (b && (a >> dig & 1) == (b >> dig & 1)) {
    a &= (1LL << dig) - 1;
    b &= (1LL << dig) - 1;
    --dig;
  }
  if (b == 0)
    return 1;
  res = (1LL << dig) - a;
  x = b - (1LL << dig);
  while (x)
    x >>= 1, ++cnt;
  res += min(1LL << dig, res + (1LL << cnt));
  return res;
}
Problem p03708 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define int long long
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int A, B;

int g(int l, int r, int k, bool zero) {
  int K = (1LL << (k - 1));
  int br = 2 * K - r;
  if (l >= K || l + 1 >= br)
    return 2 * K;
  int s = r + 1;
  int e = 0;
  rep(j, 60) if (l & (1LL << j)) e = j + 1;
  s += 1LL << e;
  assert(s <= 2 * K);
  // cout<<"g("<<l<<", "<<r<<", k="<<k<<") = "<<s<<"\n";
  return s;
}

int f(int l, int r, int k, bool zero) {
  if (k == 0)
    return r - l + 1;
  // cout<<"f("<<l<<", "<<r<<", "<<k<<")\n";
  int mask = 1LL << k;
  if ((r & mask) == 0)
    return f(l, r, k - 1, zero);
  if ((l & mask) > 0 && (r & mask) > 0)
    return f(l - mask, r - mask, k - 1, false);
  return f(l, mask - 1, k, zero) + g(r - mask, mask - 1 - l, k, false);
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> A >> B;
  cout << f(A, B, 60, true) << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define int long long
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int A, B;

int g(int l, int r, int k, bool zero) {
  int K = (1LL << (k - 1));
  int br = 2 * K - r;
  if (l >= K || l + 1 >= br)
    return 2 * K;
  int s = r + 1;
  int e = 0;
  rep(j, 60) if (l & (1LL << j)) e = j + 1;
  s += 1LL << e;
  s = min(s, 2 * K);
  assert(s <= 2 * K);
  // cout<<"g("<<l<<", "<<r<<", k="<<k<<") = "<<s<<"\n";
  return s;
}

int f(int l, int r, int k, bool zero) {
  if (k == 0)
    return r - l + 1;
  // cout<<"f("<<l<<", "<<r<<", "<<k<<")\n";
  int mask = 1LL << k;
  if ((r & mask) == 0)
    return f(l, r, k - 1, zero);
  if ((l & mask) > 0 && (r & mask) > 0)
    return f(l - mask, r - mask, k - 1, false);
  return f(l, mask - 1, k, zero) + g(r - mask, mask - 1 - l, k, false);
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> A >> B;
  cout << f(A, B, 60, true) << "\n";
  return 0;
}
Problem p03708 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define L long long
using namespace std;
L l, r;
inline L calc(int i, L l, L r, bool t) {
  if (l == -1 && r == -1)
    return 0;
  if (t && r == -1)
    return 0;
  if (i == -1)
    return 1;
  if (r == -1) {
    if (l == -1)
      return 0;
    if (l == 0)
      return 1;
    int k;
    for (k = i; !(l & (1ll << k)); k--)
      ;
    return (1ll << k + 1);
  }
  if (!r)
    return (1ll << i + 1);
  L k = 0;
  k += calc(i - 1, min(l, (1ll << i) - 1), r < (1ll << i) ? r : -1, t);
  if (r <= (1ll << i))
    k += calc(i - 1, min(l, (1ll << i) - 1), 0, 0);
  else if (l >= (1ll << i))
    k += calc(i - 1, min(l, (1ll << i) - 1), r - (1ll << i), t);
  else
    k += calc(i - 1, min(l, (1ll << i) - 1), r - (1ll << i), 1);
  return k;
}
int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  int i;
  scanf("%lld%lld", &l, &r);
  for (i = 60; (l & (1ll << i)) == (r & (1ll << i)); i--)
    if (l & (1ll << i))
      l -= 1ll << i, r -= 1ll << i;
  printf("%lld\n", calc(i - 1, r - (1ll << i), l, 0) + calc(i - 1, -1, l, 0));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define L long long
using namespace std;
L l, r;
inline L calc(int i, L l, L r, bool t) {
  if (l == -1 && r == -1)
    return 0;
  if (t && r == -1)
    return 0;
  if (i == -1)
    return 1;
  if (r == -1) {
    if (l == -1)
      return 0;
    if (l == 0)
      return 1;
    int k;
    for (k = i; !(l & (1ll << k)); k--)
      ;
    return (1ll << k + 1);
  }
  if (!r)
    return (1ll << i + 1);
  L k = 0;
  k += calc(i - 1, min(l, (1ll << i) - 1), r < (1ll << i) ? r : -1, t);
  if (r <= (1ll << i))
    k += calc(i - 1, min(l, (1ll << i) - 1), 0, 0);
  else if (l >= (1ll << i))
    k += calc(i - 1, min(l, (1ll << i) - 1), r - (1ll << i), t);
  else
    k += calc(i - 1, min(l, (1ll << i) - 1), r - (1ll << i), 1);
  return k;
}
int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  int i;
  scanf("%lld%lld", &l, &r);
  if (l == r) {
    printf("1\n");
    return 0;
  }
  for (i = 60; (l & (1ll << i)) == (r & (1ll << i)); i--)
    if (l & (1ll << i))
      l -= 1ll << i, r -= 1ll << i;
  printf("%lld\n", calc(i - 1, r - (1ll << i), l, 0) + calc(i - 1, -1, l, 0));
  return 0;
}
Problem p03708 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

#define X first
#define Y second

const int N = 2e3 + 10;

ll solve(ll L, ll R) {
  int st = 61;
  if (L == R)
    return 1;
  while ((L & (1LL << st)) == (R & (1LL << st))) {
    L &= ~(1LL << st);
    R &= ~(1LL << st);
    st--;
  }
  int p1 = st - 1, p2 = st - 1;
  ///
  while (p1 >= 0 && !(L & (1LL << p1)))
    p1--;
  if (p1 == -1)
    return 1LL << (st + 1);
  ///
  while (p2 >= 0 && !(R & (1LL << p2)))
    p2--;
  if (p2 == -1)
    return (R - L) * 2 + 1;
  if (p1 <= p2)
    return (1LL << (st + 1)) - L;
  int len = st - p1 - 1 + p2 + 1, rem = p1 - p2;
  /// < (1<<st)
  assert(L < (1 << st));
  ll ans = (1LL << st) - L;
  ///>= (1<<st)
  ans += (1LL << len) +
         (1LL << (p2 + 1)) * ((1LL << (st - p1 - 1)) - 1) * ((1LL << rem) - 1);
  ans += (1LL << (p1 + 1)) - (L & ((1LL << (p1 + 1)) - 1));
  return ans;
}
int main() {
  ll L, R;
  cin >> L >> R;
  cout << solve(L, R);
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

#define X first
#define Y second

const int N = 2e3 + 10;

ll solve(ll L, ll R) {
  int st = 61;
  if (L == R)
    return 1;
  while ((L & (1LL << st)) == (R & (1LL << st))) {
    L &= ~(1LL << st);
    R &= ~(1LL << st);
    st--;
  }
  int p1 = st - 1, p2 = st - 1;
  ///
  while (p1 >= 0 && !(L & (1LL << p1)))
    p1--;
  if (p1 == -1)
    return 1LL << (st + 1);
  ///
  while (p2 >= 0 && !(R & (1LL << p2)))
    p2--;
  if (p2 == -1)
    return (R - L) * 2 + 1;
  if (p1 <= p2)
    return (1LL << (st + 1)) - L;
  int len = st - p1 - 1 + p2 + 1, rem = p1 - p2;
  /// < (1<<st)
  assert(L < (1LL << st));
  ll ans = (1LL << st) - L;
  ///>= (1<<st)
  ans += (1LL << len) +
         (1LL << (p2 + 1)) * ((1LL << (st - p1 - 1)) - 1) * ((1LL << rem) - 1);
  ans += (1LL << (p1 + 1)) - (L & ((1LL << (p1 + 1)) - 1));
  return ans;
}
int main() {
  ll L, R;
  cin >> L >> R;
  cout << solve(L, R);
}
Problem p03708 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "algorithm"
#include "bitset"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

// const long long int MOD = 998244353;
const long long int MOD = 1000000007;
const long double EPS = 1e-7;

long long int N, M, K, H, W, L, R;

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

  cin >> L >> R;
  N = L;
  M = R;
  while (N) {
    H++;
    N >>= 1;
  }
  H--;
  while (M) {
    W++;
    M >>= 1;
  }
  W--;
  while (H == W) {
    L -= 1LL << H;
    R -= 1LL << W;
    N = L;
    M = R;
    H = 0;
    W = 0;
    while (N) {
      H++;
      N >>= 1;
    }
    H--;
    while (M) {
      W++;
      M >>= 1;
    }
    W--;
  }
  // cout << L << " " << R << endl;
  // cout << (1LL << W) << endl;
  long long int l = L + (1LL << W);
  long long int r = (1LL << (W + 1)) - 1;
  //	cout << l << " " << r << endl;
  long long int box = R;
  box -= (1LL << W);
  long long int bag = box;
  int num = 0;
  while (bag) {
    bag /= 2;
    num++;
  }
  // cout << (1LL << num) - 1+(1LL<<W) << endl;
  if (l <= (1LL << num) - 1 + (1LL << W)) {
    cout << (1LL << (W + 1)) - L << endl;
    return 0;
  } else {
    cout << r - l + 1 + (1LL << num) - 1 + (1LL << W) - L + 1 << endl;
  }
  return 0;
}


Fixed submission:
#include "algorithm"
#include "bitset"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

// const long long int MOD = 998244353;
const long long int MOD = 1000000007;
const long double EPS = 1e-7;

long long int N, M, K, H, W, L, R;

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

  cin >> L >> R;
  if (L == R) {
    cout << 1 << endl;
    return 0;
  }
  N = L;
  M = R;
  while (N) {
    H++;
    N >>= 1;
  }
  H--;
  while (M) {
    W++;
    M >>= 1;
  }
  W--;
  while (H == W) {
    L -= 1LL << H;
    R -= 1LL << W;
    N = L;
    M = R;
    H = 0;
    W = 0;
    while (N) {
      H++;
      N >>= 1;
    }
    H--;
    while (M) {
      W++;
      M >>= 1;
    }
    W--;
  }
  // cout << L << " " << R << endl;
  // cout << (1LL << W) << endl;
  long long int l = L + (1LL << W);
  long long int r = (1LL << (W + 1)) - 1;
  //	cout << l << " " << r << endl;
  long long int box = R;
  box -= (1LL << W);
  long long int bag = box;
  int num = 0;
  while (bag) {
    bag /= 2;
    num++;
  }
  // cout << (1LL << num) - 1+(1LL<<W) << endl;
  if (l <= (1LL << num) - 1 + (1LL << W)) {
    cout << (1LL << (W + 1)) - L << endl;
    return 0;
  } else {
    cout << r - l + 1 + (1LL << num) - 1 + (1LL << W) - L + 1 << endl;
  }
  return 0;
}
Problem p03708 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef unsigned long long ll;
typedef pair<int, int> PP;

/*
    freopen("input","r",stdin);
    freopen("output","w",stdout);
*/

ll cal(ll x) {
  // Calculate y that can be get from or operation in (0 - x)
  ll t = 1;
  while (x > 0) {
    t *= 2;
    x /= 2;
  }
  return t - 1;
}

int main() {
  ios::sync_with_stdio(false);
  ll a, b, A, B;
  cin >> a >> b;
  if (a == b)
    return 1;
  A = a;
  B = b;
  ll rem = 1LL << 62;
  while (1 > 0) {
    if (A / rem != B / rem)
      break;
    rem /= 2;
  }
  ll x = a % rem;
  ll y = b % rem;
  ll t = cal(y);
  ll ans;
  if (t >= x) {
    ans = rem - x + rem;
  } else {
    ans = (rem - x) * 2 + (t + 1);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef unsigned long long ll;
typedef pair<int, int> PP;

/*
    freopen("input","r",stdin);
    freopen("output","w",stdout);
*/

ll cal(ll x) {
  // Calculate y that can be get from or operation in (0 - x)
  ll t = 1;
  while (x > 0) {
    t *= 2;
    x /= 2;
  }
  return t - 1;
}

int main() {
  ios::sync_with_stdio(false);
  ll a, b, A, B;
  cin >> a >> b;
  if (a == b) {
    cout << 1 << endl;
    return 0;
  }
  A = a;
  B = b;
  ll rem = 1LL << 62;
  while (1 > 0) {
    if (A / rem != B / rem)
      break;
    rem /= 2;
  }
  ll x = a % rem;
  ll y = b % rem;
  ll t = cal(y);
  ll ans;
  if (t >= x) {
    ans = rem - x + rem;
  } else {
    ans = (rem - x) * 2 + (t + 1);
  }
  cout << ans << endl;
  return 0;
}
Problem p03708 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define ll long long

// aの下からk-bit目の数字を返す(0か1)
int bit(ll a, int k) {
  if (k > 60)
    return 0;
  if (a & (1LL << k))
    return 1;
  else
    return 0;
}

signed main() {
  ll a, b;
  cin >> a >> b;

  if (a == b) {
    cout << 1 << endl;
    return 0;
  }

  int dif = 60;
  while (bit(a, dif) == bit(b, dif))
    --dif;

  ll tmp = (1LL << (dif + 1)) - 1LL;
  a &= tmp;
  b &= tmp;

  ll ans = 0;
  ans += (1LL << dif) - a; // 0～の形

  if (a <= (b - (1LL << dif))) {
    ans += (1LL << dif);
    cout << ans << endl;
  } else {
    b -= (1LL << dif);
    ll br1, br2;
    int br1_ = 60;
    while (bit(b, br1_) == 0)
      --br1_;
    br1 = (1LL << (br1_ + 1)) - 1;
    br2 = a;
    if (br2 > br1) {
      ans += (1LL << (dif)) - (br2 - br1 - 1);
      cout << ans << endl;
    } else {
      ans += (1LL << (dif));
      cout << ans << endl;
    }
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define ll long long

// aの下からk-bit目の数字を返す(0か1)
int bit(ll a, int k) {
  if (k > 60)
    return 0;
  if (a & (1LL << k))
    return 1;
  else
    return 0;
}

signed main() {
  ll a, b;
  cin >> a >> b;

  if (a == b) {
    cout << 1 << endl;
    return 0;
  }

  int dif = 60;
  while (bit(a, dif) == bit(b, dif))
    --dif;

  ll tmp = (1LL << (dif + 1)) - 1LL;
  a &= tmp;
  b &= tmp;

  ll ans = 0;
  ans += (1LL << dif) - a; // 0～の形

  if (a <= (b - (1LL << dif))) {
    ans += (1LL << dif);
    cout << ans << endl;
  } else {
    b -= (1LL << dif);
    ll br1, br2;
    if (b != 0) {
      int br1_ = 60;
      while (bit(b, br1_) == 0)
        --br1_;
      br1 = (1LL << (br1_ + 1)) - 1;
    } else {
      br1 = 0LL;
    }
    br2 = a;
    if (br2 > br1) {
      ans += (1LL << (dif)) - (br2 - br1 - 1);
      cout << ans << endl;
    } else {
      ans += (1LL << (dif));
      cout << ans << endl;
    }
  }

  return 0;
}
Problem p03708 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using int64 = long long;

int main() {
  int64 A, B;
  cin >> A >> B;

  if (A == B) {
    cout << 1 << endl;
    return 0;
  }

  int p = -1, k = -1;
  for (int i = 60; i >= 0; i--) {
    if ((B >> i) & 1 && !((A >> i) & 1)) {
      p = i;
      break;
    } else {
      A &= ~(1LL << i);
    }
  }
  for (int i = p - 1; i >= 0; i--) {
    if ((B >> i) & 1) {
      k = i;
      break;
    }
  }

  int64 ret = (1LL << p) - A;

  pair<int64, int64> latte = {(1LL << p), (1LL << p)};
  if (k >= 0)
    latte.second += (1LL << (k + 1));
  else
    throw 0;

  //  cout << latte.first << " " << latte.second << endl;

  pair<int64, int64> malta = {(1LL << p) + A, (1LL << (p + 1))};

  //  cout << malta.first << " " << malta.second << endl;

  if (latte > malta)
    swap(latte, malta);
  if (latte.second <= malta.first) {
    ret += latte.second - latte.first;
    ret += malta.second - malta.first;
  } else {
    ret += max(latte.second, malta.second) - min(latte.first, malta.first);
  }

  cout << ret << endl;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using int64 = long long;

int main() {
  int64 A, B;
  cin >> A >> B;

  if (A == B) {
    cout << 1 << endl;
    return 0;
  }

  int p = -1, k = -1;
  for (int i = 60; i >= 0; i--) {
    if ((B >> i) & 1 && !((A >> i) & 1)) {
      p = i;
      break;
    } else {
      A &= ~(1LL << i);
    }
  }
  for (int i = p - 1; i >= 0; i--) {
    if ((B >> i) & 1) {
      k = i;
      break;
    }
  }

  int64 ret = (1LL << p) - A;

  pair<int64, int64> latte = {(1LL << p), (1LL << p)};
  if (k >= 0)
    latte.second += (1LL << (k + 1));
  else
    ++latte.second; // throw 0;

  //  cout << latte.first << " " << latte.second << endl;

  pair<int64, int64> malta = {(1LL << p) + A, (1LL << (p + 1))};

  //  cout << malta.first << " " << malta.second << endl;

  if (latte > malta)
    swap(latte, malta);
  if (latte.second <= malta.first) {
    ret += latte.second - latte.first;
    ret += malta.second - malta.first;
  } else {
    ret += max(latte.second, malta.second) - min(latte.first, malta.first);
  }

  cout << ret << endl;
}
Problem p03708 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
#include <iostream>

namespace WoKeNengYiJingShaDiaoLe {
typedef long long ll;
ll A, B, T;
void solve() {
  scanf("%lld%lld", &A, &B);
  for (T = 1ll << 62;; T >>= 1) {
    ll a = A & T, b = B & T;
    if (a ^ b)
      break;
    A ^= a, B ^= b;
  }

  ll L = A, R, dt;
  for (dt = T >> 1; dt && !(B & dt); dt >>= 1)
    ;
  if (!dt)
    R = T;
  else
    R = T + ((dt << 1) - 1);

  ll ans;
  if (R < T + A)
    ans = R - L + 1 + (T * 2 - 1 - (T + A) + 1);
  else
    ans = (T * 2 - 1 - L + 1);

  printf("%lld\n", ans);
}
} // namespace WoKeNengYiJingShaDiaoLe

int main() {
  WoKeNengYiJingShaDiaoLe::solve();
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>

namespace WoKeNengYiJingShaDiaoLe {
typedef long long ll;
ll A, B, T;
void solve() {
  scanf("%lld%lld", &A, &B);
  if (A == B) {
    printf("1\n");
    return;
  }

  for (T = 1ll << 62;; T >>= 1) {
    ll a = A & T, b = B & T;
    if (a ^ b)
      break;
    A ^= a, B ^= b;
  }

  ll L = A, R, dt;
  for (dt = T >> 1; dt && !(B & dt); dt >>= 1)
    ;
  if (!dt)
    R = T;
  else
    R = T + ((dt << 1) - 1);

  ll ans;
  if (R < T + A)
    ans = R - L + 1 + (T * 2 - 1 - (T + A) + 1);
  else
    ans = (T * 2 - 1 - L + 1);

  printf("%lld\n", ans);
}
} // namespace WoKeNengYiJingShaDiaoLe

int main() {
  WoKeNengYiJingShaDiaoLe::solve();
  return 0;
}
Problem p03708 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
typedef long long ll;
ll A, B, ans;
int main() {
  scanf("%lld%lld", &A, &B);
  int r = 60;
  for (; (A >> r & 1) == (B >> r & 1); r--)
    ;
  A &= (1LL << r + 1) - 1;
  B &= (1LL << r + 1) - 1;
  int k = r - 1;
  for (; !(B >> k & 1) && ~k; k--)
    ;
  ans += (1LL << r + 1) - A;
  if (A > (1LL << k + 1))
    ans -= A - (1LL << k + 1);
  printf("%lld\n", ans);
}

Fixed submission:
#include <cstdio>
typedef long long ll;
ll A, B, ans;
int main() {
  scanf("%lld%lld", &A, &B);
  if (A == B)
    return puts("1"), 0;
  int r = 60;
  for (; (A >> r & 1) == (B >> r & 1); r--)
    ;
  A &= (1LL << r + 1) - 1;
  B &= (1LL << r + 1) - 1;
  int k = r - 1;
  for (; !(B >> k & 1) && ~k; k--)
    ;
  ans += (1LL << r + 1) - A;
  if (A > (1LL << k + 1))
    ans -= A - (1LL << k + 1);
  printf("%lld\n", ans);
}
Problem p03708 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
const int LOG = 60;

ll l, r;

ll hi_bit(ll x) {
  for (int i = LOG; ~i; i--)
    if (x >> i & 1)
      return i;
}

ll f(ll l, ll r) { // []
  if (l == r)
    return 1;
  for (int i = LOG; ~i; i--)
    if ((l ^ r) >> i & 1)
      break;
    else if (l >> i & 1) {
      l ^= 1LL << i;
      r ^= 1LL << i;
    }

  ll a = hi_bit(l), b = hi_bit(r);
  ll c = (__builtin_popcountll(r) == 1 ? 0 : 1 + hi_bit(r ^ (1LL << b)));
  if ((r ^ (1LL << b)) + (1LL << c) >= l)
    return ((1LL << b) + ((1LL << b) - l));
  abort();
  return (((1LL << b) - l) + (1LL << c) +
          ((1LL << b) - max((1LL << c) + 1, l)));
}

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> l >> r;
  cout << f(l, r) << '\n';
}


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

typedef long long ll;
typedef pair<ll, ll> pll;
const int LOG = 60;

ll l, r;

ll hi_bit(ll x) {
  for (int i = LOG; ~i; i--)
    if (x >> i & 1)
      return i;
}

ll f(ll l, ll r) { // []
  if (l == r)
    return 1;
  for (int i = LOG; ~i; i--)
    if ((l ^ r) >> i & 1)
      break;
    else if (l >> i & 1) {
      l ^= 1LL << i;
      r ^= 1LL << i;
    }

  ll a = hi_bit(l), b = hi_bit(r);
  ll c = (__builtin_popcountll(r) == 1 ? 0 : 1 + hi_bit(r ^ (1LL << b)));
  // cerr << a << ' ' << b << ' ' << c << '\n';
  return (((1LL << b) - l) + (1LL << c) + ((1LL << b) - max((1LL << c), l)));
}

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> l >> r;
  cout << f(l, r) << '\n';
}
Problem p03708 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define mset(a, b) memset(a, b, sizeof a)
#define mcpy(a, b) memcpy(a, b, sizeof b)
#define lb(x) ((x) & (-(x)))
#define inf 0x3f3f3f3f
#define N 500010
using namespace std;
typedef long long ll;
ll l, r, ans;
int i, j, k;
int main() {
  //	l=rand()%57+1,r=rand()%57+1;
  //	while(l>=r-1)l=rand()%57+1,r=rand()%57+1;
  cin >> l >> r;
  //	cout<<l<<" "<<r<<" ";
  /*	if(r-l<=1)return cout<<r-l+1,0;
          for(i=l;i<=r;i++)f[i]=1;
          for(i=0;i<=(r<<1);i++)if(f[i])for(j=l;j<=r;j++)f[i|j]=1;
          for(i=0;i<=1000000;i++)if(f[i])cnt++,cout<<i<<" ";puts("");
          cout<<cnt<<" ";*/
  //	ans=r-l+1;
  for (i = 61; ~i; i--)
    if (((r >> i) & 1) ^ ((l >> i) & 1))
      break;
    else
      l &= ~(1ll << i), r &= ~(1ll << i);
  ans += (1ll << i) - l;
  // l~2^i
  for (j = i - 1; ~j; j--)
    if (r & (1ll << j))
      break;
  //	cout<<"   "<<i<<" "<<j<<"    ";
  //	ans+=(1<<j+1)+(1<<j+1)*((1<<i-j-1)-l-1)+
  // 2^i~r : 100000000~100011111
  // l~r :   110101010~111111111
  ans += 1ll << i;
  ans -= max(0ll, (1ll << i) + l - ((1ll << i) + (1ll << j + 1) - 1) - 1);
  cout << ans;
  /*	if(cnt^ans){
                  cout<<cnt<<" "<<ans<<endl;
                  for(int i=200000000;i;i--);
          }
          else puts("");*/
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define mset(a, b) memset(a, b, sizeof a)
#define mcpy(a, b) memcpy(a, b, sizeof b)
#define lb(x) ((x) & (-(x)))
#define inf 0x3f3f3f3f
#define N 500010
using namespace std;
typedef long long ll;
ll l, r, ans;
int i, j, k;
int main() {
  //	l=rand()%57+1,r=rand()%57+1;
  //	while(l>=r-1)l=rand()%57+1,r=rand()%57+1;
  cin >> l >> r;
  //	cout<<l<<" "<<r<<" ";
  if (r - l <= 0)
    return cout << r - l + 1, 0;
  /*	for(i=l;i<=r;i++)f[i]=1;
          for(i=0;i<=(r<<1);i++)if(f[i])for(j=l;j<=r;j++)f[i|j]=1;
          for(i=0;i<=1000000;i++)if(f[i])cnt++,cout<<i<<" ";puts("");
          cout<<cnt<<" ";*/
  //	ans=r-l+1;
  for (i = 61; ~i; i--)
    if (((r >> i) & 1) ^ ((l >> i) & 1))
      break;
    else
      l &= ~(1ll << i), r &= ~(1ll << i);
  ans += (1ll << i) - l;
  // l~2^i
  for (j = i - 1; ~j; j--)
    if (r & (1ll << j))
      break;
  //	cout<<"   "<<i<<" "<<j<<"    ";
  //	ans+=(1<<j+1)+(1<<j+1)*((1<<i-j-1)-l-1)+
  // 2^i~r : 100000000~100011111
  // l~r :   110101010~111111111
  ans += 1ll << i;
  ans -= max(0ll, (1ll << i) + l - ((1ll << i) + (1ll << j + 1) - 1) - 1);
  cout << ans;
  /*	if(cnt^ans){
                  cout<<cnt<<" "<<ans<<endl;
                  for(int i=200000000;i;i--);
          }
          else puts("");*/
  return 0;
}
Problem p03709 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define dep(i, a, b) for (int i = a; i >= b; i--)
#define Rep(i, a) for (int i = 0; i < a; i++)
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ab(x) ((x) < 0 ? -(x) : (x))
using namespace std;
typedef long long LL;
typedef map<int, int>::iterator mit;
typedef set<int>::iterator sit;
typedef pair<LL, LL> pii;
#define x first
#define y second
const int N = 1e5 + 10, mod = 1e9 + 7;
map<LL, int> val;
int n;
pii a[N], range[N];
LL b[N];
int s[N];

void add(int x, int d) {
  for (; x < N; x += (-x) & x)
    (s[x] += d) %= mod;
}
int sum(int x) {
  int ans = 0;
  for (; x; x -= (-x) & x)
    (ans += s[x]) %= mod;
  return ans;
}

int main() {
  scanf("%d", &n);
  rep(i, 1, n) scanf("%lld%lld", &a[i].x, &a[i].y), b[i] = a[i].y;
  sort(a + 1, a + n + 1), sort(b + 1, b + n + 1);
  rep(i, 1, n) val[b[i]] = i;
  rep(i, 1, n) a[i].y = val[a[i].y];
  LL mx = 0;
  rep(i, 1, n) mx = max(mx, a[i].y), range[i].y = mx;
  LL mn = n;
  dep(i, n, 1) mn = min(mn, a[i].y), range[i].x = mn;
  sort(range + 1, range + n + 1);
  add(1, 1);
  int ans = 0;
  rep(i, 1, n) {
    int F = (sum(N - 1) - sum(range[i].x - 1)) % mod;
    add(range[i].y + 1, F);
    if (range[i].y == n)
      (ans += F) %= mod;
  }
  if (ans < 0)
    ans += mod;
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define dep(i, a, b) for (int i = a; i >= b; i--)
#define Rep(i, a) for (int i = 0; i < a; i++)
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ab(x) ((x) < 0 ? -(x) : (x))
using namespace std;
typedef long long LL;
typedef map<int, int>::iterator mit;
typedef set<int>::iterator sit;
typedef pair<LL, LL> pii;
#define x first
#define y second
const int N = 2e5 + 10, mod = 1e9 + 7;
map<LL, int> val;
int n;
pii a[N], range[N];
LL b[N];
int s[N];

void add(int x, int d) {
  for (; x < N; x += (-x) & x)
    (s[x] += d) %= mod;
}
int sum(int x) {
  int ans = 0;
  for (; x; x -= (-x) & x)
    (ans += s[x]) %= mod;
  return ans;
}

int main() {
  scanf("%d", &n);
  rep(i, 1, n) scanf("%lld%lld", &a[i].x, &a[i].y), b[i] = a[i].y;
  sort(a + 1, a + n + 1), sort(b + 1, b + n + 1);
  rep(i, 1, n) val[b[i]] = i;
  rep(i, 1, n) a[i].y = val[a[i].y];
  LL mx = 0;
  rep(i, 1, n) mx = max(mx, a[i].y), range[i].y = mx;
  LL mn = n;
  dep(i, n, 1) mn = min(mn, a[i].y), range[i].x = mn;
  sort(range + 1, range + n + 1);
  add(1, 1);
  int ans = 0;
  rep(i, 1, n) {
    int F = (sum(N - 1) - sum(range[i].x - 1)) % mod;
    add(range[i].y + 1, F);
    if (range[i].y == n)
      (ans += F) %= mod;
  }
  if (ans < 0)
    ans += mod;
  printf("%d\n", ans);
  return 0;
}
Problem p03709 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int n;
struct TTL {
  int x, v;
} p[200010];
const int mod = 1000000007;
int L[200010], R[200010];
int f[200010], q[200010];
int lsh[400010], cnt;
bool cmp(TTL a, TTL b) { return a.x < b.x; }
int ycl() // 左边最大 右边最小
{
  int maxn = 0, minn = 2e9;
  for (int i = 1; i <= n; i++) {
    maxn = max(maxn, p[i].v);
    R[i] = maxn;
    lsh[++cnt] = maxn;
  }
  for (int i = n; i >= 1; i--) {
    minn = min(minn, p[i].v);
    L[i] = minn;
    lsh[++cnt] = minn;
  }
  ///	for (int i=1;i<=n;i++)
  //		printf("%d %d\n",L[i],R[i]);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &p[i].x, &p[i].v), lsh[++cnt] = p[i].v;
  sort(p + 1, p + 1 + n, cmp);
  ycl();
  sort(lsh + 1, lsh + 1 + cnt);
  cnt = unique(lsh + 1, lsh + 1 + cnt) - 1 - lsh;
  for (int i = 1; i <= n; i++) {
    L[i] = lower_bound(lsh + 1, lsh + 1 + cnt, L[i]) - lsh;
    R[i] = lower_bound(lsh + 1, lsh + 1 + cnt, R[i]) - lsh;
    //	printf("%d %d\n",L[i],R[i]);
  }
  int head = 0, tail = 1;
  int sum = 1;
  L[0] = 0;
  R[0] = 0;
  f[0] = 1;
  n++;
  L[n] = n;
  R[n] = n;
  for (int i = 1; i <= n; i++) {
    while (head < tail && L[i] > R[q[head + 1]] + 1)
      sum = (sum - f[q[head + 1]] + mod) % mod, head++;
    f[i] = sum;
    sum = (sum + f[i]) % mod;
    q[++tail] = i;
    //	cout<<sum<<endl;
  }
  printf("%d", f[n]);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int n;
struct TTL {
  int x, v;
} p[200010];
const int mod = 1000000007;
int L[200010], R[200010];
int f[200010], q[200010];
int lsh[600010], cnt;
bool cmp(TTL a, TTL b) { return a.x < b.x; }
int ycl() // 左边最大 右边最小
{
  int maxn = 0, minn = 2e9;
  for (int i = 1; i <= n; i++) {
    maxn = max(maxn, p[i].v);
    R[i] = maxn;
    lsh[++cnt] = maxn;
  }
  for (int i = n; i >= 1; i--) {
    minn = min(minn, p[i].v);
    L[i] = minn;
    lsh[++cnt] = minn;
  }
  ///	for (int i=1;i<=n;i++)
  //		printf("%d %d\n",L[i],R[i]);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &p[i].x, &p[i].v), lsh[++cnt] = p[i].v;
  sort(p + 1, p + 1 + n, cmp);
  ycl();
  sort(lsh + 1, lsh + 1 + cnt);
  cnt = unique(lsh + 1, lsh + 1 + cnt) - 1 - lsh;
  for (int i = 1; i <= n; i++) {
    L[i] = lower_bound(lsh + 1, lsh + 1 + cnt, L[i]) - lsh;
    R[i] = lower_bound(lsh + 1, lsh + 1 + cnt, R[i]) - lsh;
    //	printf("%d %d\n",L[i],R[i]);
  }
  int head = 0, tail = 1;
  int sum = 1;
  L[0] = 0;
  R[0] = 0;
  f[0] = 1;
  n++;
  L[n] = n;
  R[n] = n;
  for (int i = 1; i <= n; i++) {
    while (head < tail && L[i] > R[q[head + 1]] + 1)
      sum = (sum - f[q[head + 1]] + mod) % mod, head++;
    f[i] = sum;
    sum = (sum + f[i]) % mod;
    q[++tail] = i;
    //	cout<<sum<<endl;
  }
  printf("%d", f[n]);
}
Problem p03709 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define ref(i, x, y) for (int i = x; i <= y; ++i)
#define def(i, x, y) for (int i = x; i >= y; --i)
const int N = 100001;
const int mod = 1e9 + 7;
int n, f[N], F[N + 1];
void inc(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}
void upd(int x, int s) {
  for (x++; x >= 1; x -= x & -x)
    inc(F[x], s);
}
int ask(int x) {
  int s = 0;
  for (x++; x <= n + 1; x += x & -x)
    inc(s, F[x]);
  return s;
}
struct xint {
  int x, y;
} s[N], p[N];
bool cmpx(xint a, xint b) { return a.x < b.x; }
bool cmpy(xint a, xint b) { return a.y < b.y; }
int main() {
  scanf("%d", &n);
  ref(i, 1, n) scanf("%d%d", &s[i].x, &s[i].y);
  sort(s + 1, s + n + 1, cmpy);
  ref(i, 1, n) s[i].y = i;
  sort(s + 1, s + n + 1, cmpx);
  p[1].y = s[1].y;
  ref(i, 2, n) p[i].y = max(p[i - 1].y, s[i].y);
  p[n].x = s[n].y;
  def(i, n - 1, 1) p[i].x = min(p[i + 1].x, s[i].y);
  f[0] = 1;
  upd(0, 1);
  int ans = 0;
  ref(i, 1, n) {
    f[i] = ask(p[i].x - 1);
    upd(p[i].y, f[i]);
    if (p[i].y == n)
      inc(ans, f[i]);
  }
  printf("%d\n", ans);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define ref(i, x, y) for (int i = x; i <= y; ++i)
#define def(i, x, y) for (int i = x; i >= y; --i)
const int N = 200001;
const int mod = 1e9 + 7;
int n, f[N], F[N + 1];
void inc(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}
void upd(int x, int s) {
  for (x++; x >= 1; x -= x & -x)
    inc(F[x], s);
}
int ask(int x) {
  int s = 0;
  for (x++; x <= n + 1; x += x & -x)
    inc(s, F[x]);
  return s;
}
struct xint {
  int x, y;
} s[N], p[N];
bool cmpx(xint a, xint b) { return a.x < b.x; }
bool cmpy(xint a, xint b) { return a.y < b.y; }
int main() {
  scanf("%d", &n);
  ref(i, 1, n) scanf("%d%d", &s[i].x, &s[i].y);
  sort(s + 1, s + n + 1, cmpy);
  ref(i, 1, n) s[i].y = i;
  sort(s + 1, s + n + 1, cmpx);
  p[1].y = s[1].y;
  ref(i, 2, n) p[i].y = max(p[i - 1].y, s[i].y);
  p[n].x = s[n].y;
  def(i, n - 1, 1) p[i].x = min(p[i + 1].x, s[i].y);
  f[0] = 1;
  upd(0, 1);
  int ans = 0;
  ref(i, 1, n) {
    f[i] = ask(p[i].x - 1);
    upd(p[i].y, f[i]);
    if (p[i].y == n)
      inc(ans, f[i]);
  }
  printf("%d\n", ans);
}