This page is still under construction.

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

Triangles and a Circle

Time limit1sMemory limit512 MB

Summary
Count triangles formed by n points on a circle of circumference L whose interior or border contains the circle's center.
Level

Medium7 of 10

Topics
Two pointers, Combinatorics, Geometry, Sorting
Solved
No attempts yet

Problem

You are given nn distinct points on a circle of length LL.

Find the number of triangles with vertices at these points that contain the center of the circle inside or on their border.

Input

The first line contains two integers nn and LL (3≤n≤300 0003 \leq n \leq 300\,000, n≤L≤109n \leq L \leq 10^9).

Pick any point on the circle and call it SS. Then any point AA on the circle can be written as xx with 0≤x<L0 \le x < L, the clockwise distance from SS to AA. We call this number the coordinate of AA.

The second line contains nn distinct integers x_1,x_2,…,x_nx\_1, x\_2, \ldots, x\_n, the coordinates of the given points (0≤x_i<L0 \leq x\_i < L).

Output

Print one integer: the number of triangles with vertices at the given points that contain the center of the circle inside or on their border.

Examples2

  1. Example 1

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

    Input
    10 10
    0 1 2 3 4 5 6 7 8 9
    
    Expected output
    60