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.
Hard8Segment treeSortingIntervalsNo attempts yetTime limit10sMemory limit512 MBA pasture is a grid of 106 rows and 106 columns. The rows are numbered 1 through 106 from top to bottom, and the columns are numbered 1 through 106 from left to right.
n cows stand in the pasture, each on one unit square. The pasture also holds m dandelion flowers, each on one unit square, and f 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) steps to (r+1,c) or to (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.
The input has three blocks: fences, then flowers, then cows.
The first line of the first block holds the number of fences f (0≤f≤200000). Each of the next f lines holds four integers r1, c1, r2, c2 (1≤r1≤r2≤106, 1≤c1≤c2≤106) that describe one fence. (r1,c1) is the upper left square inside the fence and (r2,c2) 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 m (0≤m≤200000). The k-th of the next m lines holds two integers r and c (1≤r,c≤106), the square of the k-th flower. No two flowers share a square.
The first line of the third block holds the number of cows n (1≤n≤200000). The k-th of the next n lines holds two integers r and c (1≤r,c≤106), the square of the k-th cow. No two cows share a square, and no cow shares a square with a flower.
Print n lines. The k-th line holds the number of flowers the k-th cow can reach.
The picture shows the layout of the first example.
