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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const ll INF = LLONG_MAX;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int n, m;
ll l;
ll dp[2222][2222];
vi k, s, sum;

ll solve(int left, int right) {
  if (dp[left][right] < INF)
    return dp[left][right];
  int t = max(left, right);
  ll res;
  if (t == n - 2) {
    res = (sum[k[n - 1]] - sum[k[left] - 1]) / l +
          (sum[k[n - 1]] - sum[k[right] - 1]) / l;
  } else {
    res = min(solve(t + 1, right) + (sum[k[t + 1]] - sum[k[left] - 1]) / l,
              solve(left, t + 1) + (sum[k[t + 1]] - sum[k[right] - 1]) / l);
  }
  return dp[left][right] = res;
}

int main() {
  REP(i, 2222) REP(j, 2222) dp[i][j] = INF;
  cin >> n >> m >> l;
  k.resize(n), s.resize(m), sum.resize(m + 1);
  REP(i, n) cin >> k[i];
  REP(i, m) cin >> s[i];
  sort(ALL(k));
  REP(i, m) sum[i + 1] = sum[i] + s[i];
  cout << solve(0, 0) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int n, m;
ll l;
ll dp[2222][2222];
vi k, s, sum;

ll solve(int left, int right) {
  if (dp[left][right] >= 0)
    return dp[left][right];
  int t = max(left, right);
  int res;
  if (t == n - 2) {
    res = (sum[k[n - 1]] - sum[k[left] - 1]) / l +
          (sum[k[n - 1]] - sum[k[right] - 1]) / l;
  } else {
    res = min(solve(t + 1, right) + (sum[k[t + 1]] - sum[k[left] - 1]) / l,
              solve(left, t + 1) + (sum[k[t + 1]] - sum[k[right] - 1]) / l);
  }
  return dp[left][right] = res;
}

int main() {
  MS(dp, -1);
  cin >> n >> m >> l;
  k.resize(n), s.resize(m), sum.resize(m + 1);
  REP(i, n) cin >> k[i];
  REP(i, m) cin >> s[i];
  sort(ALL(k));
  REP(i, m) sum[i + 1] = sum[i] + s[i];
  cout << solve(0, 0) << endl;
  return 0;
}
Problem p01430 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

using namespace std;

int n, m, q, a, b, come[1050], used[1050][1050], res, ok[1050][1050];

int dfs(int s, int g, int cp) {
  if (s == g)
    return 1;
  come[s] = 1;
  for (int i = 0; i <= n; i++) {
    if (come[i])
      continue;
    if (used[s][i] <= cp)
      continue;
    // if(ok[s][i] < cp)continue;
    if (dfs(i, g, cp)) {
      used[i][s]++;
      used[s][i]--;
      ok[s][i] = 0;
      return 1;
    }
  }
  return 0;
}

int flow(int s, int g, int cp = 0) {
  fill(come, come + 1000, 0);
  return dfs(s, g, cp);
}

int main() {
  cin >> n >> m >> q;
  while (m--) {
    cin >> a >> b;
    used[a][b] = used[b][a] = 1;
  }
  for (int i = 0; i < q; i++) {
    cin >> m >> a >> b;
    if (m == 1) {
      used[a][b] = used[b][a] = 1;
    } else {
      if (used[a][b] == 2) {
        if (!flow(b, a)) {
          flow(n, 1);
          flow(b, a);
          res--;
        }
      } else if (used[b][a] == 2) {
        if (!flow(a, b)) {
          flow(n, 1);
          flow(a, b);
          res--;
        }
      }
      used[a][b] = used[b][a] = 0;
    }
    while (flow(1, n))
      res++;
    cout << res << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int n, m, q, a, b, come[1050], used[1050][1050], res, ok[1050][1050];

int dfs(int s, int g, int cp) {
  if (s == g)
    return 1;
  come[s] = 1;
  for (int i = 0; i <= n; i++) {
    if (come[i])
      continue;
    if (used[s][i] <= cp)
      continue;
    // if(ok[s][i] < cp)continue;
    if (dfs(i, g, cp)) {
      used[i][s]++;
      used[s][i]--;
      ok[s][i] = 0;
      return 1;
    }
  }
  return 0;
}

int flow(int s, int g, int cp = 0) {
  fill(come, come + 1000, 0);
  return dfs(s, g, cp);
}

void color() {
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++)
      ok[i][j] = (used[i][j] != used[j][i]);
}

int main() {
  cin >> n >> m >> q;
  while (m--) {
    cin >> a >> b;
    used[a][b] = used[b][a] = 1;
  }
  for (int i = 0; i < q; i++) {
    cin >> m >> a >> b;
    if (m == 1) {
      used[a][b] = used[b][a] = 1;
    } else {
      if (used[a][b] == 2) {
        color();
        res--;
        if (!flow(n, a, 1))
          return 1;
        if (!flow(b, 1, 1))
          return 0;
      } else if (used[b][a] == 2) {
        color();
        res--;
        if (!flow(n, b, 1))
          return 1;
        if (!flow(a, 1, 1))
          return 0;
      }
      used[a][b] = used[b][a] = 0;
    }
    while (flow(1, n))
      res++;
    cout << res << endl;
  }
  return 0;
}
Problem p01434 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
#define pb push_back
#define clr clear()
#define sz size()
#define FOR(i, a) for (int i = 0; i < (int)(a); i++)
using namespace std;
typedef vector<int> vi;

class data {
public:
  int a, s, t;
  bool cut;

  data(int x, int y, int z, bool w) {
    a = x;
    s = y;
    t = z;
    cut = w;
  }
  bool operator<(const data &t) const { return a < t.a; }
};

int main() {
  int n, m, s[100000], t[100000], p[100000];
  int use[100000];
  vector<data> d;

  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s[i] >> t[i];
    d.push_back(data(s[i], i, -1, false));
    d.push_back(data(t[i], -1, i, false));
  }
  for (int i = 0; i < m; i++) {
    cin >> p[i];
    d.push_back(data(p[i], -1, -1, true));
  }

  sort(d.begin(), d.end());

  for (int i = 0; i < n; i++)
    use[i] = -1;

  long long int ans = 0;
  int c[2], cnt = 0;
  c[0] = 0;
  c[1] = 0;

  for (int i = 0; i < (int)d.size(); i++) {
    if (i)
      ans += (long long int)(d[i].a - d[i - 1].a) * c[cnt % 2];
    if (d[i].cut)
      cnt = 1 - cnt;
    if (d[i].s >= 0) {
      if (s[d[i].s] < t[d[i].s]) {
        use[d[i].s] = cnt % 2;
        c[cnt % 2]++;
      }
    }
    if (d[i].t >= 0) {
      if (s[d[i].t] < t[d[i].t])
        c[use[d[i].t]]--;
    }
  }

  for (int i = (int)d.size() - 1; i >= 0; i--) {
    if (i != (int)d.size() - 1)
      ans += (long long int)(d[i + 1].a - d[i].a) * c[cnt % 2];
    if (d[i].cut)
      cnt = 1 - cnt;
    if (d[i].s >= 0) {
      if (s[d[i].s] > t[d[i].s]) {
        use[d[i].s] = cnt % 2;
        c[cnt % 2]++;
      }
    }
    if (d[i].t >= 0) {
      if (s[d[i].t] > t[d[i].t])
        c[use[d[i].t]]--;
    }
  }
  cout << ans << endl;
}

Error: -11

Fixed submission:

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

typedef pair<int, int> P;

int g[501][501];
int d[501];

int main() {
  int n, e, q;
  int f, t, m, a, b;

  cin >> n >> e >> q;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++)
      g[i][j] = 0;
  }

  for (int i = 0; i < e; i++) {
    cin >> f >> t;
    g[f][t] = 1;
    g[t][f] = 1;
  }

  for (int k = 0; k < q; k++) {
    cin >> m >> a >> b;
    g[a][b] = 1 - g[a][b];
    g[b][a] = 1 - g[b][a];

    for (int i = 1; i <= n; i++)
      d[i] = 0;

    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++)
        cout << g[i][j];
      cout << endl;
    }

    priority_queue<P> q;
    q.push(P(0, 1));

    while (q.size()) {
      P p = q.top();
      q.pop();

      int v = p.second;
      for (int i = 1; i <= n; i++) {
        if (g[v][i] && !d[i]) {
          d[i] = d[v] + g[v][i];
          q.push(P(d[i], i));
        }
      }
    }

    cout << d[n] << endl;
  }
}
Problem p01434 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

