Problem p03876 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
vector<pii> comp(vector<pii> v) {
  vector<int> za, zb;
  for (int i = 0; i < v.size(); i++)
    za.push_back(v[i].first), zb.push_back(v[i].second);
  sort(za.begin(), za.end());
  sort(zb.begin(), zb.end());
  for (int i = 0; i < v.size(); i++) {
    v[i].first =
        (lower_bound(za.begin(), za.end(), v[i].first) - za.begin()) * 2;
    v[i].second =
        (lower_bound(zb.begin(), zb.end(), v[i].second) - zb.begin()) * 2;
  }
  return v;
}
vector<pii> calc(vector<int> v) {
  if (v.size() == 4) {
    vector<pii> r;
    r.push_back(make_pair(0, 0));
    r.push_back(make_pair(2, 0));
    r.push_back(make_pair(2, 2));
    r.push_back(make_pair(0, 2));
    return r;
  }
  int rr = 0;
  for (int i = 0; i < v.size() - 1; i++) {
    if (v[i] != v[i + 1]) {
      rr = i;
      break;
    }
  }
  vector<int> z;
  for (int i = 0; i < rr; i++)
    z.push_back(v[i]);
  for (int i = rr + 2; i < v.size(); i++)
    z.push_back(v[i]);
  vector<pii> now = calc(z);
  vector<pii> ans;
  for (int i = 0; i < rr; i++)
    ans.push_back(now[i]);
  pii s = now[(rr + now.size() - 1) % now.size()], t = now[rr % now.size()];
  pii a = make_pair((s.first + t.first) / 2, (s.second + t.second) / 2), b = a;
  if (s.first == t.first) {
    if ((s.second < t.second) == (v[rr] == 1))
      b.first--, t.first--;
    else
      b.first++, t.first++;
  } else {
    if ((s.first < t.first) == (v[rr] == 1))
      b.second++, t.second++;
    else
      b.second--, t.second--;
  }
  ans.push_back(a);
  ans.push_back(b);
  for (int i = rr; i < now.size(); i++)
    ans.push_back(now[i]);
  ans[(rr + 2) % ans.size()] = t;
  // for (int i = 0; i < v.size(); i++)printf("%d ", v[i]); printf("\\n");
  // for (int i = 0; i < ans.size(); i++)printf("%d %d   ", ans[i].first,
  // ans[i].second); printf("\\n");
  return comp(ans);
}
int main() {
  int num;
  scanf("%d", &num);
  vector<int> v;
  int s = 0;
  for (int i = 0; i < num; i++) {
    int z;
    scanf("%d", &z);
    v.push_back((180 - z) / 90);
    s += v[i];
  }
  if (s != 4) {
    printf("-1\\n");
    return 0;
  }
  vector<pii> r = calc(v);
  for (int i = 0; i < num; i++)
    printf("%d %d\\n", r[i].first, r[i].second);
}

Error: WA

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
vector<pii> comp(vector<pii> v) {
  vector<int> za, zb;
  for (int i = 0; i < v.size(); i++)
    za.push_back(v[i].first), zb.push_back(v[i].second);
  sort(za.begin(), za.end());
  sort(zb.begin(), zb.end());
  for (int i = 0; i < v.size(); i++) {
    v[i].first =
        (lower_bound(za.begin(), za.end(), v[i].first) - za.begin()) * 2;
    v[i].second =
        (lower_bound(zb.begin(), zb.end(), v[i].second) - zb.begin()) * 2;
  }
  return v;
}
vector<pii> calc(vector<int> v) {
  if (v.size() == 4) {
    vector<pii> r;
    r.push_back(make_pair(0, 0));
    r.push_back(make_pair(2, 0));
    r.push_back(make_pair(2, 2));
    r.push_back(make_pair(0, 2));
    return r;
  }
  int rr = 0;
  for (int i = 0; i < v.size() - 1; i++) {
    if (v[i] != v[i + 1]) {
      rr = i;
      break;
    }
  }
  vector<int> z;
  for (int i = 0; i < rr; i++)
    z.push_back(v[i]);
  for (int i = rr + 2; i < v.size(); i++)
    z.push_back(v[i]);
  vector<pii> now = calc(z);
  vector<pii> ans;
  for (int i = 0; i < rr; i++)
    ans.push_back(now[i]);
  pii s = now[(rr + now.size() - 1) % now.size()], t = now[rr % now.size()];
  pii a = make_pair((s.first + t.first) / 2, (s.second + t.second) / 2), b = a;
  if (s.first == t.first) {
    if ((s.second < t.second) == (v[rr] == 1))
      b.first--, t.first--;
    else
      b.first++, t.first++;
  } else {
    if ((s.first < t.first) == (v[rr] == 1))
      b.second++, t.second++;
    else
      b.second--, t.second--;
  }
  ans.push_back(a);
  ans.push_back(b);
  for (int i = rr; i < now.size(); i++)
    ans.push_back(now[i]);
  ans[(rr + 2) % ans.size()] = t;
  // for (int i = 0; i < v.size(); i++)printf("%d ", v[i]); printf("\\n");
  // for (int i = 0; i < ans.size(); i++)printf("%d %d   ", ans[i].first,
  // ans[i].second); printf("\\n");
  return comp(ans);
}
int main() {
  int num;
  scanf("%d", &num);
  vector<int> v;
  int s = 0;
  for (int i = 0; i < num; i++) {
    int z;
    scanf("%d", &z);
    v.push_back((180 - z) / 90);
    s += v[i];
  }
  if (s != 4)
    printf("-1\\n");
  vector<pii> r = calc(v);
  for (int i = 0; i < num; i++)
    printf("%d %d\\n", r[i].first, r[i].second);
}