This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
int nr, N, M, Q, h[200009], B[200009], realV[200009], aib[200009], dp[200009];
pair < int, int > v[200009], nrm[200009];
void Update (int pos, int val)
{
    while (pos <= M)
        aib[pos] = max (aib[pos], val),
        pos += pos - (pos & (pos - 1));
}
int Query (int pos)
{
    int ans = 0;
    while (pos)
        ans = max (ans, aib[pos]),
        pos &= pos - 1;
    return ans;
}
int main ()
{
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);
scanf ("%d %d", &N, &Q);
for (int i=1; i<=N; i++)
    scanf ("%d %d", &v[i].first, &v[i].second), v[i].second = -v[i].second;
sort (v + 1, v + N + 1);
for (int i=1; i<=N; i++)
    v[i].second = -v[i].second, nrm[i] = {v[i].second, i};
sort (nrm + 1, nrm + N + 1), M = 0;
for (int i=1; i<=N; i++)
{
    if (i == 1 || nrm[i].first != nrm[i - 1].first)
        realV[++M] = nrm[i].first;
    B[nrm[i].second] = M;
}
for (int i=N; i>=1; i--)
    dp[i] = Query (B[i]) + 1,
    Update (B[i], dp[i]);
while (Q --)
{
    int X, Y, ans = 0;
    scanf ("%d %d", &X, &Y);
    for (int i=1; i<=N; i++)
        if (v[i].first >= X && v[i].second <= Y)
            ans = max (ans, dp[i]);
    printf ("%d\n", ans);
}
return 0;
}
Compilation message (stderr)
matryoshka.cpp: In function 'int main()':
matryoshka.cpp:29:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 scanf ("%d %d", &N, &Q);
 ~~~~~~^~~~~~~~~~~~~~~~~
matryoshka.cpp:31:47: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%d %d", &v[i].first, &v[i].second), v[i].second = -v[i].second;
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
matryoshka.cpp:48:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%d %d", &X, &Y);
     ~~~~~~^~~~~~~~~~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |