zoukankan      html  css  js  c++  java
  • CodeForces

        考虑每个点i在什么情况下会成为最大值。 当选的区间子集是 包含i的区间的一个子集的时候,i肯定会是最大值。 所以我们就可以用这种方法得到所有点的可能的最大值是多少。。。

        也就是说,最后的局面可以仅由一个从左到右扫一遍时单峰的区间子集构成,这样不会丧失任意一个可行解。。。。

     

        于是就可以直接线段树打标记+bitset优化可行背包问题。。。。暴力跑一遍就能A啦。。。。

    Discription

    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 lili, riri and xixi (1lirin1≤li≤ri≤n, 1xin1≤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 lili, riri and xixi (1lirin1≤li≤ri≤n, 1xin1≤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 11to 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
    4 3
    1 3 1
    2 4 2
    3 4 4
    Output
    4
    1 2 3 4
    Input
    7 2
    1 5 1
    3 7 2
    Output
    3
    1 2 3
    Input
    10 3
    1 1 2
    1 1 3
    1 1 6
    Output
    6
    2 3 5 6 8 9
    #include<bits/stdc++.h>
    #define ll long long
    using namespace std;
    const int maxn=10005;
    #define pb push_back
    #define lc (o<<1)
    #define mid (l+r>>1)
    #define rc ((o<<1)|1)
    vector<int> tag[maxn*4];
    int n,m,le,ri,w,Q,num;
    bitset<maxn> ans,E;
    
    void update(int o,int l,int r){
    	if(l>=le&&r<=ri){ tag[o].pb(w); return;}
    	if(le<=mid) update(lc,l,mid);
    	if(ri>mid) update(rc,mid+1,r);
    }
    
    void dfs(int o,bitset<maxn> y,int len){
    	bitset<maxn> z=y;
    	for(int i=tag[o].size()-1;i>=0;i--){
    		z|=(z<<tag[o][i]);
    	}
    	
    	if(len==1) ans|=z;
    	else dfs(lc,z,len-(len>>1)),dfs(rc,z,len>>1);
    }
    
    int main(){
    	scanf("%d%d",&n,&Q);
    	
    	for(int i=1;i<=Q;i++){
    		scanf("%d%d%d",&le,&ri,&w);
    		update(1,1,n);
    	}
    	
    	E[0]=1,dfs(1,E,n);
    	
    	for(int i=1;i<=n;i++) if(ans[i]) num++;
    	printf("%d
    ",num);
    	for(int i=1;i<=n;i++) if(ans[i]) printf("%d ",i);
    	puts("");
    	return 0;
    }
    

      

  • 相关阅读:
    开源的 ASP.Net 错误记录发布模块 ELMAH (Error Logging Modules And Handlers)
    GridView 行单击与双击事件2
    iReaper
    ubuntu下android开发环境搭建(及错误异常处理)
    配置vim开发Android[神器终究是神器]
    Eclipse中文显示乱码问题
    预防鼠标手,从我做起
    ERROR:Android requires .class compatibility set to 5.0. Please fix project properties.
    Thinkpad在linux(ubuntu)下修改电池充电阈值,成功解决Thinkpad在Linux下的电池充电问题
    Eclipse启动时报错:A Java RunTime Environment (JRE) or Java Development Kit (JDK) must be available in order to run Eclipse. No java virtual machine was found after searching the following locations:…
  • 原文地址:https://www.cnblogs.com/JYYHH/p/9109191.html
Copyright © 2011-2022 走看看