题目: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的更多相关文章

  1. 【CF809D】Hitchhiking in the Baltic States Splay

    [CF809D]Hitchhiking in the Baltic States 题意:给你n个区间[li,ri],让你选出从中一个子序列,然后在子序列的每个区间里都选择一个tj,满足$t_1< ...

  2. CF 809 D Hitchhiking in the Baltic States —— 思路+DP(LIS)+splay优化

    题目:http://codeforces.com/contest/809/problem/D 看题解,抄标程...发现自己连 splay 都快不会写了... 首先,题目就是要得到一个 LIS: 但与一 ...

  3. Codeforces 809D. Hitchhiking in the Baltic States

    Description 给出 \(n\) 个数 \(a_i\),每一个数有一个取值 \([l_i,r_i]\) ,你来确定每一个数,使得 \(LIS\) 最大 题面 Solution 按照平时做法,设 ...

  4. CodeForces 809D Hitchhiking in the Baltic States(FHQ-Treap)

    题意 给你长度为$n$的序列,序列中的每个元素$i$有一个区间限制$[l_i,r_i]$,你从中选出一个子序列,并给它们标号$x_i$,要求满足 $,∀i<j,x_i<x_j$,且$, ∀ ...

  5. 【CF809D】Hitchhiking in the Baltic States(Splay,动态规划)

    [CF809D]Hitchhiking in the Baltic States(Splay,动态规划) 题面 CF 洛谷 题解 朴素\(dp\):设\(f[i][j]\)表示当前考虑到第\(i\)个 ...

  6. CF809D Hitchhiking in the Baltic States

    CF809D Hitchhiking in the Baltic States CF809D 长度为n的序列{xi},n<=3e5,范围在(li,ri)之间,求LIS最长是多长g(i,l)表示前 ...

  7. CF809D Hitchhiking in the Baltic States LIS、平衡树

    传送门 看到最长上升子序列肯定是DP 设\(f_i\)表示计算到当前,长度为\(i\)的最长上升子序列的最后一项的最小值,显然\(f_i\)是一个单调递增的序列. 转移:对于当前计算的元素\(x\), ...

  8. 【CF809D】Hitchhiking in the Baltic States

    题意: 给你n个区间[li,ri],让你选出从中一个子序列,然后在子序列的每个区间里都选择一个tj,满足t1<t2<...<tlent1<t2<...<tlen.最 ...

  9. BZOJ 3173: [Tjoi2013]最长上升子序列 [splay DP]

    3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1613  Solved: 839[Submit][St ...

随机推荐

  1. Nutch学习笔记二——抓取过程简析

    在上篇学习笔记中http://www.cnblogs.com/huligong1234/p/3464371.html 主要记录Nutch安装及简单运行的过程. 笔记中 通过配置抓取地址http://b ...

  2. golang map to struct

    http://stackoverflow.com/questions/26744873/converting-map-to-struct func SetField(obj interface{}, ...

  3. WinDbg抓取dmp文件

    应用程序发生异常时抓取dmp: adplus.vbs -crash -pn w3wp.exe -y srv*c:\symbols*http://msdl.microsoft.com/download/ ...

  4. Eoeclient源代码分析---SlidingMenu的使用

    Eoeclient源代码分析及代码凝视 使用滑动菜单SlidingMenu,单击滑动菜单的不同选项,能够通过ViewPager和PagerIndicator显示相应的数据内容. 0  BaseSlid ...

  5. PHP部分--file图片上传服务器、图片路径存入数据库,并读取

    前端代码 <form action="shangchuan.php" method="post" enctype="multipart/form ...

  6. linux安装ssh(转载)

    CentOS安装ssh最笨的方法:yum install ssh yum install openssh-server/etc/init.d/sshd status看sshd服务的状态/etc/ini ...

  7. 下拉刷新swipetoloadlayout的使用方法,以及自己定义头部

    <20160930---------–更新内容 回过头看自己曾经写的这个博客非常多的废话 和效果并不适合大家去使用这个好用的控件 如今整理删掉了自己写的效果, 写了个最简单的实例给一起学习的新手 ...

  8. SAP-ABAP系列 第一篇SAP简介

    第一篇 SAP简介 SAP全名为System Application and Products in Data Processing.SAP目前是全世界排名第一的RP软件,号称“全球最大的企业管理解决 ...

  9. 主题:iframe高度的自适应

    在项目开发中,遇到的一个问题.弹出的页面中有iframe.例 <iframe src="www.baidu.html" width="100%" char ...

  10. jquery根据(遍历)html()的内容/根据子元素的内容(元素文本)来选择(查询),在子元素前加入元素

    <ul> <li>First</li> <li>second</li> <li>third</li> </ul ...