Under construction

Still being built. Things may change

Intellectual Implementation

Time limit6sMemory limit512 MB

Summary
Count the triples of axis-parallel rectangles, with all coordinates distinct, where no two rectangles in a triple share any point.
Level

Hard9 of 10

Topics
Math, Segment tree, Sorting
Solved
No attempts yet

Problem

There are nn rectangles on the coordinate plane, with sides parallel to the coordinate axes. The ii-th rectangle covers all points (x,y)(x, y) with l_i≤x≤r_il\_i \le x \le r\_i and d_i≤y≤u_id\_i \le y \le u\_i.

For simplicity, for every i≠ji \neq j, we have l_i≠l_jl\_i \neq l\_j, r_i≠r_jr\_i \neq r\_j, l_i≠r_jl\_i \neq r\_j, d_i≠d_jd\_i \neq d\_j, u_i≠u_ju\_i \neq u\_j, d_i≠u_jd\_i \neq u\_j.

Count the number of triples (i,j,k)(i, j, k) with 1≤i<j<k≤n1 \le i < j < k \le n for which the ii-th, jj-th, and kk-th rectangles are pairwise disjoint (every pair of them has no common points).

Input

The first line contains a single integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5), the number of rectangles.

Each of the next nn lines contains four integers l_il\_i, r_ir\_i, d_id\_i, u_iu\_i (−109≤l_i<r_i≤109-10^9 \le l\_i < r\_i \le 10^9, −109≤d_i<u_i≤109-10^9 \le d\_i < u\_i \le 10^9), describing the ii-th rectangle.

It is guaranteed that for every i≠ji \neq j, we have l_i≠l_jl\_i \neq l\_j, r_i≠r_jr\_i \neq r\_j, l_i≠r_jl\_i \neq r\_j, d_i≠d_jd\_i \neq d\_j, u_i≠u_ju\_i \neq u\_j, d_i≠u_jd\_i \neq u\_j.

Output

Output the number of triples (i,j,k)(i, j, k) with 1≤i<j<k≤n1 \le i < j < k \le n for which the ii-th, jj-th, and kk-th rectangles are pairwise disjoint.

Examples2

  1. Example 1

    Input
    5
    1 5 1 5
    4 8 2 6
    3 7 3 7
    2 6 28 32
    42 46 42 46
    
    Expected output
    3
    
  2. Example 2

    Input
    6
    1 8 6 10
    2 5 3 12
    3 4 15 20
    0 9 2 22
    -5 22 -2 23
    -7 11 -1 17
    
    Expected output
    0