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 ...
随机推荐
- 赵雅智_android_frame动画
在開始实例解说之前,先引用官方文档中的一段话: Frame动画是一系列图片依照一定的顺序展示的过程,和放电影的机制非常相似.我们称为逐帧动画.Frame动画能够被定义在XML文件里,也能够全然编码实现 ...
- vue2.0 自定义 弹窗(MessageBox)组件
组件模板 src/components/MessageBox/index.vue <!-- 自定义 MessageBox 组件 --> <template> <div c ...
- 获取css信息
一般情况是用style直接获取css信息但是style只能获取到卸载行内的样式外链的和嵌入的样式会获取不到 2.5 用下面方法获取外链和嵌入的css样式 这种方法只能用于读取 window.getCo ...
- 无法获取html元素宽高度的问题
今天遇到了xxx.style.width无法获取元素宽度的问题,原来一直没有注意到这个小细节: 1)如果width:120px:是写在样式表里面的,获取宽度或者高度的方法是: xxx.offsetWi ...
- 【分布式计算】DFS && BigTable
1.背景 分布式计算的发迹应该是google在2003年发表的三篇paper.各自是GFS.MapReduce.BigTable. 当中MapReduce大家都非常熟悉了.不懂的同学也能够看看我之前写 ...
- 手写AngularJS脏检查机制
什么是脏检查 View -> Model 浏览器提供有User Event触发事件的API,例如,click,change等 Model -> View 浏览器没有数据监测API. Ang ...
- MySQL windows集群(转)
http://blog.csdn.net/zhangking/article/details/5670070 MySQL 群集是 MySQL 适合于分布式计算环境的高可用.高冗余版本.它采用了 ...
- FALSE_IT
本文讲一个实用的语法糖(suger),很不错,攻克了我实际工作中的问题. 如果你写了这样一个类: class Executor { int step1(); void step2(); int ste ...
- qt-mingw530-opencv-开发配置
1.安装好Qt和Qtcreator 2.解压OpenCV源码到一个目录下.路径不能带空格和中文. 3.把E:\Qt\qtcreator-2.1.0\mingw\bin添加到系统环境变量中. 4.安装C ...
- Linux随笔记
Linux配置apt-get源地址 以Ubuntu配置网易开源镜像站为例: 访问地址:http://mirrors.163.com/,找到对应的系统. 先将source.list进行备份,执行: su ...