optimum cpp solution for : Connect the Dots time limit per test2 seconds memory limit…
- User
optimum cpp solution for : Connect the Dots time limit per test2 seconds memory limit per test512 megabytes One fine evening, Alice sat down to play the classic game "Connect the Dots", but with a twist.
To play the game, Alice draws a straight line and marks n points on it, indexed from 1 to n . Initially, there are no arcs between the points, so they are all disjoint. After that, Alice performs m operations of the following type:
She picks three integers ai , di (1≤di≤10 ), and ki . She selects points ai,ai+di,ai+2di,ai+3di,…,ai+ki⋅di and connects each pair of these points with arcs. After performing all m operations, she wants to know the number of connected components† these points form. Please help her find this number.
† Two points are said to be in one connected component if there is a path between them via several (possibly zero) arcs and other points.
Input Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤105 ). The description of the test cases follows.
The first line of each test case contains two integers n and m (1≤n≤2⋅105 , 1≤m≤2⋅105 ).
The i -th of the following m lines contains three integers ai , di , and ki (1≤ai≤ai+ki⋅di≤n , 1≤di≤10 , 0≤ki≤n ).
It is guaranteed that both the sum of n and the sum of m over all test cases do not exceed 2⋅105 .
Output For each test case, output the number of connected components.
Example InputCopy 3 10 2 1 2 4 2 2 4 100 1 19 2 4 100 3 1 2 5 7 2 6 17 2 31 OutputCopy 2 96 61 Note In the first test case, there are n=10 points. The first operation joins the points 1 , 3 , 5 , 7 , and 9 . The second operation joins the points 2 , 4 , 6 , 8 , and 10 . There are thus two connected components: {1,3,5,7,9} and {2,4,6,8,10} .
In the second test case, there are n=100 points. The only operation joins the points 19 , 21 , 23 , 25 , and 27 . Now all of them form a single connected component of size 5 . The other 95 points form single-point connected components. Thus, the answer is 1+95=96 .
In the third test case, there are n=100 points. After the operations, all odd points from 1 to 79 will be in one connected component of size 40 . The other 60 points form single-point connected components. Thus, the answer is 1+60=61 .
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.