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

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)((v).size())
#define fs first
#define sd second

typedef long long ll;

typedef pair<int, int> pi;
typedef pair<pi, int> ppi;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef vector<ppi> vppi;

void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}

void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}

#ifdef LOCAL
#define debug(...)                                                             \\
  debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

double get_time() { return 1.0 * clock() / CLOCKS_PER_SEC; }

const int MAX = 2E5 + 100;

void upd(vector<int> &f, int pos, int del) {
  int siz = f.size();
  for (; pos < siz; pos |= pos + 1)
    f[pos] += del;
}

int get(vector<int> &f, int pos) {
  int ret = 0;
  for (; pos >= 0; pos = (pos & (pos + 1)) - 1)
    ret += f[pos];

  return ret;
}

int main() {
  int n;
  cin >> n;
  vector<int> x(n), y(n), res(n);
  vector<pair<int, int>> points;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
    points.push_back({x[i], y[i]});
  }

  sort(points.begin(), points.end());
  // cout << '\\n';
  // for(auto pp : points) {
  // 	cout << pp.first << ' ' << pp.second << '\\n';
  // }

  // vector<int> f1(n + 1), f2(n + 1);
  // for(int i = 1; i <= n; i++) {
  // 	upd(f2, i, 1);
  // }

  // for(int i = 0; i < n; i++) {
  // 	int a = points[i].first, b = points[i].second;
  // 	res[i] = get(f1, b) + (get(f2, n) - get(f2, b - 1));
  // 	upd(f1, b, 1);
  // 	upd(f2, b, -1);
  // }

  // for(int i = 0; i < n; i++) {
  // 	cout << res[x[i] - 1] << '\\n';
  // }

  set<int> s;
  for (int i = 1; i <= n; i++)
    s.insert(i);

  for (int i = 0; i < n; i++) {
    int j = i;
    int mn = points[i].second;
    s.erase(points[i].second);

    while (j + 1 < n && *s.rbegin() > mn) {
      j++;
      mn = min(mn, points[j].second);
      s.erase(points[j].second);
    }

    for (int k = i; k <= j; k++)
      res[points[k].first - 1] = j - i + 1;

    i = j;
  }

  for (int i = 0; i < n; i++)
    cout << res[x[i] - 1] << '\\n';
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)((v).size())
#define fs first
#define sd second

typedef long long ll;

typedef pair<int, int> pi;
typedef pair<pi, int> ppi;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef vector<ppi> vppi;

void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}

void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}

#ifdef LOCAL
#define debug(...)                                                             \\
  debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

double get_time() { return 1.0 * clock() / CLOCKS_PER_SEC; }

const int MAX = 2E5 + 100;

int upd(vector<int> &f, int pos, int del) {
  int siz = f.size();
  for (; pos < siz; pos |= pos + 1)
    f[pos] += del;
}

int get(const vector<int> &f, int pos) {
  int ret = 0;
  for (; pos >= 0; pos = (pos & (pos + 1)) - 1)
    ret += f[pos];

  return ret;
}

int main() {
  int n;
  cin >> n;
  vector<int> x(n), y(n), res(n);
  vector<pair<int, int>> points;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
    points.push_back({x[i], y[i]});
  }

  sort(points.begin(), points.end());
  // cout << '\\n';
  // for(auto pp : points) {
  // 	cout << pp.first << ' ' << pp.second << '\\n';
  // }

  vector<int> f1(n + 1), f2(n + 1);
  for (int i = 1; i <= n; i++) {
    upd(f2, i, 1);
  }

  for (int i = 0; i < n; i++) {
    int a = points[i].first, b = points[i].second;
    res[i] = get(f1, b) + (get(f2, n) - get(f2, b - 1));
    upd(f1, b, 1);
    upd(f2, b, -1);
  }

  for (int i = 0; i < n; i++) {
    cout << res[x[i] - 1] << '\\n';
  }
}