Codeforces Round 238 (Div. 1)
E
Hamming Triples
Codeforces
2800
原题
← D→
翻译中…

Little Chris is having a nightmare. Even in dreams all he thinks about is math.

翻译中…

Chris dreams about $*m*$ binary strings of length $*n*$, indexed with numbers from 1 to $*m*$. The most horrifying part is that the bits of each string are ordered in either ascending or descending order. For example, Chris could be dreaming about the following 4 strings of length 5:

翻译中…

image

翻译中…

The Hamming distance $*H*(*a*, *b*)$ between two strings $*a*$ and $*b*$ of length $*n*$ is the number of positions at which the corresponding symbols are different.

翻译中…

Сhris thinks that each three strings with different indices constitute a single triple. Chris's delusion is that he will wake up only if he counts the number of such string triples $*a*$, $*b*$, $*c*$ that the sum $*H*(*a*, *b*) + *H*(*b*, *c*) + *H*(*c*, *a*)$ is maximal among all the string triples constructed from the dreamed strings.

翻译中…

Help Chris wake up from this nightmare!

翻译中…

Input

翻译中…

The first line of input contains two space-separated integers $*n*$ and $*m*$ ($1 ≤ *n* ≤ 10^{9}$; $3 ≤ *m* ≤ 10^{5}$), the length and the number of strings. The next $*m*$ lines contain the description of the strings. The $*i*$-th line contains two space-separated integers $*s*_{*i*}$ and $*f*_{*i*}$ ($0 ≤ *s*_{*i*} ≤ 1$; $1 ≤ *f*_{*i*} ≤ *n*$), the description of the string with index $*i*$; that means that the first $*f*_{*i*}$ bits of the $*i*$-th string are equal to $*s*_{*i*}$, and the remaining $*n* - *f*_{*i*}$ bits are equal to $1 - *s*_{*i*}$. There can be multiple equal strings in Chris's dream.

翻译中…

Output

翻译中…

Output a single integer, the number of such string triples among the given that the sum of the Hamming distances between the strings of the triple is maximal.

翻译中…

样例

Input #1
5 4
0 3
0 5
1 4
1 5
Output #1
3
InputCopy OutputCopy
Input #2
10 4
1 5
0 5
0 5
1 5
Output #2
4
InputCopy OutputCopy
题目信息
题号
406E
时间限制
1 s
内存限制
256 MB
标签
模拟
数学
双指针

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。