PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#CC0000076. Mô tả bức tranh pha màu (Describe the Painting)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

2 lộ trình

Mô tả bức tranh pha màu (Describe the Painting)

Describe the Painting

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

A painting lies along a number line. Segment ii colors the half-open interval [li,ri)[l_i,r_i) with a unique color code cic_i. Overlapping colors form a set; its displayed value is the sum of color codes in that set. Describe the painting by the minimum number of consecutive painted segments on which the set of colors does not change. Equal sums alone do not imply equal color sets.

Input

The first line contains nn, followed by nn lines of li,ri,cil_i,r_i,c_i. This is a stdin/stdout adaptation of a LeetCode function problem.

Output

Print the number kk of resulting segments, then kk lines in increasing left-coordinate order, each containing left,right,sumleft,right,sum for a painted half-open segment.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le 20; 1≤li<ri≤1051\le l_i<r_i\le10^5; 1≤ci≤1091\le c_i\le10^9; all cic_i are distinct.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤li<ri≤1051\le l_i<r_i\le10^5; 1≤ci≤1091\le c_i\le10^9; all cic_i are distinct.
  • Subtask 3 (50 points): 1≤n≤200001\le n\le 20000; 1≤li<ri≤1051\le l_i<r_i\le10^5; 1≤ci≤1091\le c_i\le10^9; all cic_i are distinct.

Examples

Input

4
1 4 5
1 4 7
4 7 1
4 7 11

Output

2
1 4 12
4 7 12

Explanation

On [1,4)[1,4) the active colors are 5,75,7 (sum 1212). On [4,7)[4,7) the colors are 1,111,11 (also sum 1212). The color sets differ, so the two pieces must remain separate.