线段树区间合并优化dp——cf1197E(好)
线段树优化dp的常见套路题,就是先按某个参数排序,然后按这个下标建立线段树,再去优化dp
本题由于要维护两个数据:最小值和对应的方案数,所以用线段树区间合并
/*
dp[i]表示第i个套娃作为最内层的最小浪费空间
dp[i]=min(dp[j])+out[i]-in[i];
那么按照out排序后按下标建立线段树,节点维护的是二元组(区间最小值,这个最小值对应的方案数)
求dp[i]时二分找到out[j]<=in[i]的区间[1,pos],然后线段树里查询再更新 处理方案数,线段树向上合并时,只保留值最小的那个节点信息即可,如果值相同,那么将方案数合并即可
初始值:如果一个娃外面套任何东西,那么这个娃的方案数设为1即*/ #include<bits/stdc++.h>
using namespace std;
#define N 200005
#define ll long long
#define mod 1000000007 inline ll f(ll a,ll b){
ll res=a+b;
if(res>mod)res-=mod;
return res;
} struct Node{ll out,in;}a[N];
bool operator<(Node a,Node b){return a.in<b.in;}
ll n,dp[N],x[N]; #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
struct Seg {
ll val,num;
Seg(){}
Seg(ll val,ll num):val(val),num(num){}
}seg[N<<];
Seg merge(Seg A,Seg B){
if(A.val<B.val)return A;
if(A.val>B.val)return B;
Seg res;
res.val=A.val;
res.num=(A.num+B.num)%mod;
return res;
}
void build(int l,int r,int rt){
seg[rt].num=;seg[rt].val=0x3f3f3f3f3f3f3f3f;
if(l==r)return;
int m=l+r>>;
build(lson);build(rson);
}
void update(int pos,ll val,ll num,int l,int r,int rt){
if(l==r){seg[rt].num=num;seg[rt].val=val;return;}
int m=l+r>>;
if(pos<=m)update(pos,val,num,lson);
else update(pos,val,num,rson);
seg[rt]=merge(seg[rt<<],seg[rt<<|]);
}
Seg query(int L,int R,int l,int r,int rt){
if(L<=l && R>=r)return seg[rt];
int m=l+r>>;
Seg res;
res.val=0x3f3f3f3f3f3f3f3f;
if(L<=m)res=merge(res,query(L,R,lson));
if(R>m)res=merge(res,query(L,R,rson));
return res;
} void debug(int l,int r,int rt){
cout<<l<<" "<<r<<" "<<seg[rt].num<<" "<<seg[rt].val<<"\n";
if(l==r)return;
int m=l+r>>;
debug(lson);debug(rson);
} int main(){
cin>>n;
for(int i=;i<=n;i++){
cin>>a[i].out>>a[i].in;
x[i]=a[i].in;
}
sort(a+,a++n);sort(x+,x++n); build(,n,);
for(int i=n;i>=;i--){
int pos=lower_bound(x+,x++n,a[i].out)-x;
if(pos>n){//外面套不了娃
update(i,a[i].in,,,n,);
}
else {
Seg res=query(pos,n,,n,);
//cout<<res.val<<" "<<res.num<<'\n';
update(i,res.val-(a[i].out-a[i].in),res.num,,n,);
//cout<<res.val<<" "<<res.num<<'\n';
}
//debug(1,n,1);
}
Seg res=query(,n,,n,);
cout<<res.num;
}
线段树区间合并优化dp——cf1197E(好)的更多相关文章
- 【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)
D. Slalom time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- 【BZOJ3638】Cf172 k-Maximum Subsequence Sum 线段树区间合并(模拟费用流)
[BZOJ3638]Cf172 k-Maximum Subsequence Sum Description 给一列数,要求支持操作: 1.修改某个数的值 2.读入l,r,k,询问在[l,r]内选不相交 ...
- 【bzoj3638】Cf172 k-Maximum Subsequence Sum 模拟费用流+线段树区间合并
题目描述 给一列数,要求支持操作: 1.修改某个数的值 2.读入l,r,k,询问在[l,r]内选不相交的不超过k个子段,最大的和是多少. 输入 The first line contains inte ...
- POJ 3667 Hotel(线段树 区间合并)
Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...
- HDU 3911 线段树区间合并、异或取反操作
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...
- HDU 3911 Black And White(线段树区间合并+lazy操作)
开始以为是水题,结果...... 给你一些只有两种颜色的石头,0为白色,1为黑色. 然后两个操作: 1 l r 将[ l , r ]内的颜色取反 0 l r 计算[ l , r ]内最长连续黑色石头的 ...
- HYSBZ 1858 线段树 区间合并
//Accepted 14560 KB 1532 ms //线段树 区间合并 /* 0 a b 把[a, b]区间内的所有数全变成0 1 a b 把[a, b]区间内的所有数全变成1 2 a b 把[ ...
- poj3667 线段树 区间合并
//Accepted 3728 KB 1079 ms //线段树 区间合并 #include <cstdio> #include <cstring> #include < ...
- hdu3911 线段树 区间合并
//Accepted 3911 750MS 9872K //线段树 区间合并 #include <cstdio> #include <cstring> #include < ...
随机推荐
- centos7.5部署ELk
第1章 环境规划 1.1 ELK介绍 ELK是ElasticSerach.Logstash.Kibana三款产品名称的首字母集合,用于日志的搜集和搜索. Elasticsearc ...
- 【MySQL】mysql查询强制大小写及替换字段
强制大小写 select * from test where name like BINARY '%Adc%' mysql替换字段 update test set name= REPLACE (nam ...
- Java奇葩笔试题
1.下面代码中,在if处填写什么代码,可以使得输出结果为:AB 1 2 3 4 5 6 7 8 9 public static void main(String[] args) { if ( ){// ...
- Vuetify按需加载配置
自己配置vuetify按需加载的步骤,在此记录: 执行npm install vuetify –save 或 yarn add vuetify添加vuetify添加依赖执行npm install -- ...
- Rsync 实现服务器文件的同步——服务端的安装配置
一.安装rsync 直接使用yum命令进行安装即可. yum -y install rsync 二.配置文件 网上大多教程都说安装是默认没有配置文件的,但是经过我的尝试,yum安装下默认是有配置文件的 ...
- C++ STL rope 可持久化平衡树 (可持久化数组)
官方文档好像 GG 了. rope 不属于标准 STL,属于扩展 STL,来自 pb_ds 库 (Policy-Based Data Structures). 基本操作: #include <e ...
- 现在就去100offer 参加互联网人才拍卖! 现在登录现在注册 为什么整个互联网行业都缺前端工程师?
现在,几乎整个互联网行业都缺前端工程师,不仅在刚起步的创业公司,上市公司乃至巨头,这个问题也一样存在.没错,优秀的前端工程师简直比大熊猫还稀少. 每天,100offer的HR群都有人在吐槽招不到前端工 ...
- 好947 Mybatis 配置resultMap 带參数查询Map 注意selectOne数据库返回结果一条数据库 否则会报错
//TMD 写几个demo 还有大站採集 <a target=_blank href="http://hao947.com/" target="_blank&quo ...
- 在命令行中插入TAB键
参考man bash: quoted-insert (C-q, C-v) Add the next character typed to the line verbatim. This is how ...
- 使用JMail发送邮件
使用JMail做最简单的文本邮件发送: 第一步.下载JMail和JAF 第二步.解压放到本地classpath中 第三步.使用: public class MailService{ privat ...