This page is still under construction.

Parts of this page are still being built. What you see may change.

Cow Confinement

Time limit10sMemory limit512 MB

Summary
Each cow moves only down or right across a large grid and cannot cross rectangular fences, and the task asks how many flowers each cow can reach.
Level

Hard8 of 10

Topics
Segment tree, Sorting, Intervals
Solved
No attempts yet

Problem

A pasture is a grid of 10610^6 rows and 10610^6 columns. The rows are numbered 11 through 10610^6 from top to bottom, and the columns are numbered 11 through 10610^6 from left to right.

nn cows stand in the pasture, each on one unit square. The pasture also holds mm dandelion flowers, each on one unit square, and ff fences. A fence is a rectangle whose sides run along the edges of unit squares. No two fences intersect or touch. A fence may lie entirely inside the area another fence encloses.

Because of the wind a cow moves only down or right. A cow on square (r,c)(r, c) steps to (r+1,c)(r + 1, c) or to (r,c+1)(r, c + 1). A cow walks through squares that hold other cows or flowers, but it cannot cross a fence.

For each cow, count the flowers on the squares it can reach from its own square.

Input

The input has three blocks: fences, then flowers, then cows.

The first line of the first block holds the number of fences ff (0≤f≤2000000 \le f \le 200000). Each of the next ff lines holds four integers r1r_1, c1c_1, r2r_2, c2c_2 (1≤r1≤r2≤1061 \le r_1 \le r_2 \le 10^6, 1≤c1≤c2≤1061 \le c_1 \le c_2 \le 10^6) that describe one fence. (r1,c1)(r_1, c_1) is the upper left square inside the fence and (r2,c2)(r_2, c_2) is the lower right square inside the fence. No two fences intersect or touch.

The first line of the second block holds the number of flowers mm (0≤m≤2000000 \le m \le 200000). The kk-th of the next mm lines holds two integers rr and cc (1≤r,c≤1061 \le r, c \le 10^6), the square of the kk-th flower. No two flowers share a square.

The first line of the third block holds the number of cows nn (1≤n≤2000001 \le n \le 200000). The kk-th of the next nn lines holds two integers rr and cc (1≤r,c≤1061 \le r, c \le 10^6), the square of the kk-th cow. No two cows share a square, and no cow shares a square with a flower.

Output

Print nn lines. The kk-th line holds the number of flowers the kk-th cow can reach.

Hint

The picture shows the layout of the first example.

Examples2

  1. Example 1

    Input
    4
    2 2 8 4
    1 9 4 10
    6 7 9 9
    3 3 7 3
    9
    3 4
    8 4
    11 5
    10 7
    10 8
    9 8
    2 8
    4 11
    9 11
    8
    1 1
    5 10
    6 9
    3 7
    7 1
    4 2
    7 5
    3 3
    
    Expected output
    5
    1
    0
    1
    3
    1
    3
    0
    
  2. Example 2

    Input
    0
    4
    2 3
    5 5
    7 2
    6 8
    3
    1 1
    5 4
    7 3
    
    Expected output
    4
    2
    0