CF 809D Hitchhiking in the Baltic States——splay+dp
题目:http://codeforces.com/contest/809/problem/D
如果值是固定的,新加入一个值,可以让第一个值大于它的那个长度的值等于它。
如今值是一段区间,就对区间内的dp值都有影响;中间的那些的值变成了上一个的值+1,左边 l 处多了一个点,右边第一个大于等于 r 的点被删掉。
用 splay 维护这些点;点的个数就是最多能达到的长度。
又好好学习了一番 splay 。带有标记的原来是要在那个 splay 操作前那样一条链地 pushdown下来呀。
非常奇怪的是按题解写法,找第一个小于边界的值,再找右边界那个值的 nxt ,就没问题;而自己找第一个大于等于的值,就会TLE。那个T的点是所有 l , r 都是 1,1e9 的,也许和这个有关?
加“哨兵”十分方便。
别忘了 insert 也要 splay 。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=3e5+;
int n,c[N][],tg[N],val[N],fa[N],ans,rt,tot;
inline int rdn()
{
int ret=;bool fx=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')fx=;ch=getchar();}
while(ch>=''&&ch<='') ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return fx?ret:-ret;
}
inline void rotate(int x,int &k)
{
int y=fa[x],z=fa[y];
if(y==k) k=x;
else c[z][y==c[z][]]=x;
int d=(x==c[y][]);
fa[x]=z; fa[c[x][!d]]=y; fa[y]=x;
c[y][d]=c[x][!d]; c[x][!d]=y;
}
inline void down(int cr)
{
if(!tg[cr])return;
int ls=c[cr][],rs=c[cr][];
if(ls) tg[ls]+=tg[cr],val[ls]+=tg[cr];
if(rs) tg[rs]+=tg[cr],val[rs]+=tg[cr];
tg[cr]=;
}
inline void push(int cr)
{
if(fa[cr]) push(fa[cr]);
down(cr);
}
inline void splay(int x,int &k)
{
push(x);
while(x!=k)
{
int y=fa[x],z=fa[y];
if(y!=k)
{
if((x==c[y][])^(y==c[z][]))
rotate(x,k);
else rotate(y,k);
}
rotate(x,k);
}
}
inline int lower(int x)
{
int cr=rt,ret=;
while(cr)
{
down(cr);
if(val[cr]<x)
ret=cr,cr=c[cr][];
else cr=c[cr][]; // if(val[cr]>=x)
// ret=cr,cr=c[cr][0];
// else cr=c[cr][1];
}
return ret;
}
inline void del(int cr)
{
splay(cr,rt);
int pr=c[cr][],nt=c[cr][];
while(c[pr][])pr=c[pr][];
while(c[nt][])nt=c[nt][];
splay(pr,rt); splay(nt,c[rt][]);
fa[cr]=; c[nt][]=;
}
inline void insert(int &cr,int p,int pr)//&
{
if(!cr){fa[cr=++tot]=pr;val[cr]=p;splay(cr,rt);return;}//
down(cr);
insert(c[cr][val[cr]<p],p,cr);
}
int nxt(int x)
{
splay(x,rt);//
int cr=c[x][];
while(c[cr][])cr=c[cr][];
return cr;
}
inline void solve(int l,int r)
{
int x=lower(l),y=lower(r),t=nxt(y);
if(x!=y)
{
splay(x,rt); splay(t,c[rt][]);
// val[x]++;
tg[c[t][]]++; val[c[t][]]++;
} if(t!=) del(t),ans--;
// if(y!=2) del(y),ans--;
insert(rt,l,); ans++;
}
int main()
{
n=rdn();
val[]=-1e9-; val[]=1e9+;
fa[]=; c[][]=; tot=; rt=;
for(int i=,l,r;i<=n;i++)
{
l=rdn(); r=rdn();
solve(l,r);
}
printf("%d\n",ans);
return ;
}
CF 809D Hitchhiking in the Baltic States——splay+dp的更多相关文章
- 【CF809D】Hitchhiking in the Baltic States Splay
[CF809D]Hitchhiking in the Baltic States 题意:给你n个区间[li,ri],让你选出从中一个子序列,然后在子序列的每个区间里都选择一个tj,满足$t_1< ...
- CF 809 D Hitchhiking in the Baltic States —— 思路+DP(LIS)+splay优化
题目:http://codeforces.com/contest/809/problem/D 看题解,抄标程...发现自己连 splay 都快不会写了... 首先,题目就是要得到一个 LIS: 但与一 ...
- Codeforces 809D. Hitchhiking in the Baltic States
Description 给出 \(n\) 个数 \(a_i\),每一个数有一个取值 \([l_i,r_i]\) ,你来确定每一个数,使得 \(LIS\) 最大 题面 Solution 按照平时做法,设 ...
- CodeForces 809D Hitchhiking in the Baltic States(FHQ-Treap)
题意 给你长度为$n$的序列,序列中的每个元素$i$有一个区间限制$[l_i,r_i]$,你从中选出一个子序列,并给它们标号$x_i$,要求满足 $,∀i<j,x_i<x_j$,且$, ∀ ...
- 【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
CF809D Hitchhiking in the Baltic States CF809D 长度为n的序列{xi},n<=3e5,范围在(li,ri)之间,求LIS最长是多长g(i,l)表示前 ...
- CF809D Hitchhiking in the Baltic States LIS、平衡树
传送门 看到最长上升子序列肯定是DP 设\(f_i\)表示计算到当前,长度为\(i\)的最长上升子序列的最后一项的最小值,显然\(f_i\)是一个单调递增的序列. 转移:对于当前计算的元素\(x\), ...
- 【CF809D】Hitchhiking in the Baltic States
题意: 给你n个区间[li,ri],让你选出从中一个子序列,然后在子序列的每个区间里都选择一个tj,满足t1<t2<...<tlent1<t2<...<tlen.最 ...
- BZOJ 3173: [Tjoi2013]最长上升子序列 [splay DP]
3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1613 Solved: 839[Submit][St ...
随机推荐
- poj 1651 Multiplication Puzzle【区间DP】
题目链接:http://poj.org/problem? id=1651 题意:初使ans=0,每次消去一个值,位置在pos(pos!=1 && pos !=n) 同一时候ans+=a ...
- JavaScript 工厂模式和订阅模式
设计模式的好处: 代码规范 // 例如表单验证,两个 input ,一个用户名,一个密码 // 通常做法是 function checkUser(){ //..... } function check ...
- beifen---http://vdisk.weibo.com/s/uhCtnyUhD0Ooc
- OpenKM安装(CentOS6)
OpenKM全称是Open Knowledge Management,是一个DMS(文档管理系统).本文介绍如何在CentOS下安装它.本文的安装程序和资料全部来自OpenKM官网:http://ww ...
- Android 一键清理动画
版本号:1.0 日期:2014.7.29 2014.7.30 版权:© 2014 kince 转载注明出处 一键清理是非常多Launcher都会带有的功能,其效果也比較美观.实现方式或许有非常多 ...
- 【iOS开发】---- UIView动画
iOS 动画UIView动画 原文:http://www.cocoachina.com/bbs/read.php?tid=110168 1.概述 UIKit直接将动画集成到UIView类中,实现简 ...
- [Usaco2005 Dec]Cleaning Shifts 清理牛棚
题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now ...
- Java面试-Hibernate总结
1 Hibernate的检索方式 Ø 导航对象图检索(依据已经载入的对象.导航到其它对象. ) Ø OID检索(依照对象的OID来检索对象. ) Ø HQL检索(使用面向对象的HQL查询语言. ...
- meteor ---快速启动meteor和 mongodb 方法--MAC
c:~ lsg$ cat .bash_profile c:~ lsg$ vim .bash_profile --- 修改这个文件 按i 修改文件 shift+Z+Z 保存修改内容 添加如下代码 exp ...
- Flyweight Pattern
1.Flyweight 模式以共享的方式高效的支持大量的细粒度对象,对象分为内部状态.外部状态.将可以被共享的状态作为内部状态存储在对象中,而外部状态在适当的时候作为参数传递给对象. 当以下所有的条件 ...