Visual Python++

Time limit5sMemory limit512 MB

Summary
Match n top-left corners to n bottom-right corners so the rectangles form properly nested or disjoint blocks, or report a syntax error.
Level

Hard8 of 10

Topics
Sorting, Stack, Greedy, Implementation
Solved
No attempts yet

Problem

In the Visual Python++ programming language, a block of statements is a rectangle of characters. Its top-left corner is in row r1r_1 and column c1c_1, and its bottom-right corner is in row r2r_2 and column c2c_2. Every character at a position (r,c)(r, c) with r1≤r≤r2r_1 \le r \le r_2 and c1≤c≤c2c_1 \le c \le c_2 belongs to that block. Among these positions, the ones with r=r1r = r_1, r=r2r = r_2, c=c1c = c_1, or c=c2c = c_2 form the border of the block.

Blocks nest to any depth. In a syntactically correct program, two blocks are either nested (one contained in the other) or disjoint (they share no position), and in both cases their borders must not overlap. So when a block BB sits inside a block AA, the two satisfy r1A<r1B≤r2B<r2Ar_1^A < r_1^B \le r_2^B < r_2^A and c1A<c1B≤c2B<c2Ac_1^A < c_1^B \le c_2^B < c_2^A, and two blocks that are not nested share no position at all.

A programmer does not draw the rectangles. Drawing them takes too long, so a programmer writes one character p at the top-left corner of a block and one character y at its bottom-right corner. The parser then matches the corners and recovers the nesting structure of the program.

Write the part of the parser that performs this matching.

Input

The first line contains an integer nn (1≤n≤1051 \le n \le 10^5), the number of corner pairs.

Each of the next nn lines contains two integers rr and cc (1≤r,c≤1091 \le r, c \le 10^9), a top-left corner in row rr and column cc. The next nn lines give the bottom-right corners in the same format. All 2n2n corner positions are distinct.

Output

If the corners can be matched so that the nn blocks form a syntactically correct program, print nn lines. Line ii contains the number jj of the bottom-right corner matched with the ii-th top-left corner. Top-left corners and bottom-right corners are numbered from 1 to nn, each group in the order they appear in the input. At most one matching produces a correct program, so the answer is unique.

If no matching produces a correct program, print syntax error on a single line.

Examples4

  1. Example 1

    Input
    2
    4 7
    9 8
    14 17
    19 18
    
    Expected output
    2
    1
    
  2. Example 2

    Input
    2
    4 7
    14 17
    9 8
    19 18
    
    Expected output
    1
    2
    
  3. Example 3

    Input
    2
    4 8
    9 7
    14 18
    19 17
    
    Expected output
    syntax error
    
  4. Example 4

    Input
    3
    1 1
    4 8
    8 4
    10 6
    6 10
    10 10
    
    Expected output
    syntax error