Codeforces 809D. Hitchhiking in the Baltic States
Description
给出 \(n\) 个数 \(a_i\),每一个数有一个取值 \([l_i,r_i]\) ,你来确定每一个数,使得 \(LIS\) 最大
题面
Solution
按照平时做法,设 \(f[i]\) 表示 \(LIS\) 长为 \(i\) 时, \(LIS\) 结尾的最小值
考虑插入一个取值为 \([L,R]\) 可以更新的值
找到小于 \(L\) 的第一个数 \(p\) 和小于 \(R\) 的第一个数 \(q\)
那么 \(f[p+1]\) 可以更新为 \(L\) , \(i=[p+2,q]\) 都可以被更新为 \(f[i]=f[i-1]+1\)
实际上就是把数组右移,然后再区间加 \(1\)
平衡树维护一下,对应删除 \(q+1\) ,插入 \(f[p+1]=L\)
#include<bits/stdc++.h>
using namespace std;
template<class T>void gi(T &x){
int f;char c;
for(f=1,c=getchar();c<'0'||c>'9';c=getchar())if(c=='-')f=-1;
for(x=0;c<='9'&&c>='0';c=getchar())x=x*10+(c&15);x*=f;
}
const int N=3e5+10;
int n=0,ch[N][2],fa[N],rt=0,v[N],Q,la[N];
inline void mark(int x,int k){if(x)v[x]+=k,la[x]+=k;}
inline void pushdown(int x){
if(!la[x])return ;
mark(ch[x][0],la[x]);mark(ch[x][1],la[x]);la[x]=0;
}
inline void rotate(int x){
int y=fa[x];bool t=ch[y][1]==x;
ch[y][t]=ch[x][!t];
fa[ch[y][t]]=y;
ch[x][!t]=y;
fa[x]=fa[y];
if(fa[y])ch[fa[y]][ch[fa[y]][1]==y]=x;
fa[y]=x;
}
inline void Push(int x){
if(fa[x])Push(fa[x]);
pushdown(x);
}
inline void splay(int x,int goal){
Push(x);
while(fa[x]!=goal){
int y=fa[x],p=fa[y];
if(p==goal)rotate(x);
else if((ch[p][0]==y)==(ch[y][0]==x))rotate(y),rotate(x);
else rotate(x),rotate(x);
}
if(!goal)rt=x;
}
int ans=0;
inline int lower(int k){
int x=rt,ret=0;
while(x){
pushdown(x);
if(v[x]<k)ret=x,x=ch[x][1];
else x=ch[x][0];
}
return ret;
}
inline int nxt(int x){
splay(x,0);x=ch[x][1];
while(ch[x][0])x=ch[x][0];
return x;
}
inline void ins(int &x,int k,int last){
if(!x){v[x=++n]=k;fa[x]=last;splay(x,0);return ;}
pushdown(x);
ins(ch[x][k>v[x]],k,x);
}
inline void del(int x){
splay(x,0);
int p=ch[x][0],q=ch[x][1];
while(ch[p][1])p=ch[p][1];
while(ch[q][0])q=ch[q][0];
splay(p,0);splay(q,p);
ch[q][0]=fa[x]=0;
}
inline void solve(int l,int r){
int p=lower(l),q=lower(r),x=nxt(q);
if(p!=q){
splay(p,0);splay(x,p);
mark(ch[x][0],1);
}
if(x!=1)del(x),ans--;
ins(rt,l,0);ans++;
}
int main(){
freopen("pp.in","r",stdin);
freopen("pp.out","w",stdout);
cin>>Q;
v[++n]=1<<30;v[++n]=-(1<<30);fa[2]=1;ch[1][0]=2;rt=1;
for(int i=1,L,R;i<=Q;i++)gi(L),gi(R),solve(L,R);
cout<<ans<<endl;
return 0;
}
Codeforces 809D. Hitchhiking in the Baltic States的更多相关文章
- CodeForces 809D Hitchhiking in the Baltic States(FHQ-Treap)
题意 给你长度为$n$的序列,序列中的每个元素$i$有一个区间限制$[l_i,r_i]$,你从中选出一个子序列,并给它们标号$x_i$,要求满足 $,∀i<j,x_i<x_j$,且$, ∀ ...
- CF 809D Hitchhiking in the Baltic States——splay+dp
题目:http://codeforces.com/contest/809/problem/D 如果值是固定的,新加入一个值,可以让第一个值大于它的那个长度的值等于它. 如今值是一段区间,就对区间内的d ...
- 【CF809D】Hitchhiking in the Baltic States(Splay,动态规划)
[CF809D]Hitchhiking in the Baltic States(Splay,动态规划) 题面 CF 洛谷 题解 朴素\(dp\):设\(f[i][j]\)表示当前考虑到第\(i\)个 ...
- 【CF809D】Hitchhiking in the Baltic States Splay
[CF809D]Hitchhiking in the Baltic States 题意:给你n个区间[li,ri],让你选出从中一个子序列,然后在子序列的每个区间里都选择一个tj,满足$t_1< ...
- CF809D Hitchhiking in the Baltic States
CF809D Hitchhiking in the Baltic States CF809D 长度为n的序列{xi},n<=3e5,范围在(li,ri)之间,求LIS最长是多长g(i,l)表示前 ...
- CF 809 D Hitchhiking in the Baltic States —— 思路+DP(LIS)+splay优化
题目:http://codeforces.com/contest/809/problem/D 看题解,抄标程...发现自己连 splay 都快不会写了... 首先,题目就是要得到一个 LIS: 但与一 ...
- 【CF809D】Hitchhiking in the Baltic States
题意: 给你n个区间[li,ri],让你选出从中一个子序列,然后在子序列的每个区间里都选择一个tj,满足t1<t2<...<tlent1<t2<...<tlen.最 ...
- CF809D Hitchhiking in the Baltic States LIS、平衡树
传送门 看到最长上升子序列肯定是DP 设\(f_i\)表示计算到当前,长度为\(i\)的最长上升子序列的最后一项的最小值,显然\(f_i\)是一个单调递增的序列. 转移:对于当前计算的元素\(x\), ...
- E. Three States - Codeforces Round #327 (Div. 2) 590C States(广搜)
题目大意:有一个M*N的矩阵,在这个矩阵里面有三个王国,编号分别是123,想知道这三个王国连接起来最少需要再修多少路. 分析:首先求出来每个王国到所有能够到达点至少需要修建多少路,然后枚举所有点求出来 ...
随机推荐
- xmlns 与 targetNamespace 的解释
test.xsd文件: <?xml version="1.0" encoding="UTF-8"?> <xs:schema elementFo ...
- RabbitMq初探——php的一个demo
<?php /** * Created by PhpStorm. * Date: 2017/10/17 * Time: 16:21 */ class Rabbit { public functi ...
- fillna()
将下面注释掉 fillna() 函数:有一个inplace参数,默认为false,不会对原来dataframe中进行替换,为True时候会修改原来的.
- 极大似然估计MLE 极大后验概率估计MAP
https://www.cnblogs.com/sylvanas2012/p/5058065.html 写的贼好 http://www.cnblogs.com/washa/p/3222109.html ...
- DCL实现多线程安全的高性能懒汉模式
DCL实现多线程安全的高性能懒汉模式 1.单线程安全的懒汉模式实现 源码: private static LazyLoad instance = null; public static LazyLoa ...
- 【转】ListBox Dock Fill 总是有空隙的问题
源地址:https://www.cnblogs.com/norsd/p/6359291.html ListBox Dock设置了Fill, Right等 设计界面如己所愿,但是实际运行时,底部总是有不 ...
- 【线程】结果缓存实现(future与concurrenthashmap)
Computable<A,V>接口中生命了一个函数Computable,其输入类型为A,输出类型为V,在ExpensiveFunction中实现的Computable,需要很长时间来计算结 ...
- Linux系统如何迁移至LVM磁盘
今天遇到一个问题,算是比较严重的把.就是要把当前系统转移到 LVM 卷里面去,下面有一些发生过程介绍. 不感兴趣可以直接跳过,看实战部分<如何迁移系统至LVM卷> 朋友今天突然找我,说是要 ...
- [转] 以普通用户启动的Vim如何保存需要root权限的文件
[转] 以普通用户启动的Vim如何保存需要root权限的文件 在Linux上工作的朋友很可能遇到过这样一种情况,当你用Vim编辑完一个文件时,运行:wq保存退出,突然蹦出一个错误: E45: 'rea ...
- codis__数据迁移和伸缩容
数据迁移命令 注意点:是迁移到某个 redis-group 而不是某个redis-servers 实例 伸缩容用法 redis 内存等不够用时 增容 : 增加redis-group, 然后迁移使用上 ...