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

#include <cstring>
#include <iostream>
#include <string>
using namespace std;

char field[70][70];
string numberTable[70][70];
int dy[] = {-1, 0}, dx[] = {0, -1};

void clearLeftZero(string &str) {
  while (str.empty() == false && str[0] == '0') {
    str.erase(0, 1);
  }
}

string compare(const string str1, const string str2) {
  if (str1.length() != str2.length()) {
    return (str1.length() > str2.length()) ? str1 : str2;
  }
  if (str1 > str2) {
    return str1;
  }
  return str2;
}

int main() {
  while (true) {
    int w, h;
    cin >> w >> h;
    if ((w | h) == 0) {
      break;
    }
    memset(field, 0, sizeof(field));
    for (int y = 0; y < h; y++) {
      cin >> field[y];
    }
    string res;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        numberTable[y][x].clear();
        if ('0' <= field[y][x] && field[y][x] <= '9') {
          string num = string(1, field[y][x]);
          for (int i = 0; i < 2; i++) {
            int ny = y + dy[i], nx = x + dx[i];
            if (0 <= ny && 0 <= nx && numberTable[ny][nx].empty() == false) {
              num = compare(numberTable[ny][nx] + field[y][x], num);
            }
          }
          clearLeftZero(num);
          if (num.empty() == false) {
            numberTable[y][x] = num;
          }
        }
        if (numberTable[y][x].empty() == false) {
          res = compare(numberTable[y][x], res);
        }
      }
    }
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

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

char field[70][70];
bool isSearched[70][70];
int w, h;
int dy[] = {-1, 0}, dx[] = {0, -1};
vector<string> numbers;
size_t maxLength;

void search(const int y, const int x, string number) {
  number.insert(0, 1, field[y][x]);
  isSearched[y][x] = true;
  bool isEnd = true;
  for (int i = 0; i < 2; i++) {
    int ny = y + dy[i], nx = x + dx[i];
    if (0 <= ny && 0 <= nx && '0' <= field[ny][nx] && field[ny][nx] <= '9') {
      isEnd = false;
      search(ny, nx, number);
    }
  }
  if (isEnd) {
    while (number.empty() == false && number[0] == '0') {
      number.erase(0, 1);
    }
    if (maxLength <= number.length()) {
      numbers.push_back(number);
      maxLength = number.length();
    }
  }
}

int main() {
  while (true) {
    cin >> w >> h;
    if ((w | h) == 0) {
      break;
    }
    memset(field, 0, sizeof(field));
    for (int y = 0; y < h; y++) {
      cin >> field[y];
    }
    numbers.clear();
    maxLength = 0;
    memset(isSearched, 0, sizeof(isSearched));
    for (int y = h - 1; y >= 0; y--) {
      for (int x = w - 1; x >= 0; x--) {
        if (isSearched[y][x] == false && '0' <= field[y][x] &&
            field[y][x] <= '9') {
          string number = "";
          search(y, x, number);
        }
      }
    }
    string maxStr = "0";
    for (vector<string>::iterator it = numbers.begin(); it != numbers.end();
         it++) {
      if ((*it).length() < maxLength) {
        continue;
      }
      if (maxStr < *it) {
        maxStr = *it;
      }
    }
    cout << maxStr << endl;
  }
}
Problem p00708 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define loop(n, i) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define HERE cout << "HERE: " << __LINE__ << endl;
#define INSP(v) cout << v << " at " << __LINE__ << endl;

using namespace std;
typedef long long ll;
struct Elem {
  int to;
  double cost;
};

bool operator>(const Elem &x, const Elem &y) {
  return tie(x.cost, x.to) > tie(y.cost, y.to);
}

