CodeForces - 981E Addition on Segments
考虑每个点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 (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.
Input
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.
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
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
#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\n",num);
for(int i=1;i<=n;i++) if(ans[i]) printf("%d ",i);
puts("");
return 0;
}
CodeForces - 981E Addition on Segments的更多相关文章
- Codeforces 981 E - Addition on Segments
E - Addition on Segments 思路: dp dp[i]表示构成i的区间的右端点 先将询问按r排序 然后,对于每次询问,每次枚举 i 从 n-x 到 1,如果dp[i] >= ...
- Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力
Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...
- Codeforces 193 D. Two Segments
http://codeforces.com/contest/193/problem/D 题意: 给一个1~n的排列,在这个排列中选出两段区间,求使选出的元素排序后构成公差为1的等差数列的方案数. 换个 ...
- Codeforces 1108E2 Array and Segments (Hard version)(差分+思维)
题目链接:Array and Segments (Hard version) 题意:给定一个长度为n的序列,m个区间,从m个区间内选择一些区间内的数都减一,使得整个序列的最大值减最小值最大. 题解:利 ...
- Codeforces 895.B XK Segments
B. XK Segments time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 【CodeForces】899 E. Segments Removal
[题目]E. Segments Removal [题意]给定n个数字,每次操作删除最长的连续相同数字(等长删最左),求全部删完的最少次数.n<=2*10^6,1<=ai<=10^9. ...
- 『ACM C++』 Codeforces | 1066A - Points in Segments
大一生活真 特么 ”丰富多彩“ ,多彩到我要忙到哭泣,身为班长,很多班级的事情需要管理,也是,什么东西都得体验学一学,从学生会主席.团委团总支.社团社长都体验过一番了,现在差个班长也没试过,就来体验了 ...
- codeforces 652D D. Nested Segments(离散化+sort+树状数组)
题目链接: D. Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Codeforces 620F Xors on Segments(暴力+DP)
题目链接 Xors on Segments 预处理出$x[i]$ $=$ $1$ $xor$ $2$ $xor$ $3$ $xor$ $……$ $xor$ $i$ 话说这题$O(n^{2})$居然能过 ...
随机推荐
- BZOJ 4777 Usaco2017 Open Switch Grass Kruskal+替罪羊树+权值线段树
这道题首先可以看出答案一定是一条边,而且答案一定在最小生成树上,那么我们就可以在这个最小生成树上维护他与异色儿子的边最小值,所以我们就可以已通过Kruskal和一棵平衡树来解决,时间复杂度是O(n*l ...
- JavaScript的大括号的语义
Javascript中大括号"{}"有四种语义作用: 语义1. 组织复合语句,这是最常见的: view source print? 1 if( condition ) { 2 ...
- Ubuntu下安装LNMP之Mysql的安装及卸载
Mysql的安装过程也可参考:http://blog.csdn.net/qq_20565303/article/details/69813868 Mysql安装包下载地址:https://dev.my ...
- PAT团体程序设计大赛---(模拟)
L1-1 古风排版(20 分) 中国的古人写文字,是从右向左竖向排版的.本题就请你编写程序,把一段文字按古风排版. 输入格式: 输入在第一行给出一个正整数N(<100),是每一列的字符数.第二行 ...
- 使用UMeditor富文本编辑器上传图片
注:本文系作者原创,但可随意转载. 最近写自己的网站玩儿,写到博客的部分,打算使用UMeditor,因为之前也用过(但是好像没实现图片上传的功能),感觉用起来还比较简单. 不过还是折腾了一下午...遇 ...
- ZOJ3872 Beauty of Array---规律 | DP| 数学能力
传送门ZOJ 3872 Beauty of Array Time Limit: 2 Seconds Memory Limit: 65536 KB Edward has an array A ...
- 【UVALive4685-Succession】树形DP
http://acm.hust.edu.cn/vjudge/problem/14338 题意:给定一棵树,每个点有一个值,让你选择k个点,并且这k个点是连在一起的(从任意一个点出发,可以遍历完所有选择 ...
- szoj461【四校联考0430】挑战
传送门:(涉及版权忽略) [题解] 我们发现n的范围很小,提示我们可以折半,然后我们就会了O(T2^(n/2)*n)的做法,然而会T. 考虑如何优化.直接排序会多一个log(2^(n/2))也就是n, ...
- JavaScript DOM编程艺术 读书笔记
2. JavaScript语法 2.1 注释 HTML允许使用"<!--"注释跨越多个行,但JavaScript要求这种注释的每行都必须在开头加上"< ...
- TensorFlow 官方文档中文版【转】
转自:http://wiki.jikexueyuan.com/project/tensorflow-zh/ TensorFlow 官方文档中文版 你正在阅读的项目可能会比 Android 系统更加深远 ...