Problem p03950 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
#define SIZE 524288
class segtree {
public:
  int seg[SIZE * 2];
  void init() { fill(seg, seg + SIZE * 2, -1000000000); }
  void update(int a, int b) {
    a += SIZE;
    seg[a] = max(seg[a], b);
    for (;;) {
      a /= 2;
      if (a == 0)
        break;
      seg[a] = max(seg[a * 2], seg[a * 2 + 1]);
    }
  }
  int get(int beg, int end, int node, int lb, int ub) {
    if (ub < beg || end < lb)
      return -1000000000;
    if (beg <= lb && ub <= end)
      return seg[node];
    return max(get(beg, end, node * 2, lb, (lb + ub) / 2),
               get(beg, end, node * 2 + 1, (lb + ub) / 2 + 1, ub));
  }
};
segtree t1, t2;
int get(vector<pi3> a, vector<pi3> b) {
  t1.init(), t2.init();
  vector<int> zat;
  for (int i = 0; i < a.size(); i++)
    zat.push_back(a[i].first.second);
  for (int i = 0; i < b.size(); i++)
    zat.push_back(b[i].first.second);
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(zat.begin(), zat.end());
  int pt = 0;
  int maxi = 0;
  for (int i = 0; i < b.size(); i++) {
    for (;;) {
      if (pt == a.size())
        break;
      if (a[pt].first.first > b[i].first.first)
        break;
      int low =
          lower_bound(zat.begin(), zat.end(), a[pt].first.second) - zat.begin();
      t1.update(low, a[pt].second + a[pt].first.second + a[pt].first.first);
      t2.update(low, a[pt].second - a[pt].first.second + a[pt].first.first);
      pt++;
    }
    int low =
        lower_bound(zat.begin(), zat.end(), b[i].first.second) - zat.begin();
    int m1 = b[i].second + t1.get(0, low, 1, 0, SIZE - 1) - b[i].first.second -
             b[i].first.first;
    int m2 = b[i].second + t2.get(low, SIZE - 1, 1, 0, SIZE - 1) +
             b[i].first.second - b[i].first.first;
    maxi = max(maxi, max(m1, m2));
  }
  return maxi;
}
vector<pi3> getrect(vector<pii> v, int mx, int my) {
  for (int i = 0; i < v.size(); i++)
    swap(v[i].first, v[i].second);
  v.push_back(make_pair(0, 0));
  v.push_back(make_pair(my, 0));
  sort(v.begin(), v.end());
  vector<pii> now;
  vector<pi3> ret;
  for (int i = 0; i < v.size() - 1; i++)
    ret.push_back(make_pair(make_pair(v[i].first, v[i + 1].first),
                            mx + mx + v[i + 1].first - v[i].first));
  for (int i = 0; i < v.size(); i++) {
    for (;;) {
      if (now.empty())
        break;
      pii z = now[now.size() - 1];
      if (v[i].second < z.second) {
        now.pop_back();
        if (!now.empty()) {
          pii x = now[now.size() - 1];
          ret.push_back(make_pair(make_pair(x.first, v[i].first),
                                  v[i].first - x.first + z.second + z.second));
        }
      } else
        break;
    }
    now.push_back(v[i]);
  }
  return ret;
}
int solve(vector<pii> v, int mx, int my) {
  int m = mx / 2;
  vector<pii> va, vb;
  for (int i = 0; i < v.size(); i++) {
    if (v[i].first <= m)
      va.push_back(make_pair(m - v[i].first, v[i].second));
    if (v[i].first >= m)
      vb.push_back(make_pair(v[i].first - m, v[i].second));
  }
  vector<pi3> a = getrect(va, m, my), b = getrect(vb, mx - m, my);
  // for (int i = 0; i < a.size(); i++)printf("%d %d  %d\n", a[i].first.first,
  // a[i].first.second, a[i].second); printf("---\n"); for (int i = 0; i <
  // b.size(); i++)printf("%d %d  %d\n", b[i].first.first, b[i].first.second,
  // b[i].second); printf("--------\n");
  int s = get(a, b), t = get(b, a);
  return max(s, t);
}
int main() {
  int mx, my, num;
  scanf("%d%d%d", &mx, &my, &num);
  vector<pii> v;
  for (int i = 0; i < num; i++) {
    int za, zb;
    scanf("%d%d", &za, &zb);
    v.push_back(make_pair(za, zb));
  }
  int a = solve(v, mx, my);
  for (int i = 0; i < num; i++)
    swap(v[i].first, v[i].second);
  int b = solve(v, my, mx);
  printf("%d\n", max(a, b));
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
#define SIZE 1048576
class segtree {
public:
  int seg[SIZE * 2];
  void init() { fill(seg, seg + SIZE * 2, -1000000000); }
  void update(int a, int b) {
    a += SIZE;
    seg[a] = max(seg[a], b);
    for (;;) {
      a /= 2;
      if (a == 0)
        break;
      seg[a] = max(seg[a * 2], seg[a * 2 + 1]);
    }
  }
  int get(int beg, int end, int node, int lb, int ub) {
    if (ub < beg || end < lb)
      return -1000000000;
    if (beg <= lb && ub <= end)
      return seg[node];
    return max(get(beg, end, node * 2, lb, (lb + ub) / 2),
               get(beg, end, node * 2 + 1, (lb + ub) / 2 + 1, ub));
  }
};
segtree t1, t2;
int get(vector<pi3> a, vector<pi3> b) {
  t1.init(), t2.init();
  vector<int> zat;
  for (int i = 0; i < a.size(); i++)
    zat.push_back(a[i].first.second);
  for (int i = 0; i < b.size(); i++)
    zat.push_back(b[i].first.second);
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(zat.begin(), zat.end());
  int pt = 0;
  int maxi = 0;
  for (int i = 0; i < b.size(); i++) {
    for (;;) {
      if (pt == a.size())
        break;
      if (a[pt].first.first > b[i].first.first)
        break;
      int low =
          lower_bound(zat.begin(), zat.end(), a[pt].first.second) - zat.begin();
      t1.update(low, a[pt].second + a[pt].first.second + a[pt].first.first);
      t2.update(low, a[pt].second - a[pt].first.second + a[pt].first.first);
      pt++;
    }
    int low =
        lower_bound(zat.begin(), zat.end(), b[i].first.second) - zat.begin();
    int m1 = b[i].second + t1.get(0, low, 1, 0, SIZE - 1) - b[i].first.second -
             b[i].first.first;
    int m2 = b[i].second + t2.get(low, SIZE - 1, 1, 0, SIZE - 1) +
             b[i].first.second - b[i].first.first;
    maxi = max(maxi, max(m1, m2));
  }
  return maxi;
}
vector<pi3> getrect(vector<pii> v, int mx, int my) {
  for (int i = 0; i < v.size(); i++)
    swap(v[i].first, v[i].second);
  v.push_back(make_pair(0, 0));
  v.push_back(make_pair(my, 0));
  sort(v.begin(), v.end());
  vector<pii> now;
  vector<pi3> ret;
  for (int i = 0; i < v.size() - 1; i++)
    ret.push_back(make_pair(make_pair(v[i].first, v[i + 1].first),
                            mx + mx + v[i + 1].first - v[i].first));
  for (int i = 0; i < v.size(); i++) {
    for (;;) {
      if (now.empty())
        break;
      pii z = now[now.size() - 1];
      if (v[i].second < z.second) {
        now.pop_back();
        if (!now.empty()) {
          pii x = now[now.size() - 1];
          ret.push_back(make_pair(make_pair(x.first, v[i].first),
                                  v[i].first - x.first + z.second + z.second));
        }
      } else
        break;
    }
    now.push_back(v[i]);
  }
  return ret;
}
int solve(vector<pii> v, int mx, int my) {
  int m = mx / 2;
  vector<pii> va, vb;
  for (int i = 0; i < v.size(); i++) {
    if (v[i].first <= m)
      va.push_back(make_pair(m - v[i].first, v[i].second));
    if (v[i].first >= m)
      vb.push_back(make_pair(v[i].first - m, v[i].second));
  }
  vector<pi3> a = getrect(va, m, my), b = getrect(vb, mx - m, my);
  // for (int i = 0; i < a.size(); i++)printf("%d %d  %d\n", a[i].first.first,
  // a[i].first.second, a[i].second); printf("---\n"); for (int i = 0; i <
  // b.size(); i++)printf("%d %d  %d\n", b[i].first.first, b[i].first.second,
  // b[i].second); printf("--------\n");
  int s = get(a, b), t = get(b, a);
  return max(s, t);
}
int main() {
  int mx, my, num;
  scanf("%d%d%d", &mx, &my, &num);
  vector<pii> v;
  for (int i = 0; i < num; i++) {
    int za, zb;
    scanf("%d%d", &za, &zb);
    v.push_back(make_pair(za, zb));
  }
  int a = solve(v, mx, my);
  for (int i = 0; i < num; i++)
    swap(v[i].first, v[i].second);
  int b = solve(v, my, mx);
  printf("%d\n", max(a, b));
}