AtCoder Regular Contest 179
E
Rectangle Concatenation
AtCoder
⌀ 2909
原题
← DF →
翻译中…

Problem Statement

翻译中…

For positive integers $h$ and $w$, let $(h,w)$ denote a rectangle with height $h$ and width $w$. In this problem, we do not consider rotating the rectangles, and the rectangles $(h,w)$ and $(w,h)$ are distinguished when $h \neq w$.

翻译中…

A sequence of rectangles $((h_1,w_1),(h_2,w_2),\dots ,(h_n,w_n))$ is called a rectangle generation sequence if there exists a method that successfully follows the steps below:

翻译中…
  • Let the rectangle $X$ be $(h_1,w_1)$. Hereafter, let $H$ and $W$ respectively denote the height and width of the rectangle $X$ at each step.
  • For $i=2,3,\dots ,n$, perform one of the following operations. If neither can be performed, the procedure unsuccessfully terminates.
  • If the height of $X$ is equal to $h_i$, attach the rectangle $(h_i,w_i)$ horizontally to $X$. Formally, if $H=h_i$ at that time, replace $X$ with the rectangle $(H,W+w_i)$.
    • If the width of $X$ is equal to $w_i$, attach the rectangle $(h_i,w_i)$ vertically to $X$. Formally, if $W=w_i$ at that time, replace $X$ with the rectangle $(H+h_i,W)$.
  • If the above series of operations does not fail, the procedure successfully terminates.
翻译中…

翻译中…

You are given $N$ rectangles. The $i$-th rectangle has a height of $H_i$ and a width of $W_i$.

翻译中…

Find the number of pairs of positive integers $(l,r)$ that satisfy $1 \le l \le r \le N$ and the following condition:

翻译中…
  • The sequence of rectangles $((H_l,W_l),(H_{l+1},W_{l+1}),\dots ,(H_r,W_r))$ is a rectangle generation sequence.
翻译中…

Constraints

翻译中…
  • $1 \leq N \leq 3 \times 10^5$
  • $1 \leq H_i, W_i \leq 10^6$
  • All input values are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

N
H_1 W_1
H_2 W_2
\vdots
H_N W_N

翻译中…

Output

翻译中…

Print the answer.

翻译中…

样例

Input #1
4
1 2
1 3
2 3
3 1
Output #1
7
The pairs $(l,r)$ that satisfy the condition are $(1,1),(1,2),(2,2),(2,3),(2,4),(3,3),(4,4)$; there are seven. For example, for $(l,r)=(2,4)$, the procedure succeeds if the first attachment is done vertically and the second is done horizontally.
Input #2
5
2 1
2 1
1 2
3 2
1 4
Output #2
10
Input #3
1
1000000 1000000
Output #3
1
Input #4
10
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
Output #4
55
题目信息
题号
arc179_e
时间限制
2 s
内存限制
1024 MB

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