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 ...
随机推荐
- 快捷键整理(来源:http://www.cnblogs.com/xing901022/p/4741630.htm)
Eclipse 跳转到指定行:ctrl+l 1几个最重要的快捷键 代码助手:Ctrl+Space(简体中文操作系统是Alt+/)快速修正:Ctrl+1单词补全:Alt+/打开外部Java文档:Shif ...
- 不要拿ERP的报表忽悠领导!——一个报表引发的企业经营反思
文 | 帆软数据应用研究院船长 本文出自:知乎专栏<帆软数据应用研究院>——数据干货&资讯集中地 领导的经营决策能只依赖于ERP报表吗? 不能! 1. ERP报表个性化不足:企业经 ...
- 基于InfluxDB实现分页查询功能
InfluxDB作为时序数据库中的翘楚,应用范围非常广泛,尤其在监控领域. 最近做了一个功能,将InfluxDB中的数据查询出来后,在前台分页展现,比如每页10条,一共100页,可以查看首页.末页,进 ...
- ios开发GCD(2)-dispatch_semaphore_t信号量计数器
思考:现在有多个线程异步执行,我们想要同时最多只能执行2个或n个,该怎么办? dispatch_semaphore_t 看代码解析: NSLog(@"开始"); dispatch_ ...
- Fiddler抓包学习——https请求的抓取
第一步:设置Fiddler windows下安装证书 打开fiddler 查看证书是否安装 说明已安装成功 设置端口号(下面手机设置代理所需要的) 第二部 手机端安装证书 通过fiddler查看电 ...
- [20170615]执行dbms_sqldiag.dump_trace看执行计划.txt
[20170615]执行dbms_sqldiag.dump_trace看执行计划.txt --//上午在想查看10053执行计划时使用包时出现如下提示: SCOTT@book> @ &r ...
- 根据id来大量删除数据between
id的范围来删除数据 比如要删除 110到220的id信息:delete id from 表名 where id between 110 and 220;
- sftp 建立用户
1.创建sftp组:#groupadd sftp 2.创建测试账户:#useradd -g sftp -s /bin/false testuser 修改密码:# passwd sftp 3.修改测试账 ...
- Windows 下配置 Apache 支持 https
1.打开cmd ,输入 F: // 切换到Apache安装路径,我的Apache安装目录在 F盘 2.cd F:\Apache\bin 3.set "openssl_conf = F:\A ...
- Python进阶(三)
匿名函数 匿名函数有个限制,就是只能有一个表达式,不用写return,返回值就是该表达式的结果.用匿名函数有个好处,因为函数没有名字,不必担心函数名冲突.此外,匿名函数也是一个函数对象,也可以把匿名函 ...