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

#include <bits/stdc++.h>
#define lli long long int
using namespace std;
#define mod 1000000007
#define MOD 1000000037
#define mod1 998244353
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define INF 2147483607
#define common cout << "Case #" << w + 1 << ": "

vector<int> segv;
vector<int> segtree;

int build(int l, int r, int currpos) {
  if (l == r) {
    segtree[currpos] = segv[l];
    return segv[l];
  }
  int mid = (l + r) / 2;
  segtree[currpos] =
      min(build(l, mid, 2 * currpos + 1), build(mid + 1, r, 2 * currpos + 2));
  return segtree[currpos];
}

int getmin(int l, int r, int currpos1, int currpos2, int ind) {
  if (r < currpos1 or l > currpos2)
    return INF;
  if (l <= currpos1 and r >= currpos2)
    return segtree[ind];
  int mid = (currpos1 + currpos2) / 2;
  return min(getmin(l, r, currpos1, mid, 2 * ind + 1),
             getmin(l, r, mid + 1, currpos2, 2 * ind + 2));
}

int update(int pos, int updval, int currpos1, int currpos2, int ind) {
  if (pos < currpos1 or pos > currpos2)
    return segtree[ind];
  if (currpos1 == currpos2 and pos == currpos2) {
    segtree[ind] = updval;
    return updval;
  }
  if (currpos1 == currpos2)
    return segtree[ind];
  int mid = (currpos1 + currpos2) / 2;
  segtree[ind] = min(update(pos, updval, currpos1, mid, 2 * ind + 1),
                     update(pos, updval, mid + 1, currpos2, 2 * ind + 2));
  return segtree[ind];
}

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> v;
  set<int> s;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    v.push_back({x, y});
    s.insert(y);
  }
  map<int, int> m;

  set<int>::iterator it;
  int ind = 0;
  for (it = s.begin(); it != s.end(); it++) {
    m[*it] = ind;
    ind += 1;
  }
  map<int, multiset<int, greater<int>>> impx, impy;
  for (int i = 0; i < n; i++) {
    impy[v[i].second].insert(v[i].first);
    impx[v[i].first].insert(v[i].second);
  }
  segv.resize(ind, INF);
  int t = ceil(log2(ind));
  segtree.resize(pow(2, t + 1));
  map<int, multiset<int, greater<int>>>::iterator itr;
  for (itr = impy.begin(); itr != impy.end(); itr++) {
    int key = itr->first;
    if (itr->second.size() > 0)
      segv[m[key]] = -(*itr->second.begin() + key);
  }
  build(0, ind - 1, 0);
  // for(int i=0;i<ind;i++)
  //     cout << segv[i] << " ";
  // cout << endl;
  int ans = 0;
  for (itr = impx.begin(); itr != impx.end(); itr++) {
    int key = itr->first;
    multiset<int, greater<int>>::iterator temp = itr->second.end();
    --temp;
    int yc = *temp;
    int pos = m[yc];
    // cout << pos << endl;
    // cout << ind-1 << endl;
    int val = -getmin(pos, ind - 1, 0, ind - 1, 0);
    // cout << key << " " << val << endl;
    ans = max(ans, val - (key + yc));
    set<int> change;
    for (temp = itr->second.begin(); temp != itr->second.end(); temp++) {
      int y = *temp;
      impy[y].erase(impy[y].find(key));
      change.insert(y);
    }
    for (it = change.begin(); it != change.end(); it++) {
      int pos = m[*it];
      int val;
      if (impy[*it].size() > 0)
        val = -(*impy[*it].begin() + *it);
      else
        val = INF;
      update(pos, val, 0, ind - 1, 0);
    }
  }
  // cout << ans << endl;
  for (int i = 0; i < n; i++)
    impy[v[i].second].insert(v[i].first);
  segv.resize(ind, INF);
  t = ceil(log2(ind));
  segtree.resize(pow(2, t + 1));
  for (itr = impy.begin(); itr != impy.end(); itr++) {
    int key = itr->first;
    if (itr->second.size() > 0)
      segv[m[key]] = -(*itr->second.begin() - key);
  }
  build(0, ind - 1, 0);
  for (itr = impx.begin(); itr != impx.end(); itr++) {
    int key = itr->first;
    multiset<int, greater<int>>::iterator temp = itr->second.begin();
    int yc = *temp;
    int pos = m[yc];
    int val = -getmin(0, pos, 0, ind - 1, 0);
    ans = max(ans, val - (key - yc));
    set<int> change;
    for (temp = itr->second.begin(); temp != itr->second.end(); temp++) {
      int y = *temp;
      impy[y].erase(impy[y].find(key));
      change.insert(y);
    }
    for (it = change.begin(); it != change.end(); it++) {
      int pos = m[*it];
      int val;
      if (impy[*it].size() > 0)
        val = -(*impy[*it].begin() - *it);
      else
        val = INF;
      update(pos, val, 0, ind - 1, 0);
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define lli long long int
using namespace std;
#define mod 1000000007
#define MOD 1000000037
#define mod1 998244353
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define INF 2147483607
#define common cout << "Case #" << w + 1 << ": "

vector<int> segv;
vector<int> segtree;

int build(int l, int r, int currpos) {
  if (l == r) {
    segtree[currpos] = segv[l];
    return segv[l];
  }
  int mid = (l + r) / 2;
  segtree[currpos] =
      min(build(l, mid, 2 * currpos + 1), build(mid + 1, r, 2 * currpos + 2));
  return segtree[currpos];
}

int getmin(int l, int r, int currpos1, int currpos2, int ind) {
  if (r < currpos1 or l > currpos2)
    return INF;
  if (l <= currpos1 and r >= currpos2)
    return segtree[ind];
  int mid = (currpos1 + currpos2) / 2;
  return min(getmin(l, r, currpos1, mid, 2 * ind + 1),
             getmin(l, r, mid + 1, currpos2, 2 * ind + 2));
}

int update(int pos, int updval, int currpos1, int currpos2, int ind) {
  if (pos < currpos1 or pos > currpos2)
    return segtree[ind];
  if (currpos1 == currpos2 and pos == currpos2) {
    segtree[ind] = updval;
    return updval;
  }
  if (currpos1 == currpos2)
    return segtree[ind];
  int mid = (currpos1 + currpos2) / 2;
  segtree[ind] = min(update(pos, updval, currpos1, mid, 2 * ind + 1),
                     update(pos, updval, mid + 1, currpos2, 2 * ind + 2));
  return segtree[ind];
}

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> v;
  set<int> s;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    v.push_back({x, y});
    s.insert(y);
  }
  map<int, int> m;

  set<int>::iterator it;
  int ind = 0;
  for (it = s.begin(); it != s.end(); it++) {
    m[*it] = ind;
    ind += 1;
  }
  map<int, multiset<int, greater<int>>> impx, impy;
  for (int i = 0; i < n; i++) {
    impy[v[i].second].insert(v[i].first);
    impx[v[i].first].insert(v[i].second);
  }
  segv.resize(ind, INF);
  int t = ceil(log2(ind));
  segtree.resize(pow(2, t + 1));
  map<int, multiset<int, greater<int>>>::iterator itr;
  for (itr = impy.begin(); itr != impy.end(); itr++) {
    int key = itr->first;
    if (itr->second.size() > 0)
      segv[m[key]] = -(*itr->second.begin() + key);
  }
  build(0, ind - 1, 0);
  // for(int i=0;i<ind;i++)
  //     cout << segv[i] << " ";
  // cout << endl;
  int ans = 0;
  for (itr = impx.begin(); itr != impx.end(); itr++) {
    int key = itr->first;
    multiset<int, greater<int>>::iterator temp = itr->second.end();
    --temp;
    int yc = *temp;
    int pos = m[yc];
    // cout << pos << endl;
    // cout << ind-1 << endl;
    int val = -getmin(pos, ind - 1, 0, ind - 1, 0);
    // cout << key << " " << val << endl;
    ans = max(ans, val - (key + yc));
    set<int> change;
    for (temp = itr->second.begin(); temp != itr->second.end(); temp++) {
      int y = *temp;
      impy[y].erase(impy[y].find(key));
      change.insert(y);
    }
    for (it = change.begin(); it != change.end(); it++) {
      int pos = m[*it];
      int val;
      if (impy[*it].size() > 0)
        val = -(*impy[*it].begin() + *it);
      else
        val = INF;
      update(pos, val, 0, n - 1, 0);
    }
  }
  // cout << ans << endl;
  for (int i = 0; i < n; i++)
    impy[v[i].second].insert(v[i].first);
  segv.resize(ind, INF);
  t = ceil(log2(ind));
  segtree.resize(pow(2, t + 1));
  for (itr = impy.begin(); itr != impy.end(); itr++) {
    int key = itr->first;
    if (itr->second.size() > 0)
      segv[m[key]] = -(*itr->second.begin() - key);
  }
  build(0, ind - 1, 0);
  for (itr = impx.begin(); itr != impx.end(); itr++) {
    int key = itr->first;
    multiset<int, greater<int>>::iterator temp = itr->second.begin();
    int yc = *temp;
    int pos = m[yc];
    int val = -getmin(0, pos, 0, ind - 1, 0);
    ans = max(ans, val - (key - yc));
    set<int> change;
    for (temp = itr->second.begin(); temp != itr->second.end(); temp++) {
      int y = *temp;
      impy[y].erase(impy[y].find(key));
      change.insert(y);
    }
    for (it = change.begin(); it != change.end(); it++) {
      int pos = m[*it];
      int val;
      if (impy[*it].size() > 0)
        val = -(*impy[*it].begin() - *it);
      else
        val = INF;
      update(pos, val, 0, n - 1, 0);
    }
  }
  cout << ans << endl;
}