Under construction

Still being built. Things may change

Fortress Destruction

Time limit1sMemory limit512 MB

Summary
Given N nested blocks forming a fortress and Q missile strikes, output how many blocks each strike destroys from the top of its column, in firing order.
Level

Medium7 of 10

Topics
Segment tree, Tree, Implementation
Solved
No attempts yet

Problem

NN blocks are stacked to build a fortress. Each block occupies the interval from column LL to column RR on floor HH, and no two blocks overlap. Every block above floor 1 has at least one block directly below it. A block stacked on top lies entirely within the interval of the block below it. That is, for any block jj stacked on block ii, Li≤Lj≤Rj≤RiL_i \le L_j \le R_j \le R_i holds.

To destroy this fortress, KK missile strikes are fired. Each strike fires a missile with power PP at position XX, and it destroys up to PP blocks from the top among the blocks at column XX of the fortress. If a block underneath is removed during this process, the block above it falls down without damage.

The following figure shows a missile fired at X=5X=5 with P=1P=1.

For each strike, in the order they were fired, find the number of blocks destroyed.

Input

The first line contains the number of blocks NN. (1≤N≤100,0001 \le N \le 100{,}000)

The next NN lines each contain the information of a block that makes up the fortress: HH, LL, and RR. (1≤H≤N1 \le H \le N, 1≤L≤R≤1,000,000,0001 \le L \le R \le 1{,}000{,}000{,}000)

The next line contains the number of strikes QQ. (1≤Q≤100,0001 \le Q \le 100{,}000)

The next QQ lines each contain the position XX and power PP of a strike. (1≤X≤1,000,000,0001 \le X \le 1{,}000{,}000{,}000, 1≤P≤N1 \le P \le N)

Output

For each strike, in the order they were fired, print the number of blocks destroyed.

Examples1

  1. Example 1

    Input
    7
    4 10 11
    3 9 11
    3 7 8
    2 7 11
    1 1 12
    3 3 4
    2 2 5
    4
    5 1
    11 2
    10 3
    9 4
    
    Expected output
    1 2 2 0