CF BUDDY
← Problems·

981E · Addition on Segments

2200 · bitmasks, data structures, divide and conquer

Problem: Grisha come to a contest and faced the following problem.

You are given an array of size nn, initially consisting of zeros. The elements of the array are enumerated from 11 to nn. You perform qq operations on the array. The ii-th operation is described with three integers lil_i, rir_i and xix_i (1lirin1 \leq l_i \leq r_i \leq n, 1xin1 \leq x_i \leq n) and means that you should add xix_i to each of the elements with indices li,li+1,,ril_i, l_i + 1, \ldots, r_i. After all operations you should find the maximum in the array.

Grisha is clever, so he solved the problem quickly.

However something went wrong inside his head and now he thinks of the following question: "consider we applied some subset of the operations to the array. What are the possible values of the maximum in the array?"

Help Grisha, find all integers yy between 11 and nn such that if you apply some subset (possibly empty) of the operations, then the maximum in the array becomes equal to yy.

Input Format: The first line contains two integers nn and qq (1n,q1041 \leq n, q \leq 10^{4}) — the length of the array and the number of queries in the initial problem.

The following qq lines contain queries, one per line. The ii-th of these lines contains three integers lil_i, rir_i and xix_i (1lirin1 \leq l_i \leq r_i \leq n, 1xin1 \leq x_i \leq n), denoting a query of adding xix_i to the segment from lil_i-th to rir_i-th elements of the array, inclusive.

Output Format: In the first line print the only integer kk, denoting the number of integers from 11 to nn, inclusive, that can be equal to the maximum in the array after applying some subset (possibly empty) of the given operations.

In the next line print these kk integers from 11 to nn — the possible values of the maximum. Print these integers in increasing order.

Note: Consider the first example. If you consider the subset only of the first query, the maximum is equal to 11. If you take only the second query, the maximum equals to 22. If you take the first two queries, the maximum becomes 33. If you take only the fourth query, the maximum becomes 44. If you take the fourth query and something more, the maximum becomes greater that nn, so you shouldn't print it.

In the second example you can take the first query to obtain 11. You can take only the second query to obtain 22. You can take all queries to obtain 33.

In the third example you can obtain the following maximums:

  • You can achieve the maximim of 22 by using queries: (1)(1).
  • You can achieve the maximim of 33 by using queries: (2)(2).
  • You can achieve the maximim of 55 by using queries: (1,2)(1, 2).
  • You can achieve the maximim of 66 by using queries: (3)(3).
  • You can achieve the maximim of 88 by using queries: (1,3)(1, 3).
  • You can achieve the maximim of 99 by using queries: (2,3)(2, 3).

Sample Cases

Case 1

Input

4 3
1 3 1
2 4 2
3 4 4

Output

4
1 2 3 4

Case 2

Input

7 2
1 5 1
3 7 2

Output

3
1 2 3

Case 3

Input

10 3
1 1 2
1 1 3
1 1 6

Output

6
2 3 5 6 8 9

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback