Problem p03283 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
#define rep(var, n) for (int var = 0; var < (n); ++var)
#define ALL(c) (c).begin(), (c).end()

int N, M, Q;
int l[200000], r[200000];
int p[100000], q[100000];

int ans[100000];

template <typename T> class fenwick_tree_1 { // 1-base
  vector<T> x;

public:
  fenwick_tree_1(int n) : x(n + 1, 0) {}
  void add(int k, T a = 1) {
    for (; k <= x.size(); k += k & -k)
      x[k] += a;
  }
  T sum(int i, int j) {
    if (i == 1) {
      T S = 0;
      for (; j > 0; j -= j & -j)
        S += x[j];
      return S;
    } else {
      return sum(1, j) - sum(1, i - 1);
    }
  }
};

void solve() {
  vector<ii> lines(M);
  rep(i, M) {
    lines[i] = ii(r[i], l[i]); // (Ri, Li)
  }
  sort(ALL(lines)); // 右端で昇順ソート

  map<int, vector<ii>> queries;
  rep(query_id, Q) {
    // クエリを右端ごとにまとめる (query_group)
    queries[q[query_id]].push_back(ii(p[query_id], query_id));
  }

  fenwick_tree_1<int> ft(N);

  int last_line_id = 0;
  for (auto query_group : queries) {
    int q = query_group.first;
    // vector<ii> g = query_group.second;

    while (last_line_id < M && lines[last_line_id].first <= q) {
      int l = lines[last_line_id].second;
      ft.add(l);
      ++last_line_id;
    }

    // この時点でFTに入ってるのは、r <= q なlineのみ
    // なので、p が与えられたとき [p, q] の間の個数を数えれば良い
    for (ii p_i : query_group.second) {
      int p = p_i.first, query_id = p_i.second;
      ans[query_id] = ft.sum(p, q);
    }
  }
}

int main() {
  scanf("%d %d %d", &N, &M, &Q);
  rep(i, M) { scanf("%d %d", &l[i], &r[i]); }
  rep(i, Q) { scanf("%d %d", &p[i], &q[i]); }

  solve();

  rep(i, Q) { printf("%d\n", ans[i]); }

  return 0;
}


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

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
#define rep(var, n) for (int var = 0; var < (n); ++var)
#define ALL(c) (c).begin(), (c).end()

int N, M, Q;
int l[200000], r[200000];
int p[100000], q[100000];

int ans[100000];

template <typename T> class fenwick_tree_1 { // 1-base
  vector<T> x;

public:
  fenwick_tree_1(int n) : x(n + 1, 0) {}
  void add(int k, T a = 1) {
    for (; k <= x.size(); k += k & -k)
      x[k] += a;
  }
  T sum(int i, int j) {
    if (i == 1) {
      T S = 0;
      for (; j > 0; j -= j & -j)
        S += x[j];
      return S;
    } else {
      return sum(1, j) - sum(1, i - 1);
    }
  }
};

void solve() {
  vector<ii> lines(M);
  rep(i, M) {
    lines[i] = ii(r[i], l[i]); // (Ri, Li)
  }
  sort(ALL(lines)); // 右端で昇順ソート

  map<int, vector<ii>> queries;
  rep(query_id, Q) {
    // クエリを右端ごとにまとめる (query_group)
    queries[q[query_id]].push_back(ii(p[query_id], query_id));
  }

  fenwick_tree_1<int> ft(511);

  int last_line_id = 0;
  for (auto query_group : queries) {
    int q = query_group.first;
    // vector<ii> g = query_group.second;

    while (last_line_id < M && lines[last_line_id].first <= q) {
      int l = lines[last_line_id].second;
      ft.add(l);
      ++last_line_id;
    }

    // この時点でFTに入ってるのは、r <= q なlineのみ
    // なので、p が与えられたとき [p, q] の間の個数を数えれば良い
    for (ii p_i : query_group.second) {
      int p = p_i.first, query_id = p_i.second;
      ans[query_id] = ft.sum(p, q);
    }
  }
}

int main() {
  scanf("%d %d %d", &N, &M, &Q);
  rep(i, M) { scanf("%d %d", &l[i], &r[i]); }
  rep(i, Q) { scanf("%d %d", &p[i], &q[i]); }

  solve();

  rep(i, Q) { printf("%d\n", ans[i]); }

  return 0;
}