ZeptoLab Code Rush 2015
F
Pudding Monsters
Codeforces
3000
原题
← EG →
翻译中…

In this problem you will meet the simplified model of game Pudding Monsters.

翻译中…

An important process in developing any game is creating levels. A game field in Pudding Monsters is an $*n* × *n*$ rectangular grid, $*n*$ of its cells contain monsters and some other cells contain game objects. The gameplay is about moving the monsters around the field. When two monsters are touching each other, they glue together into a single big one (as they are from pudding, remember?).

翻译中…

image

翻译中…

Statistics showed that the most interesting maps appear if initially each row and each column contains exactly one monster and the rest of map specifics is set up by the correct positioning of the other game objects.

翻译中…

A technique that's widely used to make the development process more efficient is reusing the available resources. For example, if there is a large $*n* × *n*$ map, you can choose in it a smaller $*k* × *k*$ square part, containing exactly $*k*$ monsters and suggest it as a simplified version of the original map.

翻译中…

You wonder how many ways there are to choose in the initial map a $*k* × *k*$ ($1 ≤ *k* ≤ *n*$) square fragment, containing exactly $*k*$ pudding monsters. Calculate this number.

翻译中…

Input

翻译中…

The first line contains a single integer $*n*$ ($1 ≤ *n* ≤ 3 × 10^{5}$) — the size of the initial field.

翻译中…

Next $*n*$ lines contain the coordinates of the cells initially containing monsters. The $*i*$-th of the next lines contains two numbers $*r*_{*i*}, *c*_{*i*}$ ($1 ≤ *r*_{*i*}, *c*_{*i*} ≤ *n*$) — the row number and the column number of the cell that initially contains the $*i*$-th monster.

翻译中…

It is guaranteed that all $*r*_{*i*}$ are distinct numbers and all $*c*_{*i*}$ are distinct numbers.

翻译中…

Output

翻译中…

Print the number of distinct square fragments of the original field that can form a new map.

翻译中…

样例

Input #1
5
1 1
4 3
3 2
2 4
5 5
Output #1
10
InputCopy OutputCopy
题目信息
题号
526F
时间限制
2 s
内存限制
256 MB
标签
数据结构
分治

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