codeforces981e(线段树+bitset)

E. Addition on Segments
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 liliriri and xixi (1lirin1≤li≤ri≤n1xin1≤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.

Input

The first line contains two integers nn and qq (1n,q1041≤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 liliriri and xixi (1lirin1≤li≤ri≤n1xin1≤xi≤n), denoting a query of adding xixi to the segment from lili-th to riri-th elements of the array, inclusive.

Output

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.

Examples
input
Copy
4 3
1 3 1
2 4 2
3 4 4
output
Copy
4
1 2 3 4 
input
Copy
7 2
1 5 1
3 7 2
output
Copy
3
1 2 3 
input
Copy
10 3
1 1 2
1 1 3
1 1 6
output
Copy
6
2 3 5 6 8 9 
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).


题意:有一个长度为n的数组,标号为1到n,初始都为0,现在可以进行q次操作,每次操作把一个区间中的数都加上一个数,现在只做q次操作的子集,求问完成后数组中最大数可能是多少,把所有可能的都列出来。

思路:用线段树存储一段区间内可以进行的不同操作,然后利用STL库中的bitset,自顶向下搜寻,记录下区间可以达到的所有值,最后汇总。

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<bitset>
#include<vector>
using namespace std;
const int maxn = 1e4+10;
bitset<maxn> ans,y;
vector<int> 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,bitset<maxn>y,int len)
{
    bitset<maxn>z=y;
    for(int i=tree[rt].size()-1;i>=0;i--)
    {
        z|=(z<<tree[rt][i]);
    }
    if(len == 1)
    {
        ans |= z;
        return ;
    }
    dfs(rt<<1,z,len-(len>>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<q;i++)
        {
            scanf("%d%d%d",&l,&r,&w);
            update(l,r,1,1,n,w);
        }
        dfs(1,y,n);
        int cnt=0;
        for(int i=1;i<=n;i++)
        {
            if(ans[i])cnt++;
        }
        printf("%d\n",cnt);
        for(int i=1;i<=n;i++)
        {
            if(ans[i])
            {
                printf("%d%c",i,--cnt==0?'\n':' ');
            }
        }
    }
}








猜你喜欢

转载自blog.csdn.net/qq_25576697/article/details/80887540