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})$居然能过 ...
随机推荐
- 东北育才冲刺noip(day9)
这十天来呢,感觉自己进步很大,(虽然被碾压的很惨),看到了自己以前完全没见过,也没想过的算法,打开新世界的大门. 同时呢,也感觉自己太弱了,于是就注册了这个博客. 为了促进进步,在这里立下flag,我 ...
- 【BZOJ 1901】Zju2112 Dynamic Rankings &&【COGS 257】动态排名系统 树状数组套线段树
外面是树状数组,里面是动态开点线段树,对于查询我们先把有关点找出来,然后一起在线段树上行走,这样就是单个O(log2)的了 #include <cstdio> #include <v ...
- 适用于实数范围的中缀表达式的 + - * / ( ) 计算(C++实现)
核心算法: mid=FormatMid(mid); //格式化中缀表达式 JudgeLegalMid(mid); //判断中缀表达式的合法性 MidToPost mtp(mid); mtp.ToPos ...
- Sqlserver面试题
1.用一条SQL语句 查询出每门课都大于80分的学生姓名 name kecheng fenshu 张三 语文 81张三 数学 75李四 语文 ...
- ssh中的相对路径与绝对路径的问题
一:前言:自己在学习ssh的时候常常被路径给迷惑,就比如在刚刚学习jsp的servlet时,绝对路径和相对路径我就弄混了,所以专门写了一篇博客来记载.而现在自己是在学ssh的时候在此遇到路径问题,本来 ...
- Nginx各项配置的含义
#user nobody; #配置用户或者组,默认为nobody nobody worker_processes 4; #允许生成的进程数,默认为1 worker_cpu_affinity 00000 ...
- import as from import 区别
在python中import或者from…import是用来导入相应的模块.那每一种有什么具体的差别呢? 一.import 只有import,为最简单的引入对应的包.例如: import ...
- Python基础(5)_文件操作
一.文件处理流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 二.文件打开模式 打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文 ...
- saltstack入门至放弃之salt安装部署
学习了一段时间的saltstack,是时候记录下了.友提:学习环境是两台centos_7.2_x64机器 系统初始化: 两台机器执行以下脚本即可(友提:两台服务器的主机名配置在/etc/hosts中, ...
- celery后台运行
参考:https://blog.csdn.net/qq_18863573/article/details/52437689 pip install supervisor # 要用python2的pip ...