题解:P13968 [VKOSHP 2024] Classics

· · 题解

前言

今天复习平衡树写到的题 , 有感而发 ,就写下此解~~~

原题链接

大体题意就是 , 给你一个n , 从1插到n , 每次给你一个插入位置 , 插入i , 问你此时最长上升子序列; 首先我们不难发现 , 要插入i到pos_{i}和pos_{i + 1}之间 ,首先考虑[1 \thicksim pos_{i}] , 这一段的最长上升子序列不会发生变化 ,而你又发现插入的数都比[pos_i + 1 \thicksim n]区间内的数大 , 所以这个区间内的的最长上升子序列也不会被影响 , 那么你算插入点的最长上升子序列就是[1 \thicksim pos_{i}]的最长上升子序列加1 ; 说到这,又有一个问题 , 我们插入是使用什么呢 ???? 有请高级ds —— 平衡树登场 , 这个玩意就可以维护 , 只是平衡树的板子需要改一下 , 只需将split操作权值比较换为下标比较(就是用子树大小 , 因为我们插入也是按照下标插入), 可能光口胡有些不清楚 , 来张图

那好了这个题就结束了; 时间复杂度O(n \log n)

#include<bits/stdc++.h>
using namespace std;
#define lson tree[rt].ls
#define rson tree[rt].rs
mt19937 rd(114514);
const int N = 2e5 + 10;
int n , tot , root;
struct st{
    int ls , rs , dp , maxn , rnd , siz;
};
st tree[N << 2];
int New(){
    tot ++;
    tree[tot] = {0 , 0 , 1 , 1 , rand() , 1};
    return tot;
}
void pushup(int rt){
    tree[rt].siz = tree[lson].siz + tree[rson].siz + 1;
    tree[rt].maxn = max({tree[lson].maxn , tree[rson].maxn , tree[rt].dp});
}
void split(int rt , int v , int &x , int &y){
    if(! rt) return x = y = 0 , void();
    int tmp = tree[lson].siz + 1;
    if(tmp <= v) x = rt , split(rson , v - tmp , rson , y);
    else y = rt , split(lson , v , x , lson);
    pushup(rt);
}
int merge(int rt , int v){
    if(! rt || ! v) return rt | v;
    if(tree[rt].rnd < tree[v].rnd){
        rson = merge(rson , v);
        pushup(rt);
        return rt;
    }
    else{
        tree[v].ls = merge(rt , tree[v].ls);
        pushup(v);
        return v;
    }
}
void insert(int v){
    int x , y;
    split(root , v , x , y);
    int nw = New();
    tree[nw].dp = tree[nw].maxn = tree[x].maxn + 1;
    root = merge(merge(x , nw) , y);
}
int main(){
    ios::sync_with_stdio(false); cin.tie(0) ; cout.tie(0);
    int p;
    cin >> n;
    for(int i = 1 ; i <= n ; i ++){
        cin >> p , insert(p - 1);
        cout << tree[root].maxn << endl;
    }   
    return 0;
}