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 ...
随机推荐
- nginx-2-nginx的反向代理
Nginx服务器的反向代理服务 nginx服务器的反向代理服务是其最常用的重要功能之一,在实际的工作当中应用广泛,涉及的指令也比较多,各类指令完成的功能也不尽相同.
- Salesforce服务云简介
服务云简介 Salesforce的服务云(Service Cloud)是专注于客服和呼叫中心解决方案的子系统.它是Salesforce核心CRM系统的一部分. 服务云特性 服务云提供了客户服务和呼叫中 ...
- Ansible--常用模块使用
ansible命令解释 通过ansible命令执⾏的任务称为ad-hoc命令(任务),其实它是相对playbook⽽⾔的.通常,命令⾏⽤来实现ansible的批量管理功能,playbook⽤来实现批量 ...
- 【转】解决CentOS 64位系统vsftpd 530 login incorrect的问题
转自:http://www.centos.bz/2011/12/centos-64-install-vsftpd-530-login-incorredct/ 今天在centos 6 64位测试安装vs ...
- Fiddler抓包使用教程-过滤
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/72929800 本文出自[赵彦军的博客] Fiddler抓包可以完成我们移动开发者的 ...
- (其他)最常用的15大Eclipse开发快捷键技巧
转自CSDNJava我人生(陈磊兴) 原文出处 引言 做java开发的,经常会用Eclipse或者MyEclise集成开发环境,一些实用的Eclipse快捷键和使用技巧,可以在平常开发中节约出很多 ...
- BigDecimal常被忽略的问题
一:相除精度丢失的问题 BigDecimal的api除法相对加减乘要实现的复杂多了,只介绍常用的我遇到的问题: 问题:两数相除,如果9/3=3整除没问题,但是10/3=0.33333333...... ...
- windows下安装Erlang
由于RabbitMQ是用Erlang编写的,因此需要先安装Erlang环境,建议安装的版本新一点.下载地址点我试试 我这里下载的V20.3 x64版本,下载后点击开始安装,基本是一路next(默认设置 ...
- 安装Linux Mint 17后要做的20件事
Linux Mint 17 Qiana Cinnamon Linux Mint 17已经发布,定名为Qiana.Mint是Linux最佳发行版之一,它定位于桌面用户,关注可用性和简洁.它携带了风格迥异 ...
- matplotlib多plot可视化
代码: # -*- coding: utf-8 -*- """ Created on Thu Jul 12 16:37:47 2018 @author: zhen &qu ...