Under construction

Still being built. Things may change

Infection Simulation

Time limit1.5sMemory limit1024 MB

Summary
Given N customer stay intervals, each query asks how many customers end up infected from a given patient zero when the infection resistance is X.
Level

Hard8 of 10

Topics
Intervals, Graph, Dynamic programming, Binary search
Solved
No attempts yet

Problem

EGOI cafeteria had NN customers yesterday. Customers are numbered from 11 to NN. Customer ii (1≤i≤N1 \le i \le N) entered at time LiL_i and left at time RiR_i. Today it was found that one customer came in already infected with the new strain of disease X that is currently spreading in JOI Country.

The infection resistance of disease X is an integer xx. Specifically, for each 1≤i≤N1 \le i \le N, customer ii becomes a new infected person as soon as the total time customer ii spends in the cafeteria together with at least one infected person reaches xx or more.

JOI Country enforces strict infection control, so the number of infected people must be tracked accurately. However, no one knows who was infected with disease X, and the value of xx is unknown.

Ms. Rie, the manager of the EGOI cafeteria, wants the final number of infected customers for each of QQ scenarios. In the jj-th scenario (1≤j≤Q1 \le j \le Q), customer PjP_j is the only initial infected person, and the infection resistance of disease X is XjX_j.

Given the customer and scenario information, write a program that prints the final number of infected customers for each scenario. A customer infected at exactly their leave time is counted. A customer who has become infected never stops being infected.

Input

The input is given in the following format.

N
L1   R1
L2   R2
︙
LN   RN
Q
P1   X1
P2   X2
︙
PQ   XQ

Output

Print QQ lines. On line jj (1≤j≤Q1 \le j \le Q), print the final number of infected customers in the jj-th scenario.

Constraints

  • 1≤N≤1000001 \le N \le 100000.
  • 0≤Li<Ri≤1090 \le L_i < R_i \le 10^9 (1≤i≤N1 \le i \le N).
  • 1≤Q≤1000001 \le Q \le 100000.
  • 1≤Pj≤N1 \le P_j \le N (1≤j≤Q1 \le j \le Q).
  • 1≤Xj≤1091 \le X_j \le 10^9 (1≤j≤Q1 \le j \le Q).
  • All input values are integers.

Examples5

  1. Example 1

    Input
    4
    10 40
    20 80
    45 60
    70 95
    1
    1 15
    
    Expected output
    3
    
  2. Example 2

    Input
    8
    0 30
    0 90
    0 80
    0 60
    0 20
    0 40
    0 70
    0 50
    3
    1 30
    1 40
    4 50
    
    Expected output
    7
    1
    5
    
  3. Example 3

    Input
    5
    0 10
    0 10
    0 10
    0 10
    0 10
    4
    1 9
    1 10
    1 11
    1 1000000000
    
    Expected output
    5
    5
    1
    1
    
  4. Example 4

    Input
    7
    38 61
    13 27
    10 54
    22 56
    49 75
    27 47
    70 99
    1
    3 10
    
    Expected output
    6
    
  5. Example 5

    Input
    10
    10 20
    11 21
    13 23
    16 26
    20 30
    25 35
    31 41
    38 48
    46 56
    80 90
    4
    1 3
    1 6
    1 8
    1 10
    
    Expected output
    8
    5
    3
    1