typedef long long ll;

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<int> s(n), t(n), p(m + 2);
  for (int i = 0; i < n; i++)
    scanf("%d%d", &s[i], &t[i]);
  for (int i = 1; i <= m; i++)
    scanf("%d", &p[i]);
  p[m + 1] = 1e9 + 1;
  sort(begin(p), end(p));

  vector<int> sum(m + 2);
  for (int i = 1; i <= m + 1; i++) {
    sum[i] = p[i] - p[i - 1];
    if (i >= 2)
      sum[i] += sum[i - 2];
  }

  ll res = 0;
  for (int i = 0; i < n; i++) {
    ll temp = 0;
    if (s[i] < t[i]) {
      int l = lower_bound(begin(p), end(p), s[i]) - p.begin();
      int r = lower_bound(begin(p), end(p), t[i]) - p.begin();
      temp = p[l] - s[i];
      if ((r ^ l) % 2 == 0)
        temp += sum[r] - sum[l] - (p[r] - t[i]);
      else
        temp += sum[r - 1] - sum[l];
    } else {
      int l = lower_bound(begin(p), end(p), t[i]) - p.begin();
      int r = lower_bound(begin(p), end(p), s[i]) - p.begin();
      temp = -(p[r] - s[i]);
      if ((r ^ l) % 2 == 0)
        temp += sum[r] - sum[l] + (p[l] - t[i]);
      else
        temp += sum[r] - sum[l - 1];
    }
    res += temp;
  }
  printf("%lld\\n", res);
}

