CF809D Hitchhiking in the Baltic States LIS、平衡树
看到最长上升子序列肯定是DP
设\(f_i\)表示计算到当前,长度为\(i\)的最长上升子序列的最后一项的最小值,显然\(f_i\)是一个单调递增的序列。
转移:对于当前计算的元素\(x\),它的取值范围为\([l,r]\),设当前可以转移的区间为\([j,k]\)(即对于\(\forall p \in [j,k] , f_p \in [l,r)\)且\(f_{j-1} < l , f_{k + 1} \geq r\)),则对于\(\forall p \in [j,k]\)都有\(f_{p + 1} = f_{p} + 1\)(因为序列\(f_i\)是单调递增的,所以转移一定更优)且\(f_j = l\)。
考虑这个转移方程,它的实质就是:删去第\(k+1\)个元素,第\(j\)到\(k\)个元素统一\(+1\),在第\(j\)个元素的左边插入一个值为\(l\)的元素。其实就是平衡树的基本操作,使用平衡树维护即可。
还有我也不知道我的Splay为什么每一次要随机一个点Splay到根才能过……
#include<bits/stdc++.h>
#define MAXN 300010
#define inf 0x7fffffff
using namespace std;
struct node{
int ch[2] , fa , num , add;
}Tree[MAXN];
int cntNode = 2 , N , root = 1;
inline bool son(int dir){
return Tree[Tree[dir].fa].ch[1] == dir;
}
inline void ZigZag(int dir){
bool f = son(dir);
if(Tree[dir].fa == root)
root = dir;
Tree[Tree[dir].fa].ch[f] = Tree[dir].ch[f ^ 1];
Tree[Tree[dir].ch[f ^ 1]].fa = Tree[dir].fa;
int x = Tree[Tree[dir].fa].fa;
Tree[x].ch[son(Tree[dir].fa)] = dir;
Tree[Tree[dir].fa].fa = dir;
Tree[dir].ch[f ^ 1] = Tree[dir].fa;
Tree[dir].fa = x;
}
inline void pushdown(int dir){
if(Tree[dir].add){
Tree[Tree[dir].ch[0]].add += Tree[dir].add;
Tree[Tree[dir].ch[1]].add += Tree[dir].add;
Tree[Tree[dir].ch[0]].num += Tree[dir].add;
Tree[Tree[dir].ch[1]].num += Tree[dir].add;
Tree[dir].add = 0;
}
}
inline void Splay(int dir , int fa){
pushdown(dir);
while(Tree[dir].fa != fa)
if(Tree[Tree[dir].fa].fa == fa)
ZigZag(dir);
else{
if(son(Tree[dir].fa) == son(dir))
ZigZag(Tree[dir].fa);
else
ZigZag(dir);
ZigZag(dir);
}
}
void insert(int &now , int num , int fa){
if(now == 0){
now = ++cntNode;
Tree[now].num = num;
Tree[now].fa = fa;
Splay(now , 0);
return;
}
pushdown(now);
insert(Tree[now].ch[Tree[now].num < num] , num , now);
}
void getPre(int now , int num , int minN){
if(now == 0){
Splay(minN , 0);
return;
}
pushdown(now);
if(Tree[now].num == num){
int t = Tree[now].ch[0];
while(t){
pushdown(t);
if(Tree[t].num != num)
minN = t;
t = Tree[t].ch[1];
}
Splay(minN , 0);
return;
}
if(Tree[now].num < num)
getPre(Tree[now].ch[1] , num , Tree[now].num > Tree[minN].num ? now : minN);
else
getPre(Tree[now].ch[0] , num , minN);
}
void getNXT(int now , int num , int maxN){
if(now == 0){
Splay(maxN , root);
return;
}
pushdown(now);
if(Tree[now].num == num){
int t = Tree[now].ch[1];
while(t){
pushdown(t);
maxN = t;
t = Tree[t].ch[0];
}
Splay(maxN , root);
return;
}
if(Tree[now].num < num)
getNXT(Tree[now].ch[1] , num , maxN);
else
getNXT(Tree[now].ch[0] , num , Tree[now].num < Tree[maxN].num ? now : maxN);
}
inline void find(int dir){
if(Tree[dir].fa)
find(Tree[dir].fa);
pushdown(dir);
}
int main(){
srand((unsigned)time(0));
int ans = 0;
scanf("%d" , &N);
Tree[1].ch[1] = 2;
Tree[2].num = inf;
Tree[2].fa = 1;
for(int i = 1 ; i <= N ; i++){
int a , b;
scanf("%d%d" , &a , &b);
getPre(root , a , 1);
getNXT(root , b - 1 , 2);
Tree[Tree[Tree[root].ch[1]].ch[0]].add++;
Tree[Tree[Tree[root].ch[1]].ch[0]].num++;
if(Tree[Tree[root].ch[1]].ch[1]){
ZigZag(Tree[root].ch[1]);
getNXT(Tree[root].ch[1] , Tree[root].num , 2);
Tree[Tree[root].ch[1]].ch[0] = Tree[root].ch[0];
root = Tree[root].ch[1];
Tree[Tree[root].ch[0]].fa = root;
Tree[root].fa = 0;
}
else
ans++;
insert(Tree[root].ch[Tree[root].num < a] , a , root);
int t = rand() % (i + 2) + 1;
find(t);
Splay(t , 0);
}
cout << ans;
return 0;
}
CF809D Hitchhiking in the Baltic States LIS、平衡树的更多相关文章
- 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 Splay
[CF809D]Hitchhiking in the Baltic States 题意:给你n个区间[li,ri],让你选出从中一个子序列,然后在子序列的每个区间里都选择一个tj,满足$t_1< ...
- 【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
题意: 给你n个区间[li,ri],让你选出从中一个子序列,然后在子序列的每个区间里都选择一个tj,满足t1<t2<...<tlent1<t2<...<tlen.最 ...
- 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$,且$, ∀ ...
- CF 809D Hitchhiking in the Baltic States——splay+dp
题目:http://codeforces.com/contest/809/problem/D 如果值是固定的,新加入一个值,可以让第一个值大于它的那个长度的值等于它. 如今值是一段区间,就对区间内的d ...
- Noip前的大抱佛脚----赛前任务
赛前任务 tags:任务清单 前言 现在xzy太弱了,而且他最近越来越弱了,天天被爆踩,天天被爆踩 题单不会在作业部落发布,所以可(yi)能(ding)会不及时更新 省选前的练习莫名其妙地成为了Noi ...
随机推荐
- 从输入url到页面展示到底发生了什么
阅读目录 1.输入地址 2.浏览器查找域名的 IP 地址 3.浏览器向 web 服务器发送一个 HTTP 请求 4.服务器的永久重定向响应 5.浏览器跟踪重定向地址 6.服务器处理请求 7.服务器返回 ...
- 【Java入门提高篇】Day26 Java容器类详解(八)HashSet源码分析
前面花了好几篇的篇幅把HashMap里里外外说了个遍,大家可能对于源码分析篇已经讳莫如深了.别慌别慌,这一篇来说说集合框架里最偷懒的一个家伙——HashSet,为什么说它是最偷懒的呢,先留个悬念,看完 ...
- [20170623]利用传输表空间恢复部分数据.txt
[20170623]利用传输表空间恢复部分数据.txt --//昨天我测试使用传输表空间+dblink,上午补充测试发现表空间设置只读才能执行impdp导入原数据,这个也很好理解.--//这样的操作模 ...
- [20170728]oracle保留字.txt
[20170728]oracle保留字.txt --//oracle有许多保留字,我印象最深的就是使用rman备份表空间test,test就是rman里面的保留字.--//还有rman也是rman里面 ...
- 洗礼灵魂,修炼python(11)--python函数,模块
前面的章节你如果看懂了,基本算是入门了七八了,不过如果你以为python就这么点东西,你觉得很简单啊,那你就错了,真正的东西在后面,前面我说的几大核心其实也不是多么高深多么厉害的,那些东西是基础很常用 ...
- Spring MVC 的工作原理
引自:https://www.cnblogs.com/xiaoxi/p/6164383.html SpringMVC的工作原理图: SpringMVC流程 1. 用户发送请求至前端控制器Dispat ...
- Java jni字符串转换
1.jstring转QString 对于Qt5.2以上(含)可以用QAndroidJniObject::toString(),详见这里:https://stackoverflow.com/questi ...
- 解决内存不能为read错误
解决方法 1. 命令解决方法:开始菜单,运行,输入cmd,回车,在命令提示符下输入(复制即可) :for %1 in (%windir%\system32\*.ocx) do regsv ...
- January 19th, 2018 Week 3rd Friday
As iron sharpens iron, so a friend sharpens a friend. 铁磨铁,可以磨砺出刀刃,朋友相交,亦应如此. When making friends wit ...
- 使用vue-cli脚手架创建的项目结构详解
项目整体目录结构预览 src目录 src整体结构 开发过程中基本上操作都在该目录下进行操作的,项目所有源码都是在这个目录下 main.js文件,项目核心文件 App.vue文件,项目入口文件 rout ...