E - Rectangle Concatenation Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800800

問題文

正整数 h,wh,w に対し, 縦の辺の長さが hh, 横の辺の長さが ww であるような長方形を (h,w)(h,w) と表すことにします. なお, 本問では長方形を回転する操作は考えず, hwh\neq w のとき長方形 (h,w)(h,w) と長方形 (w,h)(w,h) は異なるものとして扱います.

長方形の列 ((h1,w1),(h2,w2),,(hn,wn))((h_1,w_1),(h_2,w_2),\dots ,(h_n,w_n))長方形生成列 であるとは, 次の手順が成功するような方法が存在することを言います.

  • 長方形 XX(h1,w1)(h_1,w_1) とする. 以下では, 各時点での長方形 XX の縦の辺の長さと横の辺の長さをそれぞれ H,WH,W と表す.
  • i=2,3,,ni=2,3,\dots ,n の順に次のいずれか一方の操作を行う. いずれも行うことができないとき手順は失敗とし, 手順を終了する.
    • XX の縦の辺の長さが hih_i に等しいとき, XX に長方形 (hi,wi)(h_i,w_i) を横向きに結合する. 正確には, その時点で H=hiH=h_i のとき XX を長方形 (H,W+wi)(H,W+w_i) に置き換える.
    • XX の横の辺の長さが wiw_i に等しいとき, XX に長方形 (hi,wi)(h_i,w_i) を縦向きに結合する. 正確には, その時点で W=wiW=w_i のとき XX を長方形 (H+hi,W)(H+h_i,W) に置き換える.
  • 上記の一連の操作が失敗しなかった場合は手順は成功とし, 手順を終了する.

NN 個の長方形が与えられます. ii 番目の長方形は, 縦の辺の長さが HiH_i, 横の辺の長さが WiW_i の長方形です.

1lrN1\le l\le r\le N を満たす正整数の組 (l,r)(l,r) であって次の条件を満たすものの個数を求めてください.

  • 長方形の列 ((Hl,Wl),(Hl+1,Wl+1),,(Hr,Wr))((H_l,W_l),(H_{l+1},W_{l+1}),\dots ,(H_r,W_r)) が長方形生成列である.

制約

  • 1N3×1051\leq N\leq 3\times 10^5
  • 1Hi,Wi1061 \leq H_i,W_i \leq 10^6
  • 入力される値はすべて整数.

入力

入力は以下の形式で標準入力から与えられる.

NN
H1H_1 W1W_1
H2H_2 W2W_2
\vdots
HNH_N WNW_N

出力

答えを出力せよ.


入力例 1Copy

Copy
4
1 2
1 3
2 3
3 1

出力例 1Copy

Copy
7

条件を満たす (l,r)(l,r)(1,1),(1,2),(2,2),(2,3),(2,4),(3,3),(4,4)(1,1),(1,2),(2,2),(2,3),(2,4),(3,3),(4,4)77 つです.

例えば, (l,r)=(2,4)(l,r)=(2,4) については, 結合を縦向き \to 横向きの順に行うと手順が成功します.


入力例 2Copy

Copy
5
2 1
2 1
1 2
3 2
1 4

出力例 2Copy

Copy
10

入力例 3Copy

Copy
1
1000000 1000000

出力例 3Copy

Copy
1

入力例 4Copy

Copy
10
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1

出力例 4Copy

Copy
55

Score : 800800 points

Problem Statement

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

A sequence of rectangles ((h1,w1),(h2,w2),,(hn,wn))((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 XX be (h1,w1)(h_1,w_1). Hereafter, let HH and WW respectively denote the height and width of the rectangle XX at each step.
  • For i=2,3,,ni=2,3,\dots ,n, perform one of the following operations. If neither can be performed, the procedure unsuccessfully terminates.
    • If the height of XX is equal to hih_i, attach the rectangle (hi,wi)(h_i,w_i) horizontally to XX. Formally, if H=hiH=h_i at that time, replace XX with the rectangle (H,W+wi)(H,W+w_i).
    • If the width of XX is equal to wiw_i, attach the rectangle (hi,wi)(h_i,w_i) vertically to XX. Formally, if W=wiW=w_i at that time, replace XX with the rectangle (H+hi,W)(H+h_i,W).
  • If the above series of operations does not fail, the procedure successfully terminates.

You are given NN rectangles. The ii-th rectangle has a height of HiH_i and a width of WiW_i.

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

  • The sequence of rectangles ((Hl,Wl),(Hl+1,Wl+1),,(Hr,Wr))((H_l,W_l),(H_{l+1},W_{l+1}),\dots ,(H_r,W_r)) is a rectangle generation sequence.

Constraints

  • 1N3×1051 \leq N \leq 3 \times 10^5
  • 1Hi,Wi1061 \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:

NN
H1H_1 W1W_1
H2H_2 W2W_2
\vdots
HNH_N WNW_N

Output

Print the answer.


Sample Input 1Copy

Copy
4
1 2
1 3
2 3
3 1

Sample Output 1Copy

Copy
7

The pairs (l,r)(l,r) that satisfy the condition are (1,1),(1,2),(2,2),(2,3),(2,4),(3,3),(4,4)(1,1),(1,2),(2,2),(2,3),(2,4),(3,3),(4,4); there are seven.

For example, for (l,r)=(2,4)(l,r)=(2,4), the procedure succeeds if the first attachment is done vertically and the second is done horizontally.


Sample Input 2Copy

Copy
5
2 1
2 1
1 2
3 2
1 4

Sample Output 2Copy

Copy
10

Sample Input 3Copy

Copy
1
1000000 1000000

Sample Output 3Copy

Copy
1

Sample Input 4Copy

Copy
10
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1

Sample Output 4Copy

Copy
55


2025-04-05 (Sat)
16:57:44 +00:00