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 qqoperations on the array. The ii-th operation is described with three integers lili, riri and xixi (1≤li≤ri≤n1≤li≤ri≤n, 1≤xi≤n1≤xi≤n) and means that you should add xixi to each of the elements with indices li,li+1,…,rili,li+1,…,ri. 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.
The first line contains two integers nn and qq (1≤n,q≤1041≤n,q≤104) — 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 lili, riri and xixi (1≤li≤ri≤n1≤li≤ri≤n, 1≤xi≤n1≤xi≤n), denoting a query of adding xixi to the segment from lili-th to riri-th elements of the array, inclusive.
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.
4 3 1 3 1 2 4 2 3 4 4
4 1 2 3 4
7 2 1 5 1 3 7 2
3 1 2 3
10 3 1 1 2 1 1 3 1 1 6
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:
题意:有一个长度为n的数组,标号为1到n,初始都为0,现在可以进行q次操作,每次操作把一个区间中的数都加上一个数,现在只做q次操作的子集,求问完成后数组中最大数可能是多少,把所有可能的都列出来。
思路:用线段树存储一段区间内可以进行的不同操作,然后利用STL库中的bitset,自顶向下搜寻,记录下区间可以达到的所有值,最后汇总。
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn = 1e4+10;
bitset ans,y;
vector tree[maxn << 2];
void update(int l,int r,int rt,int x,int y,int w)
{
if(x>=l&&y<=r)
{
tree[rt].push_back(w);
return ;
}
if(x == y)return;
int mid = (x+y)>>1;
update(l,r,rt<<1,x,mid,w);
update(l,r,(rt<<1)+1,mid+1,y,w);
}
void dfs(int rt,bitsety,int len)
{
bitsetz=y;
for(int i=tree[rt].size()-1;i>=0;i--)
{
z|=(z<>1));
dfs((rt<<1)+1,z,len>>1);
}
int main()
{
int n,q;
while(~scanf("%d%d",&n,&q))
{
for(int i=0;i<=(n<<2);i++)tree[i].clear();
ans = 0;
y = 0;
y[0]=1;
int l,r,w;
for(int i=0;i