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})$居然能过 ...
随机推荐
- [hdu 6069]素数筛+区间质因数分解
给[L,R]区间的每一个数都质因数分解的复杂度可以达到(R-L)logR,真的涨姿势…… 另外,质因数分解有很重要的一点,就是只需要打sqrt(R)以内的素数表就够了……因为超过sqrt(R)的至多只 ...
- import pymongo exceptions.ImportError: No module named pymongo
最近用Scrapy写爬虫,将爬取的数据存入Mongodb中,使用的是pymongo这个库,但是运行的时候报错如标题所示 搜了好多网站包括stackoverflow都没有解决,后来发现自己用的是虚拟环境 ...
- ssh中的相对路径与绝对路径的问题
一:前言:自己在学习ssh的时候常常被路径给迷惑,就比如在刚刚学习jsp的servlet时,绝对路径和相对路径我就弄混了,所以专门写了一篇博客来记载.而现在自己是在学ssh的时候在此遇到路径问题,本来 ...
- 报错!!!!!!!!!!!org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSessionRepositoryFilter' is defined
报错!!!!!!!!!!! 因用maven项目不是很熟练,经常在Maven转Web项目(为什么要转web项目?因为要在tomcat中跑起来.maven项目好像是可以直接部署到tomcat的,或集成to ...
- hdu3294 manacher算法
这道题哇 其实是裸的manacher 无论怎么变 是回文的就是回文 所以 特殊处理一下输出就好了 不过最后的左右端点l,r.l=(p-p[pos]+2)/2-1,r=(p+p[pos]-2)/2-1; ...
- [POJ2068]Nim解题报告
Let's play a traditional game Nim. You and I are seated across a table and we have a hundred stones ...
- bzoj 1876 高精
首先我们知道,对于两个数a,b,他们的gcd情况有如下形式的讨论 当a为奇数,b为偶数的时候gcd(a,b)=gcd(a div 2,b) 当b为奇数,a为偶数的时候gcd(a,b)=gcd(a,b ...
- bzoj 1588 裸平衡树
//By BLADEVIL #include <cstdio> #include <set> #define inf 1<<30 using namespace s ...
- Objective-C中ORM的运用:实体对象和字典的相互自动转换
http://blog.csdn.net/cooldragon/article/details/18991973 iOS开发中基于ORM的框架很多,如SQLitePersistentObject,实际 ...
- Oracle存储过程学习笔记
SQL是一种语言! SQL是一种语言! SQL是一种语言! 个人理解:存储过程就相当于Java中的方法;声明变量区域就相当于java中的声明局部变量一样,只是放到一个指定区域定义了 一.先看一部分基础 ...