CF981E.Addition on Segments

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 (1≤li≤ri≤n1 \leq l_i \leq r_i \leq n, 1≤xi≤n1 \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.

格里沙参加了一场竞赛,遇到了如下问题:

给你一个长度为 nn 的数组,初始时所有元素均为 00。数组元素的下标从 11 到 nn 编号。你将对数组执行 qq 次操作。第 ii 次操作由三个整数 lil_i、rir_i 和 xix_i(满足 1≤li≤ri≤n1 \leq l_i \leq r_i \leq n,1≤xi≤n1 \leq x_i \leq n)描述,其含义是:将下标在区间 [li,li+1,…,ri][l_i, l_i + 1, \ldots, r_i] 内的每个元素均加上 xix_i。在完成所有操作后,你需要找出数组中的最大值。

格里沙很聪明,因此他很快解决了这个问题。

然而,他大脑中却出了点问题,现在他开始思考这样一个问题:“假设我们只对数组应用操作集合的一个子集(即任意选取一部分操作执行),那么数组最大值可能取哪些值?”

请帮助格里沙,找出所有满足 1≤y≤n1 \leq y \leq n 的整数 yy,使得存在某个操作子集(可以为空),当仅执行该子集中的操作时,数组的最大值恰好等于 yy。

输入格式

The first line contains two integers nn and qq (1≤n,q≤1041 \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 (1≤li≤ri≤n1 \leq l_i \leq r_i \leq n, 1≤xi≤n1 \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.

第一行包含两个整数 nn 和 qq(1≤n,q≤1041 \leq n, q \leq 10^{4})—— 分别表示数组的长度以及初始问题中的查询数量。

接下来的 qq 行每行包含一个查询。其中第 ii 行包含三个整数 lil_i、rir_i 和 xix_i(1≤li≤ri≤n1 \leq l_i \leq r_i \leq n,1≤xi≤n1 \leq x_i \leq n),表示一个将 xix_i 加到数组中从第 lil_i 个元素至第 rir_i 个元素(含端点)所构成区间的查询。

输出格式

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.

第一行输出唯一整数 kk,表示在对数组应用给定操作的某个子集(该子集可以为空)后,数组最大值可能等于 11 到 nn(含端点)中多少个整数。

下一行按升序输出这 kk 个整数——即所有可能的最大值。

输入输出样例

  • 输入#1

    4 3
    1 3 1
    2 4 2
    3 4 4

    输出#1

    4
    1 2 3 4
  • 输入#2

    7 2
    1 5 1
    3 7 2

    输出#2

    3
    1 2 3
  • 输入#3

    10 3
    1 1 2
    1 1 3
    1 1 6

    输出#3

    6
    2 3 5 6 8 9

说明/提示

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).

考虑第一个样例。如果只考虑第一个查询的子集,则最大值为 11;如果只考虑第二个查询,则最大值为 22;如果考虑前两个查询,则最大值变为 33;如果只考虑第四个查询,则最大值变为 44;如果考虑第四个查询及其它任意额外查询,则最大值将超过 nn,因此你不应输出该结果。

在第二个样例中,你可以仅选取第一个查询以得到 11;仅选取第二个查询以得到 22;选取全部查询以得到 33。

在第三个样例中,你可以得到如下最大值:

  • 通过查询 (1)(1) 可达到最大值 22;
  • 通过查询 (2)(2) 可达到最大值 33;
  • 通过查询 (1,2)(1, 2) 可达到最大值 55;
  • 通过查询 (3)(3) 可达到最大值 66;
  • 通过查询 (1,3)(1, 3) 可达到最大值 88;
  • 通过查询 (2,3)(2, 3) 可达到最大值 99。

输入解题思路,AI测评打分。不知道怎么写?

首页