传送门


看到最长上升子序列肯定是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、平衡树的更多相关文章

  1. CF809D Hitchhiking in the Baltic States

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

  2. 【CF809D】Hitchhiking in the Baltic States Splay

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

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

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

  4. 【CF809D】Hitchhiking in the Baltic States

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

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

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

  6. Codeforces 809D. Hitchhiking in the Baltic States

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

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

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

  8. CF 809D Hitchhiking in the Baltic States——splay+dp

    题目:http://codeforces.com/contest/809/problem/D 如果值是固定的,新加入一个值,可以让第一个值大于它的那个长度的值等于它. 如今值是一段区间,就对区间内的d ...

  9. Noip前的大抱佛脚----赛前任务

    赛前任务 tags:任务清单 前言 现在xzy太弱了,而且他最近越来越弱了,天天被爆踩,天天被爆踩 题单不会在作业部落发布,所以可(yi)能(ding)会不及时更新 省选前的练习莫名其妙地成为了Noi ...

随机推荐

  1. maven 技巧

    M2Eclipse Releases maven eclipse插件最新安装地址 Full Version Tag 1.0 2011-06-22 http://download.eclipse.org ...

  2. 精通initramfs构建step by step

    (一)hello world  一.initramfs是什么  在2.6版本的linux内核中,都包含一个压缩过的cpio格式 的打包文件.当内核启动时,会从这个打包文件中导出文件到内核的rootfs ...

  3. Python零基础学习系列之三--Python编辑器选择

    上一篇文章记录了怎么安装Python环境,同时也成功的在电脑上安装好了Python环境,可以正式开始自己的编程之旅了.但是现在又有头疼的事情,该用什么来写Python程序呢,该用什么来执行Python ...

  4. java基础-温故而知新(02)

    基本数据的自动拆装箱及享元设计模式 1.1 自动装箱        -128~127 之间的整数,装在一个内存区域.         超过这个范围的整数,装在不同的内存区域. 1.2 自动拆箱     ...

  5. deepin安装Oracle jdk8,以及添加add-apt-repository命令支持

    @font-face{ font-family:"Times New Roman"; } @font-face{ font-family:"宋体"; } p.M ...

  6. Beta冲刺! Day4 - 砍柴

    Beta冲刺! Day4 - 砍柴 今日已完成 晨瑶:追进度 昭锡:改主页UI(还在 永盛:完成大部分接口和接口文档,上线代码 立强:文章去广告,适配手机屏幕.第三方编辑器整合到记录模块. 炜鸿:完成 ...

  7. 服务器出现大量的127.0.0.1:3306 TIME_WAIT连接 解决方法 [转载]

    netstat -an 192.168.12.13:3306      192.168.12.12:30443      TIME_WAIT 192.168.12.13:3306      192.1 ...

  8. jweixin-1.1.0.js微信接口“分享给朋友”文字和图片配置

    在npm官网找到了:微信官方 js-sdk CommonJS 版https://www.npmjs.com/package/weixin-js-sdk 步骤一:引入JS文件在需要调用JS接口的页面引入 ...

  9. 在Oracle中使用Guid

    sys_guid()  -  唯一索引 在Oracle中可以用SYS_GUID()来生成一个guid,相当于msSql中的newid(). 在Oracle9i和Oracle 10g 里SYS_GUID ...

  10. 控件布局_LinearLayout

    gravity和layout_gravity的区别 android:gravity与android:layout_gravity.他们的区别在于:android:gravity用于设置View组件的对 ...