electicode
АсосӣКурсҳоМанбаъҳоМасъалаҳо
Ҳамаи масъалаҳо
Khimio
Khayyam Finals 2025

Electi Lamps - KhIMIO

1 сМаҳдудияти вақт
256 МБМаҳдудияти ҳофиза
Ҳамаи ҳалҳо

Тавсифи масъала

There are NNN lamps located along a street at positions XiX_iXi​, each with a power level PiP_iPi​. The iii-th lamp lights up an area within the range when turned on. Initially, some lamps are turned on, and some are turned off. The initial state of the -th lamp is denoted by : if , the lamp is on; if , the lamp is off. The positions of the lamps are sorted in increasing order, i.e., for all .

Ҳал кардан мехоҳӣ?

Ворид шав, кодро ҳамин ҷо навис ва натиҷаро дарҳол бин.

Ворид шудан

electicode© 2026. Ҳамаи ҳуқуқҳо ҳифз шудаанд.

Намуд
[Xi−Pi,Xi+Pi][X_i - P_i, X_i + P_i]
[Xi​−Pi​,Xi​+Pi​]
iii
TiT_iTi​
Ti=1T_i = 1Ti​=1
Ti=0T_i = 0Ti​=0
Xi<Xi+1X_i < X_{i + 1}Xi​<Xi+1​
1≤i<N1 \leq i < N1≤i<N

For each lamp i (1≤i<N)i \ (1 \leq i < N)i (1≤i<N), you need to determine a value F(i)F(i)F(i), which is the minimum total distance you must walk to achieve the following:

  • All lamps before position XiX_iXi​ are considered destroyed and do not exist.
  • If the iii-th lamp was initially off, it is turned on.
  • You start at position XiX_iXi​ and want to walk to position XNX_NXN​. Along the way, you must turn off every lamp except for the one at position XNX_NXN​.

If you cannot reach position XNX_NXN​ while ensuring all lamps except lamp NNN are turned off, then F(i)=0F(i) = 0F(i)=0.

The task is to compute the sum of F(i)F(i)F(i) for all i (1≤i<N)i \ (1 \leq i < N)i (1≤i<N), modulo 998244353998244353998244353.

Note: You can only walk in areas illuminated by the lamps.

Input Format

The first line contains one integer TTT (1≤T≤1031 \leq T \leq 10^31≤T≤103), the number of test cases.

Then, for each test case:

  • The first line contains one integer NNN (1≤N≤5⋅1051 \leq N \leq 5 \cdot 10^51≤N≤5⋅105).
  • Then, NNN lines follow. In the iii-th of these lines, there are three space-separated integers XiX_iXi​, PiP_iPi​, and TiT_iTi​ (1≤Xi≤1012,1≤Pi≤1012,Ti∈{0,1})(1 \leq X_i \leq 10^{12}, 1 \leq P_i \leq 10^{12}, T_i \in \{0, 1\})(1≤Xi​≤1012,1≤P.
  • It is guaranteed that Xi<Xi+1X_i < X_{i+1}Xi​<Xi+1​ for all 1≤i<N1 \leq i < N1≤i<N.

The total sum of NNN across all test cases does not exceed 6⋅1056 \cdot 10^56⋅105.

Output Format

For each test, print one line consisting of one integer, the answer.

Scoring

Let SNS_NSN​ be the total sum of NNN across all test cases.

SubtasksAdditional restrictionsPointsRequired Subtasks
0Sample0-
1N≤2, Ti=0N \leq 2, \ T_i = 0N≤2, Ti​=02-
2N≤500, SN≤103, Ti=0N \leq 500, \ S_N \leq 10^3, \ T_i = 0N≤500, SN​≤103, Ti
3N≤5⋅103, SN≤104, Ti=0N \leq 5 \cdot 10^3, \ S_N \leq 10^4, \ T_i = 0N≤5⋅103, SN​≤10
4N≤5⋅104, SN≤105, Ti=0N \leq 5 \cdot 10^4, \ S_N \leq 10^5, \ T_i = 0N≤5⋅104, SN​≤10
5N≤2N \leq 2N≤241
6N≤500, SN≤103N \leq 500, \ S_N \leq 10^3N≤500, SN​≤10380, 2
7N≤5⋅103, SN≤104N \leq 5 \cdot 10^3, \ S_N \leq 10^4N≤5⋅103, SN​≤10412
8N≤5⋅104, SN≤105N \leq 5 \cdot 10^4, \ S_N \leq 10^5N≤5⋅104, SN​≤10514
9-250, 1, 2, 3, 4, 5, 6, 7, 8

Мисолҳо

Мисол 1
Вуруд
2 3 3 1 0 5 2 0 6 2 1 3 1 8 1 4 7 1 6 1 0
Баромад
8 0
Шарҳ
Figure 1. Initial state of Lamps.

Figure 1. Initial state of Lamps.

Now let's look at what happens when you start at the first lamp.

Figure 2. The starting lamp is turned on.

Figure 2. The starting lamp is turned on.

Figure 3. Move to lamp 2 and turn it on.

Figure 3. Move to lamp 2 and turn it on.

Figure 4. Go back to lamp 1 and turn it off.

Figure 4. Go back to lamp 1 and turn it off.

Figure 5. Go to lamp 3 and turn off lamp 2.

Figure 5. Go to lamp 3 and turn off lamp 2.

i
​
≤
1012,Ti​∈
{0,1})
​
=
0
9
-
4
,
 
Ti​
=
0
12
1, 2
5
,
 
Ti​
=
0
14
1, 2, 3
0, 1, 2, 3, 5, 6
0, 1, 2, 3, 4, 5, 6, 7