Under construction

Still being built. Things may change

Ant Colony

Interview

Time limit1sMemory limit1024 MB

Summary
Given a tree of unit tunnels on a grid, count the connected components left inside each query rectangle when the outside points are removed.
Level

Medium5 of 10

Topics
Prefix sum, Sorting, Implementation
Solved
No attempts yet

Statement

The Integer Forest holds a large colony of tree ants. The Integer Forest is an infinite two-dimensional plane with a tree at every point that has integer coordinates. The colony consists of a number of integer points, and some pairs of adjacent points are connected by tunnels. Two points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) are adjacent if ∣x1−x2∣+∣y1−y2∣=1|x_1-x_2|+|y_1-y_2| = 1, so the colony forms a graph. Tree ants are special because, besides living in trees, their colony is itself a tree: it is connected and has exactly N−1N-1 tunnels, where NN is the number of points in the colony.

The researcher Anthony wants to run an experiment on the colony. He wants to know what happens when a rectangular area is cut off from the rest of the colony. His theory is that ants in different components of the graph form their own colonies and start wars against the others. A rectangle is the set of all integer points (x,y)(x, y) with x1≤x≤x2x_1 \leq x \leq x_2 and y1≤y≤y2y_1 \leq y \leq y_2, where x1,y1,x2,y2x_1, y_1, x_2, y_2 are given integers.

Anthony has not yet decided which rectangle to use, but he has several ideas. For each rectangle, determine how many components of the graph form inside the rectangle when it is cut off. Two points belong to the same component if one can reach the other through tunnels without visiting a point outside the rectangle. Anthony only cares about the components inside the cut-off rectangle and ignores the rest of the colony.

Input

The first line contains two integers AA and BB (1≤A,B≤150 0001 \leq A, B \leq 150\,000), the size of the area the colony lives in.

The second line contains two integers NN and QQ (2≤N≤150 0002 \leq N \leq 150\,000, 1≤Q≤150 0001 \leq Q \leq 150\,000), the number of points in the colony and the number of rectangles.

The next N−1N-1 lines describe the tunnels. Each tunnel has one of two types:

  1. h xx yy: points (x,y)(x, y) and (x+1,y)(x+1, y) are connected by a tunnel (1≤x≤A−11 \leq x \leq A-1, 1≤y≤B1 \leq y \leq B).
  2. v xx yy: points (x,y)(x, y) and (x,y+1)(x, y+1) are connected by a tunnel (1≤x≤A1 \leq x \leq A, 1≤y≤B−11 \leq y \leq B-1).

The next QQ lines describe the rectangles. Each line contains four integers x1x_1, y1y_1, x2x_2, and y2y_2 (1≤x1≤x2≤A1 \leq x_1 \leq x_2 \leq A, 1≤y1≤y2≤B1 \leq y_1 \leq y_2 \leq B). The rectangle consists of all points (x,y)(x, y) with x1≤x≤x2x_1 \leq x \leq x_2 and y1≤y≤y2y_1 \leq y \leq y_2.

All coordinates (x,y)(x, y) of the colony satisfy 1≤x≤A1 \leq x \leq A and 1≤y≤B1 \leq y \leq B. It is guaranteed that the graph formed by the points and tunnels is a tree.

Output

Print QQ lines, one for each rectangle, containing the number of components that form inside that rectangle when it is cut off.

Constraints

  • 1≤A,B≤150 0001 \leq A, B \leq 150\,000
  • 2≤N≤150 0002 \leq N \leq 150\,000
  • 1≤Q≤150 0001 \leq Q \leq 150\,000

Examples1

  1. Example 1

    Input
    4 3
    8 4
    v 1 1
    h 1 1
    h 2 1
    v 2 1
    v 2 2
    h 1 3
    h 3 1
    1 1 4 3
    3 2 4 3
    3 1 3 1
    1 2 3 3
    
    Expected output
    1
    0
    1
    2