This page is still under construction.

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

Mountainous landscape

Time limit10sMemory limit256 MB

Summary
For each segment of a left-to-right polygonal chain, find the nearest later segment with a point strictly above the ray extending the segment.
Level

Hard8 of 10

Topics
Geometry, Stack
Solved
No attempts yet

Problem

You travel through a landscape made mostly of mountains. There are nn landmarks, peaks and valleys, along your path. You stop to catch your breath and wonder which mountain you are looking at on the horizon.

Stated formally: you are given a polygonal chain P1P2…PnP_1 P_2 \dots P_n in the plane, and the xx coordinates of the points are strictly increasing. For each segment PiPi+1P_i P_{i+1} of the chain, find the smallest index j>ij > i such that some point of PjPj+1P_j P_{j+1} lies strictly above the ray that starts at PiP_i and passes through Pi+1P_{i+1}.

A point Q=(x,y)Q = (x, y) lies strictly above the ray Pi→Pi+1P_i \to P_{i+1} when x≥xi+1x \ge x_{i+1} and (xi+1−xi)(y−yi)−(yi+1−yi)(x−xi)>0(x_{i+1} - x_i)(y - y_i) - (y_{i+1} - y_i)(x - x_i) > 0.

Input

The first line contains the number of test cases TT. The test cases follow one after another.

The first line of each test case contains the number of vertices of the chain, nn (2≤n≤1000002 \le n \le 100000).

Each of the next nn lines contains the integer coordinates xix_i and yiy_i of vertex PiP_i (0≤x1<x2<⋯<xn≤1090 \le x_1 < x_2 < \dots < x_n \le 10^9, 0≤yi≤1090 \le y_i \le 10^9).

Output

For each test case, print one line with n−1n-1 space separated integers. The ii-th number is the smallest index of a chain segment visible to the right of segment PiPi+1P_i P_{i+1}, or 0 when no such segment exists.

Examples4

  1. Example 1

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

    Input
    1
    2
    0 0
    1000000000 1000000000
    
    Expected output
    0
    
  3. Example 3

    Input
    1
    5
    0 0
    1 2
    2 4
    3 6
    4 8
    
    Expected output
    0 0 0 0
    
  4. Example 4

    Input
    1
    6
    0 0
    1 1
    2 4
    3 9
    4 16
    5 25
    
    Expected output
    2 3 4 5 0