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

// AOJ 0508
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

int n;
std::vector<int> es[101];
bool used[101];

int dfs(int p) {
  int ret = 1;

  for (int i = 0; i != es[p].size(); ++i) {
    if (!used[es[p][i]]) {
      used[es[p][i]] = true;
      ret = std::max(ret, dfs(es[p][i]) + 1);
      used[es[p][i]] = false;
    }
  }

  return ret;
}

int main() {
  while (std::cin >> n, n) {
    for (int i = 0; i <= 100; ++i)
      es[i].clear();

    for (int i = 0; i != n; ++i) {
      int a, b;
      std::cin >> a >> b;

      es[a].push_back(b);
      es[b].push_back(a);
    }

    int m = 101;
    for (int i = 1; i <= 100; ++i)
      if (!es[i].empty())
        m = std::min(m, (int)es[i].size());

    int ans = 0;
    for (int i = 1; i <= 100; ++i) {
      if (es[i].size() == m) {
        memset(used, false, sizeof(used));
        used[i] = true;
        ans = std::max(ans, dfs(i));
      }
    }

    std::cout << ans << std::endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int n;
std::vector<int> es[100];
bool used[100];

int dfs(int p) {
  int ret = 0;

  for (int i = 0; i != es[p].size(); ++i) {
    if (!used[es[p][i]]) {
      used[es[p][i]] = true;
      ret = std::max(ret, dfs(es[p][i]) + 1);
      used[es[p][i]] = false;
    }
  }

  return ret;
}

int main() {
  while (std::cin >> n, n) {
    int M = 0;

    for (int i = 0; i != n; ++i)
      es[i].clear();

    for (int i = 0; i != n; ++i) {
      int a, b;
      std::cin >> a >> b;

      M = std::max(M, std::max(a, b));

      es[a - 1].push_back(b - 1);
      es[b - 1].push_back(a - 1);
    }

    int ans = 0;
    for (int i = 0; i != M; ++i) {
      memset(used, false, sizeof(used));
      ans = std::max(ans, dfs(i));
    }

    std::cout << ans << std::endl;
  }

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

bool isVisited[101];

int dfs(const vector<vector<int>> &r, int pos) {
  if (isVisited[pos]) {
    return 0;
  }

  isVisited[pos] = true;
  int ret = 0;
  for (int i = 0; i < r[pos].size(); i++) {
    ret = max(ret, dfs(r, r[pos][i]));
  }
  isVisited[pos] = false;

  return ret + 1;
}

int main(void) {
  for (int i = 0; i <= 100; i++) {
    isVisited[i] = false;
  }

  int n;
  while (cin >> n, n) {
    vector<vector<int>> rings(101);
    int a, b;
    for (int i = 0; i < n; i++) {
      cin >> a >> b;
      rings[a].push_back(b);
      rings[b].push_back(a);
    }

    int ans = 0;
    for (int i = 1; i <= 100; i++) {
      ans = max(ans, dfs(rings, i));
    }
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

bool isVisited[101];

int dfs(const vector<vector<int>> &r, int pos) {
  if (isVisited[pos]) {
    return 0;
  }

  isVisited[pos] = true;
  int ret = 0;
  for (int i = 0; i < r[pos].size(); i++) {
    ret = max(ret, dfs(r, r[pos][i]));
  }
  isVisited[pos] = false;

  return ret + 1;
}

int main(void) {
  for (int i = 0; i <= 100; i++) {
    isVisited[i] = false;
  }

  int n;
  while (cin >> n, n) {
    vector<vector<int>> rings(n + 1);
    int a, b;
    for (int i = 0; i < n; i++) {
      cin >> a >> b;
      rings[a].push_back(b);
      rings[b].push_back(a);
    }

    int ans = 0;
    for (int i = 1; i <= n; i++) {
      ans = max(ans, dfs(rings, i));
    }
    cout << ans << endl;
  }

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

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

vector<int> E[100];
bool flag[100];
int Max;

void solve(int v, int len) {
  if (len > Max)
    Max = len;
  for (int u : E[v]) {
    if (!flag[u]) {
      flag[u] = true;
      solve(u, len + 1);
      flag[u] = false;
    }
  }
}
int main() {
  int n;
  while (scanf("%d", &n), n) {
    rep(i, 100) E[i].clear();
    memset(flag, 0, sizeof(flag));
    rep(i, n) {
      int a, b;
      scanf("%d%d", &a, &b);
      a--;
      b--;
      E[a].push_back(b);
      E[b].push_back(a);
    }
    Max = 0;
    rep(i, 100) {
      flag[i] = true;
      solve(i, 1);
      flag[i] = false;
    }
    printf("%d\\n", Max);
  }
}

Error: MLE

Fixed submission:

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

vector<int> E[100];
int d[100];

struct P {
  int c, v;
  string s;
};
void bfs(int s) {
  queue<P> que;
  memset(d, 0, sizeof(d));
  string S;
  rep(i, 100) S += '0';
  S[s] = '1';
  que.push({1, s, S});
  d[s] = 1;
  while (!que.empty()) {
    P u = que.front();
    que.pop();
    for (int e : E[u.v]) {
      if (u.s[e] == '0') {
        if (d[e] < u.c + 1)
          d[e] = u.c + 1;
        string v = u.s;
        v[e] = '1';
        que.push({d[e], e, v});
      }
    }
  }
}
int solve(int s) {
  bfs(s);
  int Max = 0, t = 0;
  rep(i, 100) {
    if (d[i] == INF)
      continue;
    if (Max < d[i]) {
      Max = d[i];
      t = i;
    }
  }
  bfs(t);
  Max = 0;
  rep(i, 100) {
    if (d[i] == INF)
      continue;
    Max = max(Max, d[i]);
  }
  return Max;
}
int main() {
  int n;
  while (scanf("%d", &n), n) {
    rep(i, 100) E[i].clear();
    rep(i, n) {
      int a, b;
      scanf("%d%d", &a, &b);
      a--;
      b--;
      E[a].push_back(b);
      E[b].push_back(a);
    }
    int Max = 0;
    rep(i, 100) Max = max(Max, solve(i));
    printf("%d\\n", Max);
  }
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, n) for (int i = 1; i <= n; i++)

vector<int> load[111];

int visit[111];
int visitone[111];

int saiki(int a) {
  visitone[a] = 1;

  int ret = 0;
  visit[a] = 1;

  rep(i, load[a].size()) {
    if (visit[load[a][i]] == 0) {
      ret = max(ret, saiki(load[a][i]));
    }
  }

  visit[a] = 0;
  return 1 + ret;
}

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    rep(i, 111) {
      load[i].clear();
      visitone[i] = 1;
      visit[i] = 0;
    }

    rep(i, n) {
      int a, b;
      cin >> a >> b;
      load[a].push_back(b);
      load[b].push_back(a);
      visitone[a] = visitone[b] = 0;
    }

    int ans = 0;

    rep(i, 1000) {
      reps(j, 100) {
        if (load[j].size() == i) {
          ans = max(ans, saiki(j));
        }
      }

      int flg = 1;
      reps(j, 100) {
        if (visitone[j] == 0)
          flg = 0;
      }
      if (flg == 1)
        break;
    }

    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, n) for (int i = 1; i <= n; i++)

vector<int> load[111];

int visit[111];

int saiki(int a) {
  int ret = 0;
  visit[a] = 1;

  rep(i, load[a].size()) {
    if (visit[load[a][i]] == 0) {
      ret = max(ret, saiki(load[a][i]));
    }
  }

  visit[a] = 0;
  return 1 + ret;
}

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    rep(i, 111) load[i].clear();

    rep(i, n) {
      int a, b;
      cin >> a >> b;
      load[a].push_back(b);
      load[b].push_back(a);
    }

    int ans = 0;
    reps(i, n) { ans = max(ans, saiki(i)); }

    printf("%d\\n", ans);
  }
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

vector<vector<int>> E(100);
bool visited[100];

int solve(const int v) {
  int res = 0;
  visited[v] = true;
  for (vector<int>::iterator it = E[v].begin(); it != E[v].end(); ++it) {
    if (!visited[*it]) {
      visited[*it] = true;
      res = max(res, solve(*it) + 1);
      visited[*it] = false;
    }
  }
  visited[v] = false;
  return res;
}

int main() {
  int n;
  while (scanf("%d", &n)) {
    if (n == 0)
      break;

    for (int i = 0; i < 100; ++i)
      E[i].clear();

    for (int i = 0; i < n; ++i) {
      int a, b;
      scanf("%d %d", &a, &b);
      --a;
      --b;
      E[a].push_back(b);
      E[b].push_back(a);
    }

    int ans = 0;
    memset(visited, false, sizeof(visited));
    for (int i = 0; i < 100; ++i)
      ans = max(ans, solve(i));

    printf("%d\\n", ans + 1);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

vector<vector<int>> E(100);
bool vv[100], ve[100][100];

int solve(const int v) {
  int res = 0;
  vv[v] = true;
  for (unsigned int i = 0; i < E[v].size(); ++i) {
    if (!vv[E[v][i]] && !ve[v][E[v][i]] && !ve[E[v][i]][v]) {
      ve[v][E[v][i]] = ve[E[v][i]][v] = true;
      res = max(res, solve(E[v][i]) + 1);
      ve[v][E[v][i]] = ve[E[v][i]][v] = false;
    }
  }
  vv[v] = false;
  return res;
}

int main() {
  int n;
  while (scanf("%d", &n)) {
    if (n == 0)
      break;

    for (int i = 0; i < 100; ++i)
      E[i].clear();

    for (int i = 0; i < n; ++i) {
      int a, b;
      scanf("%d %d", &a, &b);
      --a;
      --b;
      E[a].push_back(b);
      E[b].push_back(a);
    }

    int ans = 0;
    memset(vv, false, sizeof(vv));
    memset(ve, false, sizeof(ve));
    for (int i = 0; i < 100; ++i)
      ans = max(ans, solve(i));

    printf("%d\\n", ans + 1);
  }
  return 0;
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define EQ(a, b) (abs((a) - (b)) < EPS)
vi lis[101];
int cnt = 0;
int p = 1;
bool used[101];
void dfs(int s) {
  int i, j;
  for (i = 0; i < lis[s].size(); i++) {
    if (!(used[lis[s][i]])) {
      used[lis[s][i]] = true;
      p++;
      dfs(lis[s][i]);
    }
    if (p > 2)
      cnt = max(cnt, p);
  }
  used[s] = false;
  p--;
  return;
}
int main() {
  int e;
  while (1) {
    cnt = 0;
    cin >> e;
    if (e == 0)
      break;
    int mv = -1;
    for (int i = 0; i < 101; i++) {
      lis[i].clear();
    }
    for (int i = 0; i < e; i++) {
      int a, b;
      cin >> a >> b;
      mv = max(mv, max(a, b));
      lis[a].pb(b);
      lis[b].pb(a);
    }
    for (int i = 1; i <= mv; i++) {
      p = 1;
      memset(used, 0, sizeof(used));
      used[i] = true;
      dfs(i);
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define EQ(a, b) (abs((a) - (b)) < EPS)
vi lis[101];
int cnt = 0;
vi used;
void dfs(int s) {
  int i, j;
  for (i = 0; i < lis[s].size(); i++) {
    bool flag = true;
    for (j = 0; j < used.size(); j++) {
      if (used[j] == lis[s][i]) {
        flag = false;
        break;
      }
    }
    if (flag) {
      used.pb(lis[s][i]);
      dfs(lis[s][i]);
    }
    if (sz(used) > 2)
      cnt = max(cnt, sz(used));
  }
  used.pop_back();
  return;
}
int main() {
  int e;
  while (1) {
    cnt = 0;
    cin >> e;
    if (e == 0)
      break;
    int mv = -1;
    for (int i = 0; i < 101; i++) {
      lis[i].clear();
    }
    for (int i = 0; i < e; i++) {
      int a, b;
      cin >> a >> b;
      mv = max(mv, max(a, b));
      lis[a].pb(b);
      lis[b].pb(a);
    }
    for (int i = 1; i <= mv / 4 * 3; i++) {
      used.clear();
      used.pb(i);
      dfs(i);
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int n, ans, u[100];
vector<int> ad[100];
void dfs(int c, int m) {
  if (ans < m)
    ans = m;
  int s = ad[c].size(), t;
  rep(i, s) {
    t = ad[c][i];
    if (!u[t])
      u[t] = 1, dfs(t, m + 1), u[t] = 0;
  }
}
int main() {
  while (scanf("%d", &n), n) {
    ans = 1;
    rep(i, 100) u[i] = 0, ad[i].clear();
    rep(i, n) {
      int a, b;
      scanf("%d%d", &a, &b);
      a--, b--;
      ad[a].push_back(b), ad[b].push_back(a);
    }
    rep(i, 100) if (!ad[i].empty()) {
      u[i] = 1;
      dfs(i, 1);
      u[i] = 0;
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int n, ans, u[100];
vector<vector<int>> ad;
void dfs(int c, int m) {
  if (ans < m)
    ans = m;
  int s = ad[c].size(), t;
  rep(i, s) {
    t = ad[c][i];
    if (!u[t])
      u[t] = 1, dfs(t, m + 1), u[t] = 0;
  }
}
int main() {
  while (scanf("%d", &n), n) {
    ad.clear();
    ad.resize(100);
    rep(i, n) {
      int a, b;
      scanf("%d%d", &a, &b);
      a--, b--;
      ad[a].push_back(b), ad[b].push_back(a);
    }
    ans = 1;
    rep(i, 100) u[i] = 0;
    rep(i, 100) if (!ad[i].empty()) {
      u[i] = 1;
      dfs(i, 1);
      u[i] = 0;
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00432 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

#define ALL(c) begin(c), end(c)

struct Event {
  int type, x1, x2, y;
  bool operator<(const Event &rhs) const { return y < rhs.y; }
};

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

  int n, r;
  while (cin >> n >> r, n | r) {
    vector<Event> events;
    int W = 0, H = 0;
    for (int i = 0; i < n; ++i) {
      int x1, y1, x2, y2;
      cin >> x1 >> y1 >> x2 >> y2;
      ++x1;
      ++y1;
      ++x2;
      ++y2;
      events.push_back({+1, x1, x2, y1});
      events.push_back({-1, x1, x2, y2});
      W = max(W, x2 + 1);
      H = max(H, y2 + 1);
    }
    sort(ALL(events));

    vector<vector<int>> field(2, vector<int>(W));
    int area = 0, perimeter = 0;
    for (int y = 1, k = 0; y < H; ++y) {
      int curr = y % 2, prev = (y - 1) % 2;
      field[curr] = field[prev];
      for (; k < (int)events.size() && events[k].y == y; ++k) {
        auto &e = events[k];
        for (int x = e.x1; x < e.x2; ++x)
          field[curr][x] += e.type;
      }
      for (int x = 1; x < W; ++x) {
        int b = !!field[curr][x];
        area += b;
        perimeter += (!!field[curr][x - 1] ^ b) + (!!field[prev][x] ^ b);
      }
    }

    if (r == 1)
      cout << area << endl;
    else
      cout << area << endl << perimeter << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

#define ALL(c) begin(c), end(c)

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

  int n, r;
  while (cin >> n >> r, n | r) {
    vector<int> x1(n), y1(n), x2(n), y2(n);
    for (int i = 0; i < n; ++i) {
      cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
      ++x1[i];
      ++y1[i];
      ++x2[i];
      ++y2[i];
    }
    int W = *max_element(ALL(x2)) + 1;
    int H = *max_element(ALL(y2)) + 1;

    vector<vector<bool>> field(H, vector<bool>(W, false));
    for (int i = 0; i < n; ++i)
      for (int y = y1[i]; y < y2[i]; ++y)
        fill(begin(field[y]) + x1[i], begin(field[y]) + x2[i], true);

    if (r == 1) {
      int ans = 0;
      for (int y = 0; y < H; ++y)
        ans += count(ALL(field[y]), true);
      cout << ans << endl;
    } else {
      int ansS = 0, ansL = 0;
      for (int y = 1; y < H; ++y) {
        for (int x = 1; x < W; ++x) {
          int b = field[y][x];
          ansS += b;
          ansL += (field[y][x - 1] ^ b) + (field[y - 1][x] ^ b);
        }
      }
      cout << ansS << endl << ansL << endl;
    }
  }
}
Problem p00432 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#define rep(i, n) for (int i = 0; i < n; i++)

int n, r;
int x1[10000], y1[10000], x2[10000], y2[10000];
int dp[2][10010];

int main() {
  while (scanf("%d%d", &n, &r), n || r) {
    int ans = 0, ans2 = 0;
    rep(i, 2) rep(j, 10010) dp[i][j] = 0;
    rep(i, n) {
      scanf("%d%d%d%d", &x1[i], &y1[i], &x2[i], &y2[i]);
      x1[i]++;
      y1[i]++;
    }
    for (int u = 1; u < 10010; u++) {
      for (int v = 0; v < n; v++) {
        if (y1[v] == u) {
          dp[1][x1[v]]++;
          dp[1][x2[v] + 1]--;
        }
        if (y2[v] + 1 == u) {
          dp[1][x1[v]]--;
          dp[1][x2[v] + 1]++;
        }
      }
      for (int v = 1; v < 10010; v++)
        dp[1][v] += dp[1][v - 1];
      for (int v = 0; v < 10010; v++)
        dp[1][v] += dp[0][v];
      for (int v = 0; v < 10010; v++)
        if (dp[1][v] > 0)
          ans++;
      if (r == 2) {
        for (int v = 1; v < 10009; v++) {
          if (dp[1][v] == 0)
            continue;
          ans2 += (dp[0][v] == 0) + (dp[1][v - 1] == 0) + (dp[1][v + 1] == 0);
        }
        for (int v = 0; v < 10010; v++) {
          if (dp[0][v] == 0)
            continue;
          ans2 += (dp[1][v] == 0);
        }
      }
      for (int v = 0; v < 10010; v++) {
        dp[0][v] = dp[1][v];
        dp[1][v] = 0;
      }
    }
    printf("%d\\n", ans);
    if (r == 2)
      printf("%d\\n", ans2);
  }
}

Error: MLE

Fixed submission:

#include <cstdio>
#define rep(i, n) for (int i = 0; i < n; i++)

int n, r;
int m[10010][10010];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

int main() {
  while (scanf("%d%d", &n, &r), n || r) {
    rep(i, 10010) rep(j, 10010) m[i][j] = 0;
    rep(i, n) {
      int x1, x2, y1, y2;
      scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
      x1++;
      y1++;
      m[x1][y1]++;
      m[x2 + 1][y1]--;
      m[x1][y2 + 1]--;
      m[x2 + 1][y2 + 1]++;
    }
    rep(i, 10010) for (int j = 1; j < 10010; j++) { m[i][j] += m[i][j - 1]; }
    rep(i, 10010) for (int j = 1; j < 10010; j++) { m[j][i] += m[j - 1][i]; }
    int s = 0;
    rep(i, 10010) rep(j, 10010) if (m[i][j] > 0) s++;
    printf("%d\\n", s);
    if (r == 1)
      continue;
    s = 0;
    rep(i, 10010) rep(j, 10010) {
      if (m[i][j] == 0)
        continue;
      rep(k, 4) {
        if (0 <= i + dx[k] && i + dx[k] < 10010 && 0 <= j + dy[k] &&
            j + dy[k] < 10010 && m[i + dx[k]][j + dy[k]] == 0)
          s++;
      }
    }
    printf("%d\\n", s);
  }
}
Problem p00432 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

struct manip {
  int y;
  int xs, xt;
  int val;
  manip(int y, int xs, int xt, int val) : y(y), xs(xs), xt(xt), val(val) {}
  manip() {}
  bool operator<(const manip &a) { return y < a.y; }
};
const int size = 10003;
int field[2][10003];
manip q[20001];

int main() {
  while (1) {
    int n, r;
    scanf("%d%d", &n, &r);
    if (r == 0)
      break;

    memset(field, 0, sizeof(field));
    for (int i = 0; i < n; i++) {
      int xs, ys, xt, yt;
      scanf("%d%d%d%d", &xs, &ys, &xt, &yt);
      xs++;
      ys++;
      xt++;
      yt++;
      q[2 * i] = manip(ys, xs, xt, 1);
      q[2 * i + 1] = manip(yt, xs, xt, -1);
    }
    sort(q, q + 2 * n);
    q[2 * n].y = 1e9;

    int area = 0;
    int outlen = 0;
    int pt = 0;
    for (int i = 0; i < size; i++) {
      memcpy(field[i % 2], field[1 - i % 2],
             sizeof(int) * size /*sizeof(field[0])*/);
      bool changed = false;
      int diff[10003] = {};
      while (q[pt].y == i) {
        changed = true;
        diff[q[pt].xs] += q[pt].val;
        diff[q[pt].xt] -= q[pt].val;
        pt++;
      }
      if (changed) {
        for (int j = 0; j < size - 1; j++) {
          diff[j + 1] += diff[j];
          field[i % 2][j] += diff[j];
        }
      }
      for (int j = 0; j < size - 1; j++) {
        if (field[i % 2][j] > 0)
          area++;
        if ((field[i % 2][j] == 0) != (field[i % 2][j + 1] == 0))
          outlen++;
        if ((field[0][j] == 0) != (field[1][j] == 0))
          outlen++;
      }
    }
    printf("%d\\n", area);
    if (r == 2) {
      printf("%d\\n", outlen);
    }
  }
  return 0;
}


Error: MLE

Fixed submission:

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

const int size = 10003;
short field[10003][10003];

int main() {
  while (1) {
    int n, r;
    cin >> n >> r;
    if (r == 0)
      break;

    memset(field, 0, sizeof(field));
    for (int i = 0; i < n; i++) {
      int xs, ys, xt, yt;
      cin >> xs >> ys >> xt >> yt;
      xs++;
      ys++;
      xt++;
      yt++;
      field[xs][ys]++;
      field[xs][yt]--;
      field[xt][ys]--;
      field[xt][yt]++;
    }
    for (int i = 0; i < size; i++) {
      for (int j = 0; j < size - 1; j++) {
        field[i][j + 1] += field[i][j];
      }
    }
    for (int j = 0; j < size; j++) {
      for (int i = 0; i < size - 1; i++) {
        field[i + 1][j] += field[i][j];
      }
    }

    int area = 0;
    int outlen = 0;
    for (int i = 1; i < size - 1; i++) {
      for (int j = 1; j < size - 1; j++) {
        if (field[i][j] > 0) {
          area++;
          if (field[i + 1][j] == 0)
            outlen++;
          if (field[i][j + 1] == 0)
            outlen++;
          if (field[i - 1][j] == 0)
            outlen++;
          if (field[i][j - 1] == 0)
            outlen++;
        }
      }
    }
    cout << area << endl;
    if (r == 2) {
      cout << outlen << endl;
    }
  }
  return 0;
}