Error: TLE

Fixed submission:

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

using namespace std;

typedef long long ll;

struct FenwickTree {
  vector<ll> data;
  FenwickTree(int n) : data(n + 1) {}
  void AddRange(int i, int x) {
    for (; i; i -= i & -i)
      data[i] += x;
  }
  void AddRange(int i, int j, int x) {
    AddRange(i, -x);
    AddRange(j, x);
  }
  int Get(int i) {
    ll res = 0;
    for (i++; i < data.size(); i += i & -i)
      res += data[i];
    return res;
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> s(n), t(n), p(m);
  for (int i = 0; i < n; i++)
    cin >> s[i] >> t[i];
  for (int i = 0; i < m; i++)
    cin >> p[i];
  sort(begin(p), end(p));

  vector<int> f;
  {
    for (int i = 0; i < n; i++)
      f.push_back(s[i]);
    for (int i = 0; i < n; i++)
      f.push_back(t[i]);
    for (int i = 0; i < m; i++)
      f.push_back(p[i]);
    sort(begin(f), end(f));
    f.erase(unique(begin(f), end(f)), end(f));
    for (int i = 0; i < n; i++)
      s[i] = lower_bound(begin(f), end(f), s[i]) - f.begin();
    for (int i = 0; i < n; i++)
      t[i] = lower_bound(begin(f), end(f), t[i]) - f.begin();
    for (int i = 0; i < m; i++)
      p[i] = lower_bound(begin(f), end(f), p[i]) - f.begin();
  }
  int len = f.size();

  FenwickTree ft(len);
  for (int i = 0; i < n; i++)
    if (s[i] < t[i]) {
      ft.AddRange(s[i], t[i], 1);
      int j = lower_bound(begin(p), end(p), s[i]) - begin(p), x = -1;
      for (; j < m && p[j] < t[i]; j++, x *= -1)
        ft.AddRange(p[j], t[i], x);
    } else {
      ft.AddRange(t[i], s[i], 1);
      int j = lower_bound(begin(p), end(p), s[i]) - begin(p) - 1, x = -1;
      for (; j >= 0 && p[j] > t[i]; j--, x *= -1)
        ft.AddRange(t[i], p[j], x);
    }

  ll res = 0;
  for (int i = 0; i < len - 1; i++)
    res += ft.Get(i) * ll(f[i + 1] - f[i]);
  cout << res << endl;
}
Problem p01434 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

struct sec {
  int s, t;
};

struct event {
  int type; // 0:cut +1:begin(on) +2:begin(off) +3:end(on) +4:end(off)
  int x;
  event(int type, int x) { this->type = type, this->x = x; }
};

int main() {
  int N, M;
  cin >> N >> M;
  sec S[N];
  int C[M];
  for (auto &s : S)
    scanf("%d %d", &s.s, &s.t);
  for (auto &c : C)
    scanf("%d", &c);

  vector<event> E;
  for (auto &c : C)
    E.emplace_back(event(0, c));
  sort(C, C + M);
  long long sum = 0;

  for (auto &s : S) {
    auto lo = lower_bound(C, C + M, min(s.s, s.t));
    auto up = upper_bound(C, C + M, max(s.s, s.t));
    if (lo == up) {
      sum += abs(s.s - s.t);
      continue;
    }
    if (s.s < s.t) {
      sum += *lo - s.s;
      E.emplace_back(event(2, *lo));
      if ((up - lo) % 2 == 0) {
        sum += s.t - *(up - 1);
        E.emplace_back(event(4, *(up - 1)));
      } else {
        E.emplace_back(event(3, *(up - 1)));
      }
    } else {
      sum += s.s - *(up - 1);
      E.emplace_back(event(4, *(up - 1)));
      if ((up - lo) % 2) {
        E.emplace_back(event(1, *lo));
      } else {
        E.emplace_back(event(2, *lo));
        sum += *lo - s.t;
      }
    }
  }
  sort(E.begin(), E.end(), [](const event &a, const event &b) {
    return a.x == b.x ? a.type < b.type : a.x < b.x;
  });

  int on_n = 0, off_n = 0;
  for (auto e = E.begin(); e != E.end(); e++) {
    // cout << e->x << " " << e->type << endl;
    switch (e->type) {
    case 0:
      if (on_n)
        sum += (long long int)on_n * (e->x - (e - 1)->x);
      swap(on_n, off_n);
      break;
    case 1:
      on_n++;
      break;
    case 2:
      off_n++;
      break;
    case 3:
      off_n--;
      break;
    case 4:
      on_n--;
      break;
    }
  }
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

struct sec {
  int s, t;
};

int main() {
  int N, M;
  cin >> N >> M;
  sec S[N];
  vector<int> C(M);
  for (auto &s : S)
    scanf("%d %d", &s.s, &s.t);
  for (auto &c : C)
    scanf("%d", &c);
  sort(C.begin(), C.end());

  long long sum = 0;

  for (auto &s : S) {
    auto lo = lower_bound(C.begin(), C.end(), min(s.s, s.t));
    auto up = upper_bound(C.begin(), C.end(), max(s.s, s.t));
    if (s.s < s.t) {
      bool on = true;
      for (auto i = lo; i != up; i++) {
        if (on) {
          on = false;
          sum += *i - s.s;
          s.s = *i;
        } else {
          on = true;
          s.s = *i;
        }
      }
      if (on)
        sum += s.t - s.s;
    } else {
      bool on = true;
      for (auto i = up - 1; i >= lo; i--) {
        if (on) {
          on = false;
          sum += s.s - *i;
          s.s = *i;
        } else {
          on = true;
          s.s = *i;
        }
      }
      if (on)
        sum += s.s - s.t;
    }
  }

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

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

int n, m;
int s[100010], t[100010];
int p[100010];
int a[100010];

int main() {
  cin >> n >> m, m++;
  for (int i = 0; i < n; i++) {
    cin >> s[i] >> t[i];
  }
  for (int i = 1; i < m; i++) {
    cin >> p[i];
  }
  sort(p, p + m);

  long long res = 0;
  for (int i = 0; i < n; i++) {
    if (lower_bound(p, p + m, s[i]) == lower_bound(p, p + m, t[i])) {
      res += abs(s[i] - t[i]);
    } else if (s[i] < t[i]) {
      int l = lower_bound(p, p + m, s[i]) - p - 1;
      int r = lower_bound(p, p + m, t[i]) - p;
      if ((l ^ r) & 1) {
        a[l]++;
        a[r + 1]--;
        res -= s[i] - p[l];
        res -= p[r] - t[i];
      } else {
        a[l]++;
        a[r]--;
        res -= s[i] - p[l];
      }
    } else {
      int l = lower_bound(p, p + m, t[i]) - p - 1;
      int r = lower_bound(p, p + m, s[i]) - p;
      if ((l ^ r) & 1) {
        a[l]++;
        a[r + 1]--;
        res -= t[i] - p[l];
        res -= p[r] - s[i];
      } else {
        a[l + 1]++;
        a[r + 1]--;
        res -= p[r] - s[i];
      }
    }
  }

  for (int i = 0; i < m; i++) {
    a[i + 2] += a[i];
    res += (long long)(p[i + 1] - p[i]) * a[i];
  }

  cout << res << endl;
}

Error: 0

Fixed submission:

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

int n, m;
int s[10010], t[10010];
int p[10010];
int a[10010];

int main() {
  cin >> n >> m, m++;
  for (int i = 0; i < n; i++) {
    cin >> s[i] >> t[i];
  }
  for (int i = 1; i < m; i++) {
    cin >> p[i];
  }
  sort(p, p + m);

  long long res = 0;
  for (int i = 0; i < n; i++) {
    if (lower_bound(p, p + m, s[i]) == lower_bound(p, p + m, t[i])) {
      res += abs(s[i] - t[i]);
    } else if (s[i] < t[i]) {
      int l = lower_bound(p, p + m, s[i]) - p - 1;
      int r = lower_bound(p, p + m, t[i]) - p;
      if ((l ^ r) & 1) {
        a[l]++;
        a[r + 1]--;
        res -= s[i] - p[l];
        res -= p[r] - t[i];
      } else {
        a[l]++;
        a[r]--;
        res -= s[i] - p[l];
      }
    } else {
      int l = lower_bound(p, p + m, t[i]) - p - 1;
      int r = lower_bound(p, p + m, s[i]) - p;
      if ((l ^ r) & 1) {
        a[l]++;
        a[r + 1]--;
        res -= t[i] - p[l];
        res -= p[r] - s[i];
      } else {
        a[l + 1]++;
        a[r + 1]--;
        res -= p[r] - s[i];
      }
    }
  }

  for (int i = 0; i < m; i++) {
    a[i + 2] += a[i];
    res += (long long)(p[i + 1] - p[i]) * a[i];
  }

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

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mp make_pair
typedef long long Int;

int of[200000];

int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  vector<pair<int, int>> v[2];
  rep(i, N) {
    int s, t;
    scanf("%d%d", &s, &t);
    v[s < t].push_back(mp(s, i + 1));
    v[s < t].push_back(mp(t, -(i + 1)));
  }
  rep(_, M) {
    int p;
    scanf("%d", &p);
    rep(k, 2) v[k].push_back(mp(p, 0));
  }
  rep(k, 2) sort(v[k].begin(), v[k].end());
  reverse(v[0].begin(), v[0].end());
  Int ans = 0;
  rep(k, 2) {
    int nc = 0, cc = 0, pc = 0;
    rep(i, v[k].size()) {
      if (i > 0)
        ans += (Int)nc * abs(v[k][i].first - v[k][i - 1].first);
      if (v[k][i].second == 0) {
        pc++;
        swap(nc, cc);
      } else if (v[k][i].second > 0) {
        const int ix = v[k][i].second - 1;
        nc++;
        of[ix] = pc;
      } else {
        const int ix = -v[k][i].second - 1;
        if ((pc - of[ix]) % 2 == 0)
          nc--;
        else
          cc--;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mp make_pair
typedef long long Int;

int of[20000];

int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  vector<pair<int, int>> v[2];
  rep(i, N) {
    int s, t;
    scanf("%d%d", &s, &t);
    const int x = s < t ? 1 : 0;
    v[x].push_back(mp(s, i + 1));
    v[x].push_back(mp(t, -(i + 1)));
  }
  rep(_, M) {
    int p;
    scanf("%d", &p);
    rep(k, 2) v[k].push_back(mp(p, 0));
  }
  rep(k, 2) sort(v[k].begin(), v[k].end());
  reverse(v[0].begin(), v[0].end());
  Int ans = 0;
  rep(k, 2) {
    int nc = 0, cc = 0, pc = 0;
    rep(i, v[k].size()) {
      if (i > 0)
        ans += (Int)nc * abs(v[k][i].first - v[k][i - 1].first);
      if (v[k][i].second == 0) {
        pc++;
        swap(nc, cc);
      } else if (v[k][i].second > 0) {
        const int ix = v[k][i].second - 1;
        nc++;
        of[ix] = pc;
      } else {
        const int ix = -v[k][i].second - 1;
        if ((pc - of[ix]) % 2 == 0)
          nc--;
        else
          cc--;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p01434 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define N 100005
using namespace std;
long long n, m, s[N], t[N], p[N], d[N], ans;

int bynary_search(int x) {
  int l = 0, r = m;
  while (l < r) {
    int mid = (l + r) / 2;
    if (x <= p[mid])
      r = mid;
    else
      l = mid + 1;
  }
  return l;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> s[i] >> t[i];
  for (int i = 0; i < m; i++)
    cin >> p[i];
  sort(p, p + m);
  d[0] = p[0], d[1] = p[1] - p[0];
  ans = 0;
  for (int i = 2; i < m; i++)
    d[i] = d[i - 2] + p[i] - p[i - 1];
  for (int i = 0; i < n; i++) {
    int r1 = bynary_search(s[i]);
    int r2 = bynary_search(t[i]);
    if (r1 == r2)
      ans += max(s[i], t[i]) - min(s[i], t[i]);
    else if (abs(r1 - r2) == 1) {
      if (s[i] < t[i])
        ans += p[r1] - s[i];
      else
        ans += s[i] - p[r1 - 1];
    } else if (s[i] < t[i]) {
      if (r2 % 2 == r1 % 2)
        ans += p[r1] - s[i] + d[r2 - 2] - d[r1] + t[i] - p[r2 - 1];
      else
        ans += p[r1] - s[i] + d[r2 - 1] - d[r1];
    } else {
      if (r2 % 2 == r1 % 2)
        ans += s[i] - p[r1 - 1] + d[r1 - 2] - d[r2] + p[r2] - t[i];
      else
        ans += s[i] - p[r1 - 1] + d[r1 - 2] - d[r2 - 1];
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define N 10005
using namespace std;
long long n, m, s[N], t[N], p[N], d[N], ans;

int bynary_search(int x) {
  int l = 0, r = m;
  while (l < r) {
    int mid = (l + r) / 2;
    if (x <= p[mid])
      r = mid;
    else
      l = mid + 1;
  }
  return l;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> s[i] >> t[i];
  for (int i = 0; i < m; i++)
    cin >> p[i];
  sort(p, p + m);
  d[0] = p[0], d[1] = p[1] - p[0];
  ans = 0;
  for (int i = 2; i < m; i++)
    d[i] = d[i - 2] + p[i] - p[i - 1];
  for (int i = 0; i < n; i++) {
    int r1 = bynary_search(s[i]);
    int r2 = bynary_search(t[i]);
    if (r1 == r2)
      ans += max(s[i], t[i]) - min(s[i], t[i]);
    else if (abs(r1 - r2) == 1) {
      if (s[i] < t[i])
        ans += p[r1] - s[i];
      else
        ans += s[i] - p[r1 - 1];
    } else if (s[i] < t[i]) {
      if (r2 % 2 == r1 % 2)
        ans += p[r1] - s[i] + d[r2 - 2] - d[r1] + t[i] - p[r2 - 1];
      else
        ans += p[r1] - s[i] + d[r2 - 1] - d[r1];
    } else {
      if (r2 % 2 == r1 % 2)
        ans += s[i] - p[r1 - 1] + d[r1 - 2] - d[r2] + p[r2] - t[i];
      else
        ans += s[i] - p[r1 - 1] + d[r1 - 2] - d[r2 - 1];
    }
  }
  cout << ans << endl;
  return 0;
}