Problem p03283 (C++)

Buggy submission (status: Time Limit Exceeded):
// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define FOR(i, f, t) for (int i = (int)f; i < (int)t; i++)
#define FORR(i, f, t) for (int i = (int)f; i > (int)t; i--)
#define ms(obj, val) memset(obj, val, sizeof(obj))
#define pb push_back
#define SYNC ios_base::sync_with_stdio(false)
#define st(v) sort(v.begin(), v.end());
#define F first
#define S second
#define ri(x) scanf("%d", &x)
#define rii(x, y) scanf("%d %d", &x, &y)
#define riii(x, y, z) scanf("%d %d %d", &x, &y, &z)
#define lri(x) scanf("%lld", &x)
#define lrii(x, y) scanf("%lld %lld", &x, &y)
#define lriii(x, y, z) scanf("%lld %lld %lld", &x, &y, &z)

typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> ii;

const int MAXN = 2e5 + 3; // AREGLAAAAAAAAR!!!!
const int MAXLOG = 20;

ii a[MAXN];

ll BIT[MAXN];
int N;

// suma val a la posicion p, tengan en cuenta que esto no modifica A, a veces se
// quiere modificar, entonces lo tienen que hacer tambien a parte.
void BIT_upd(int p, int val) { // O(log(n))
  p++;
  for (; p <= N; p += p & -p)
    BIT[p] += val;
}

// devuelve la suma de [0,p]
ll BIT_sum(int p) { // O(log(n))
  ll ret = 0;
  p++;
  for (; p; p -= p & -p)
    ret += BIT[p];
  return ret;
}
ii qy[MAXN], qy2[MAXN];
map<ii, ll> mapa;
int main() {
  int n, q, m;
  rii(n, m);
  N = n + 1;
  ri(q);
  FOR(i, 0, m) {
    int u, v;
    rii(u, v);
    a[i] = {u, v};
  }
  sort(a, a + m);
  FOR(i, 0, m) {
    int v = a[i].S;
    BIT_upd(v, 1);
  }
  FOR(i, 0, q) {
    int u, v;
    rii(u, v);
    qy[i] = {u, v};
    qy2[i] = {u, v};
  }
  sort(qy, qy + q);
  int p = 0;
  FOR(i, 0, q) {
    int u = qy[i].F, v = qy[i].S;
    FOR(j, p, m) {
      if (a[j].F < u) {
        BIT_upd(a[j].S, -1);
      } else {
        p = j;
        break;
      }
    }
    mapa[{u, v}] = BIT_sum(v);
  }
  FOR(i, 0, q) {
    int u = qy2[i].F, v = qy2[i].S;
    printf("%lld\n", mapa[{u, v}]);
  }
}


Fixed submission:
// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define FOR(i, f, t) for (int i = (int)f; i < (int)t; i++)
#define FORR(i, f, t) for (int i = (int)f; i > (int)t; i--)
#define ms(obj, val) memset(obj, val, sizeof(obj))
#define pb push_back
#define SYNC ios_base::sync_with_stdio(false)
#define st(v) sort(v.begin(), v.end());
#define F first
#define S second
#define ri(x) scanf("%d", &x)
#define rii(x, y) scanf("%d %d", &x, &y)
#define riii(x, y, z) scanf("%d %d %d", &x, &y, &z)
#define lri(x) scanf("%lld", &x)
#define lrii(x, y) scanf("%lld %lld", &x, &y)
#define lriii(x, y, z) scanf("%lld %lld %lld", &x, &y, &z)

typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> ii;

const int MAXN = 2e5 + 3; // AREGLAAAAAAAAR!!!!
const int MAXLOG = 20;

ii a[MAXN];

ll BIT[MAXN];
int N;

// suma val a la posicion p, tengan en cuenta que esto no modifica A, a veces se
// quiere modificar, entonces lo tienen que hacer tambien a parte.
void BIT_upd(int p, int val) { // O(log(n))
  p++;
  for (; p <= N; p += p & -p)
    BIT[p] += val;
}

// devuelve la suma de [0,p]
ll BIT_sum(int p) { // O(log(n))
  ll ret = 0;
  p++;
  for (; p; p -= p & -p)
    ret += BIT[p];
  return ret;
}
ii qy[MAXN], qy2[MAXN];
map<ii, ll> mapa;
int main() {
  int n, q, m;
  rii(n, m);
  N = n + 1;
  ri(q);
  FOR(i, 0, m) {
    int u, v;
    rii(u, v);
    a[i] = {u, v};
  }
  sort(a, a + m);
  FOR(i, 0, m) {
    int v = a[i].S;
    BIT_upd(v, 1);
  }
  FOR(i, 0, q) {
    int u, v;
    rii(u, v);
    qy[i] = {u, v};
    qy2[i] = {u, v};
  }
  sort(qy, qy + q);
  int p = 0;
  FOR(i, 0, q) {
    int u = qy[i].F, v = qy[i].S;
    FOR(j, p, m) {
      if (a[j].F < u) {
        p = j + 1;
        BIT_upd(a[j].S, -1);
      } else {
        p = j;
        break;
      }
    }
    mapa[{u, v}] = BIT_sum(v);
  }
  FOR(i, 0, q) {
    int u = qy2[i].F, v = qy2[i].S;
    printf("%lld\n", mapa[{u, v}]);
  }
}