int main() {
  while (1) {
    int n;
    cin >> n;
    if (!n)
      break;
    vector<double> x(n), y(n), z(n), r(n);
    loop(n, i) cin >> x[i] >> y[i] >> z[i] >> r[i];
    vector<vector<double>> dist(n, vector<double>(n));
    loop(n, i) loop(n, j) {
      double d = pow(
          pow(x[i] - x[j], 2) + pow(y[i] - y[j], 2) + pow(z[i] - z[j], 2), 0.5);
      dist[i][j] = max(0.0, d - r[i] - r[j]);
    }

    priority_queue<Elem, vector<Elem>, greater<Elem>> Q;
    vector<int> vis(n);
    Q.push({0, 0});
    double ans = 0;
    while (Q.size()) {
      Elem e = Q.top();
      Q.pop();
      if (vis[e.to])
        continue;
      vis[e.to] = 1;
      ans += e.cost;
      loop(n, i) if (i != e.to && !vis[i]) { Q.push({i, dist[e.to][i]}); }
    }
    cout << setprecision(3) << fixed << ans << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

#define loop(n, i) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define HERE cout << "HERE: " << __LINE__ << endl;
#define INSP(v) cout << v << " at " << __LINE__ << endl;

using namespace std;
typedef long long ll;
struct Elem {
  int to;
  double cost;
};

bool operator>(const Elem &x, const Elem &y) {
  return tie(x.cost, x.to) > tie(y.cost, y.to);
}

int main() {
  int n;
  while (cin >> n) {
    vector<double> x(n), y(n), z(n), r(n);
    loop(n, i) cin >> x[i] >> y[i] >> z[i] >> r[i];
    vector<vector<double>> dist(n, vector<double>(n));
    loop(n, i) loop(n, j) {
      double d = pow(
          pow(x[i] - x[j], 2) + pow(y[i] - y[j], 2) + pow(z[i] - z[j], 2), 0.5);
      dist[i][j] = max(0.0, d - r[i] - r[j]);
    }

    priority_queue<Elem, vector<Elem>, greater<Elem>> Q;
    vector<int> vis(n);
    Q.push({0, 0});
    double ans = 0;
    while (Q.size()) {
      Elem e = Q.top();
      Q.pop();
      if (vis[e.to])
        continue;
      vis[e.to] = 1;
      ans += e.cost;
      loop(n, i) if (i != e.to && !vis[i]) { Q.push({i, dist[e.to][i]}); }
    }
    cout << setprecision(8) << fixed << ans << endl;
  }
  return 0;
}
Problem p00709 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;

int w, h, n, A[11][11], ans, cnt;
bitset<100> B[11][11];

void dfs(int d, bitset<100> s, int sco) {
  if (d == n) {
    ans = min(ans, sco);
    return;
  }
  cnt++;
  if (cnt >= 30000000)
    return;
  if (ans <= sco)
    return;
  if (!A[d / w][d % w])
    dfs(d + 1, s, sco);
  else {
    int y = d / w;
    int x = d % w;
    if ((B[y][x] & s).any()) {
      bitset<100> p;
      p.set(y * 10 + x); //(y*100+x);
      if (!(s & p).any())
        dfs(d + 1, s, sco);
      dfs(d + 1, (B[y][x] | s) ^ B[y][x], sco + 1);
    } else
      dfs(d + 1, s, sco);
  }
}

int main() {
  while (cin >> w >> h, w) {
    memset(A, 0, sizeof(A));
    r(i, 11) r(j, 11) B[i][j] &= 0;
    n = h * w;
    bitset<100> b;
    r(i, h) r(j, w) cin >> A[i][j];
    r(i, h) r(j, w) if (A[i][j]) {
      int sum = 1;
      for (;; sum++) {
        int f = 0;
        for (int y = i; y < i + sum; y++) {
          for (int x = j; x < j + sum; x++) {
            if (A[y][x] == 0)
              f++;
          }
        }
        if (f) {
          sum--;
          break;
        }
      }
      bitset<100> tmp;
      for (int y = i, k = 0; k < sum; k++, y++) {
        for (int x = j, l = 0; l < sum; l++, x++) {
          tmp.set(y * 10 + x);
        }
      }
      b.set(i * 10 + j);
      B[i][j] = tmp;
    }
    ans = 100;
    cnt = 0;
    dfs(0, b, 0);
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;

int w, h, n, A[11][11], ans, cnt;
bitset<100> B[11][11];

void dfs(int d, bitset<100> s, int sco) {
  if (d == n) {
    ans = min(ans, sco);
    return;
  }
  cnt++;
  if (cnt >= 100000000)
    return;
  if (ans <= sco)
    return;
  if (!A[d / w][d % w])
    dfs(d + 1, s, sco);
  else {
    int y = d / w;
    int x = d % w;
    if ((B[y][x] & s).any()) {
      bitset<100> p;
      p.set(y * 10 + x); //(y*100+x);
      if (!(s & p).any())
        dfs(d + 1, s, sco);
      dfs(d + 1, (B[y][x] | s) ^ B[y][x], sco + 1);
    } else
      dfs(d + 1, s, sco);
  }
}

int main() {
  while (cin >> w >> h, w) {
    memset(A, 0, sizeof(A));
    r(i, 11) r(j, 11) B[i][j] &= 0;
    n = h * w;
    bitset<100> b;
    r(i, h) r(j, w) cin >> A[i][j];
    r(i, h) r(j, w) if (A[i][j]) {
      int sum = 1;
      for (;; sum++) {
        int f = 0;
        for (int y = i; y < i + sum; y++) {
          for (int x = j; x < j + sum; x++) {
            if (A[y][x] == 0)
              f++;
          }
        }
        if (f) {
          sum--;
          break;
        }
      }
      bitset<100> tmp;
      for (int y = i, k = 0; k < sum; k++, y++) {
        for (int x = j, l = 0; l < sum; l++, x++) {
          tmp.set(y * 10 + x);
        }
      }
      b.set(i * 10 + j);
      B[i][j] = tmp;
    }
    ans = 100;
    cnt = 0;
    dfs(0, b, 0);
    cout << ans << endl;
  }
}
Problem p00710 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int main() {
  int n, r, p, c;

  while (cin >> n >> r) {
    if ((n == 0) && (r == 0)) {
      return 0;
    }
    vector<int> card;
    for (int i = 0; i < n; i++) {
      card.push_back(n - i);
    }
    for (int i = 0; i < r; i++) {
      cin >> p >> c;
      rotate(card.begin(), card.begin() + p - 1, card.begin() + p - 1 + c);
    }
    cout << card[0] << endl;
  }
}

Error: -11

Fixed submission:

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

using namespace std;

int main() {
  int n, r, p, c;
  vector<int> card;

  while (cin >> n >> r) {
    if ((n == 0) && (r == 0)) {
      return 0;
    }
    for (int i = 0; i < n; i++) {
      card[i] = i + 1;
    }
    cin >> p >> c;
    for (int i = 0; i < r; i++) {
      rotate(card.begin(), card.begin() + p, card.begin() + p - 1 + c);
    }
    cout << card[0] << endl;
  }
}
Problem p00710 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define FOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000            // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979

using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;

int main(void) {

  while (1) {
    int n, r;
    int p, c;
    int a[51] = {};
    int b[51] = {};

    cin >> n >> r;
    if (n == 0 && r == 0) {
      break;
    }
    FOR(i, 1, n) { a[i] = n - i + 1; }
    FOR(i, 1, r) {
      cin >> p >> c;
      FOR(t, 1, p - 1) { b[t] = a[t]; }
      FOR(t, 1, c) { a[t] = a[p + t - 1]; }
      FOR(t, 1, p - 1) { a[c + t] = b[t]; }
    }
    cout << a[1] << endl;
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define FOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000            // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979

using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;

int main(void) {

  while (1) {
    int n, r;
    int p, c;
    int a[51] = {};
    int b[51] = {};
    FOR(i, 1, n) { a[i] = n - i + 1; }
    cin >> n >> r;
    if (n == 0 && r == 0) {
      break;
    }
    FOR(i, 1, r) {
      cin >> p >> c;
      FOR(t, 1, p - 1) { b[t] = a[t]; }
      FOR(t, 1, c) { a[t] = a[p + t - 1]; }
      FOR(t, 1, p - 1) { a[p + t - 1] = b[t]; }
    }
    cout << a[1] << endl;
  }
}
Problem p00710 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <deque>
#include <exception>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define LL long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<LL, LL> LP;
typedef pair<int, P> PP;
typedef pair<LL, LP> LPP;

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

int main() {
  int n, r;

  while (cin >> n >> r, n + r) {
    int huda[51];
    REP(i, n) { huda[i] = n - i; }

    while (r--) {
      int p, c;
      int tmp[51];
      cin >> p >> c;

      REP(i, c) { tmp[i] = huda[p + i - 1]; }

      REPR(i, p - 2) { huda[i + c] = huda[i]; }

      REP(i, c) { huda[i] = tmp[i]; }
    }

    cout << huda[0] << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <deque>
#include <exception>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define LL long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<LL, LL> LP;
typedef pair<int, P> PP;
typedef pair<LL, LP> LPP;

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

int main() {
  int n, r;

  while (cin >> n >> r, n + r) {
    int huda[51];
    REP(i, n) { huda[i] = n - i; }

    while (r--) {
      int p, c;
      int tmp[c];
      cin >> p >> c;

      REP(i, c) { tmp[i] = huda[p + i - 1]; }

      REPR(i, p - 2) { huda[i + c] = huda[i]; }

      REP(i, c) { huda[i] = tmp[i]; }
    }

    cout << huda[0] << endl;
  }
}
Problem p00710 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define REP(i, a, n) for (int i = a; i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(s) s.begin(), s.end()
#define rall(s) s.rbegin(), s.rend()
#define range(x, min, max) ((min) <= (x) && (x) <= (max))

using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<vector<int>> VII;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef pair<int, int> PII;

const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, r;
  VI ps, cs, cards, temp;
  while (1) {
    ps.clear();
    cs.clear();
    cards.clear();
    cin >> n >> r;
    if (n == 0 && r == 0)
      break;
    rep(i, n) { cards.pb(i + 1); }
    reverse(all(cards));
    rep(i, r) {
      int p, c;
      cin >> p >> c;
      ps.pb(p);
      cs.pb(c);
    }
    rep(i, r) {
      temp.clear();
      REP(j, ps[i] - 1, ps[i] + cs[i] - 1) { temp.pb(cards[j]); }
      rep(j, ps[i] - 1) { temp.pb(cards[j]); }
      REP(j, ps[i] + cs[i] - 1, cards.size()) { temp.pb(cards[j]); }
      cards.clear();
      cards = temp;
    }
    cout << cards[0] << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define REP(i, a, n) for (int i = a; i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(s) s.begin(), s.end()
#define rall(s) s.rbegin(), s.rend()
#define range(x, min, max) ((min) <= (x) && (x) <= (max))

using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<vector<int>> VII;
typedef pair<int, int> PII;

const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, r;
  VI cards;
  vector<PII> cuts;
  while (1) {
    cards.clear();
    cuts.clear();
    cin >> n >> r;
    if (n == 0 && r == 0)
      break;
    rep(i, r) {
      int p, c;
      cin >> p >> c;
      cuts.pb(mp(p, c));
    }
    rep(i, n) { cards.pb(n - i); }
    rep(i, r) {
      VI cs(n);
      int p = cuts[i].first;
      int c = cuts[i].second;
      int now = 0;
      REP(j, p - 1, p + c - 1) { cs[now++] = cards[j]; }
      rep(j, p) { cs[now++] = cards[j]; }
      REP(j, p + c, cards.size()) { cs[now++] = cards[j]; }
      cards = cs;
    }
    cout << cards[0] << endl;
  }
  return 0;
}
Problem p00710 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

vector<int> cut(vector<int> &card, int ft, int noc) {
  vector<int>::iterator itr;
  for (itr = card.begin(); itr != card.end(); ++itr) {
    if (*itr < ft) {
      *itr += noc;
    } else if (*itr < ft + noc) {
      *itr -= ft - 1;
    }
  }
  // for(itr = card.begin(); itr != card.end(); ++itr)
  // cout << *itr <<endl;
  // cout << endl;
  return card;
}

int main(void) {
  int num, cut_num, top;
  vector<int>::iterator itr;

  while (1) {
    cin >> num >> cut_num;
    if (num == 0 && cut_num == 0) {
      break;
    }

    vector<int> card;
    for (int i = 0; i < num; i++) {
      card.push_back(i + 1);
    }

    for (int i = 0; i < cut_num; i++) {
      int from_top, num_of_cards;
      cin >> from_top >> num_of_cards;
      card = cut(card, from_top, num_of_cards);
    }

    itr = card.begin();
    for (int i = 0; i < num; i++) {
      if (*itr == 1) {
        top = i;
        break;
      }
      itr += 1;
    }
    cout << num - top << endl;
  }
  return 0;
}

Error: -6
double free or corruption (out)


Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

vector<int> cut(vector<int> &card, int ft, int noc) {
  vector<int>::iterator itr;
  for (int i = 0; i < noc; i++) {
    itr = card.end() - ft - (noc - 1);
    card.push_back(*itr);
    card.erase(itr);
  }
  return card;
}

int main(void) {
  int num, cut_num;
  vector<int>::iterator itr;

  while (1) {
    cin >> num >> cut_num;
    if (num == 0 && cut_num == 0) {
      break;
    }

    vector<int> card;
    for (int i = 0; i < num; i++) {
      card.push_back(i + 1);
    }

    for (int i = 0; i < cut_num; i++) {
      int from_top, num_of_cards;
      cin >> from_top >> num_of_cards;
      card = cut(card, from_top, num_of_cards);
    }
    itr = card.end();
    cout << *itr << endl;
    // for(itr = card.end() - 1; itr  >= card.begin(); itr--)
    // cout << *itr << endl;
    // cout << endl;
  }
  return 0;
}
Problem p00710 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

void sort(int *x, int *y) {
  int temp;
  temp = *x;
  *x = *y;
  *y = temp;
}

int main(void) {
  int n, r;
  while (cin >> n >> r && n != 0 && r != 0) {
    vector<int> A(n);
    for (int i = 0; i < n; i++) {
      A[i] = n - i;
    }

    int p, c;

    for (int i = 0; i < r; i++) {
      cin >> p >> c;
      vector<int> B(p);

      int j = 0;
      for (int i = 0; i < p - 1; i++)
        B[i] = A[i];

      for (int i = p - 1; i < p + c - 1; i++)
        sort(&A[i], &A[i - (p - 1)]);

      for (int i = c; i < c + p - 1; i++)
        A[i] = B[i - c];
    }
    cout << A[0] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

using namespace std;

int main(void) {
  int n, r;
  while (cin >> n >> r && n != 0 && r != 0) {
    vector<int> A(n);
    for (int i = 0; i < n; i++) {
      A[i] = n - i;
    }

    int p, c;

    for (int i = 0; i < r; i++) {
      cin >> p >> c;

      if (c != A.size() && ((p + c - 1) != n)) {
        rotate(A.begin(), A.begin() + A.size() - p,
               A.begin() + A.size() - p + c);
      } else if ((p + c - 1) == A.size()) {
        rotate(A.begin(), A.begin() + A.size() - c, A.end());
      }
    }
    cout << A[0] << endl;
  }
  return 0;
}
Problem p00710 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n, r;
  while (cin >> n >> r && n != 0 && r != 0) {
    list<int> a[2];
    for (int i = n; i >= 1; i--) {
      a[0].push_back(i);
    }
    int p, c;
    list<int>::iterator it = a[0].begin(), it2;
    for (int z = 0; z < r; z++) {
      cin >> p >> c;
      it = a[0].begin();
      a[1].clear();
      for (int i = 1; i < p; i++)
        it++;
      for (int i = 0; i < c; i++) {
        a[1].push_back(*it);
        it2 = it;
        it2++;
        a[0].erase(it);
        it = it2;
      }
      a[0].insert(a[0].begin(), a[1].begin(), a[1].end());
    }
    it = a[0].begin();
    cout << (*it) << endl;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
using namespace std;
bool b[33000];
int main() {
  int n, m;
  while (cin >> n >> m && n != 0) {
    list<int> a;
    list<int>::iterator it;
    list<int>::iterator it2;
    for (int i = 1; i <= n; i++)
      a.push_back(i);
    for (int z = 0; z < m; z++) {
      list<int> b;
      int p, s;
      cin >> p >> s;
      for (int i = 0; i < p; i++)
        it++;
      it2 = it;
      for (int i = 0; i < s; i++) {
        it = it2;
        b.push_back(*it);
        it2 = it;
        it2++;
        a.erase(it);
      }
      b.merge(a);
      a = b;
    }
    it = a.begin();
    cout << (*it) << endl;
  }
}
Problem p00710 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int cut(int n, int r, int s[60]) {
  int p, c;
  int temp[60] = {0};

  for (int i = 0; i < r; i++) {
    cin >> p >> c;
    for (int j = 0; j <= n; j++) {
      temp[j] = s[j];
    }
    for (int j = 0; j < p; j++) {
      s[n - p - c + j + 2] = temp[n - p + j + 2];
    }
    for (int j = 0; j < c; j++) {
      s[n - c + 1 + j] = temp[n - p - c + j + 2];
    }
  }

  return s[n];
}

int main() {
  int n, r, p, c, s[60];
  while (1) {
    cin >> n >> r;
    if (n == 0 && r == 0)
      break;

    for (int i = 0; i <= n; i++) {
      s[i] = i;
    }

    int ans = cut(n, r, s);

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int cut(int n, int r, int p, int c, int s[60]) {
  int temp[60] = {0};

  for (int i = 0; i < r; i++) {
    for (int j = 0; j <= n; j++) {
      temp[j] = s[j];
    }
    for (int j = 0; j < p; j++) {
      s[n - p - c + j + 2] = temp[n - p + j + 2];
    }
    for (int j = 0; j < c; j++) {
      s[n - c + 1 + j] = temp[n - p - c + j + 2];
    }
  }

  return s[n];
}

int main() {
  int n, r, p, c, s[60];
  while (1) {
    cin >> n >> r;
    if (n == 0 && r == 0)
      break;

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

    if (n + 1 < p + c)
      continue;

    int ans = cut(n, r, p, c, s);

    cout << ans << endl;
  }

  return 0;
}