CF 809 D Hitchhiking in the Baltic States —— 思路+DP(LIS)+splay优化
题目:http://codeforces.com/contest/809/problem/D
看题解,抄标程...发现自己连 splay 都快不会写了...
首先,题目就是要得到一个 LIS;
但与一般不同的是,新加入的不是一个值,而是一个取值范围;
仍是设 f[i] 表示长度为 i 的 LIS 的结尾最靠前是哪个位置,而此时新出现一个区间 l~r;
如果 f[i] < l,那么可以接上一个 l 变成 f[i+1],也就是 f[i+1] = l;
如果 l <= f[i] <= r,也可以把这个 f[i] 通过选这个区间里的值而尽量提前,最多能提前到上一个 f[j] , l <= f[j] < f[i] <= r;
也就是此时,f[i] = f[j] + 1;
可以知道上一个位置 j 就是 i-1,也就是新增区间带来了这样的转移:f[i] = min( f[i] , f[i-1] + 1),而 f[i-1] + 1 <= f[i] , 所以 f[i] = f[i-1] + 1;
综上,新增加一个区间:
1.会使 l 左边第一个 f[i] < l 转移到 f[i+1] = l;
2.使区间内部的值都向前跳到上一个的后一个,即 f[i+1] = f[i] + 1 , l <= f[i] < f[i+1] <= r;
3.使右边第一个 f[i] > r 转移到 f[i-1] + 1;
可以发现,转移2可以看做是区间内的 f[] 都向右移动了一下,然后区间左边第一个移动到了左端点,右边第一个移到区间内,同时角标(答案)+1;
就可以用 splay 维护,每次找到值在 l~r 范围内的,+1,再删除……,加入……什么的...
然后就模仿了标程的写法...
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ls c[x][0]
#define rs c[x][1]
using namespace std;
int const xn=3e5+;
int n,Q,v[xn],lzy[xn],fa[xn],c[xn][],rt,ans;
int rd()
{
int ret=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=; ch=getchar();}
while(ch>=''&&ch<='')ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return f?ret:-ret;
}
void pushdown(int x)
{
if(!lzy[x])return;
v[ls]+=lzy[x]; lzy[ls]+=lzy[x];
v[rs]+=lzy[x]; lzy[rs]+=lzy[x];
lzy[x]=;
}
void push(int x)
{
if(fa[x])push(fa[x]);
pushdown(x);
}
void rotate(int x)
{
int y=fa[x],z=fa[y],d=(c[y][]==x);
if(z)c[z][c[z][]==y]=x;
fa[x]=z; fa[y]=x; fa[c[x][!d]]=y;
c[y][d]=c[x][!d]; c[x][!d]=y;
}
void splay(int x,int f)
{
push(x);//!
while(fa[x]!=f)
{
int y=fa[x],z=fa[y];
if(z!=f)
{
if((c[y][]==x)^(c[z][]==y))rotate(x);
else rotate(y);
}
rotate(x);
}
if(!f)rt=x;
}
int low(int k)//<=k
{
int nw=rt,ret=;
while(nw)
{
pushdown(nw);//!!!
if(v[nw]<k)ret=nw,nw=c[nw][];
else nw=c[nw][];
}
return ret;
}
int nxt(int x)
{
splay(x,); int nw=c[x][];
while(c[nw][])nw=c[nw][];
return nw;
}
void insert(int &x,int k,int f)
{
if(!x){v[x=++n]=k; fa[x]=f; splay(x,); return;}
pushdown(x);//!!
insert(c[x][k>v[x]],k,x);
}
void del(int x)
{
splay(x,); int p=c[x][],q=c[x][];
while(c[p][])p=c[p][];
while(c[q][])q=c[q][];
splay(p,); splay(q,p);
c[q][]=; fa[x]=;
}
void solve(int l,int r)
{
int p=low(l),q=low(r),t=nxt(q);
if(p!=q)//!
{
splay(p,); splay(t,p);
v[c[t][]]++; lzy[c[t][]]++;
}
if(t!=)del(t),ans--;
insert(p,l,); ans++;
}
int main()
{
Q=rd();
v[++n]=(<<); v[++n]=-(<<); fa[]=; c[][]=; rt=;//
for(int i=,l,r;i<=Q;i++)l=rd(),r=rd(),solve(l,r);
printf("%d\n",ans);
return ;
}
CF 809 D Hitchhiking in the Baltic States —— 思路+DP(LIS)+splay优化的更多相关文章
- 【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 Splay
[CF809D]Hitchhiking in the Baltic States 题意:给你n个区间[li,ri],让你选出从中一个子序列,然后在子序列的每个区间里都选择一个tj,满足$t_1< ...
- CF809D Hitchhiking in the Baltic States
CF809D Hitchhiking in the Baltic States CF809D 长度为n的序列{xi},n<=3e5,范围在(li,ri)之间,求LIS最长是多长g(i,l)表示前 ...
- CF 809D Hitchhiking in the Baltic States——splay+dp
题目:http://codeforces.com/contest/809/problem/D 如果值是固定的,新加入一个值,可以让第一个值大于它的那个长度的值等于它. 如今值是一段区间,就对区间内的d ...
- 【CF809D】Hitchhiking in the Baltic States
题意: 给你n个区间[li,ri],让你选出从中一个子序列,然后在子序列的每个区间里都选择一个tj,满足t1<t2<...<tlent1<t2<...<tlen.最 ...
- CF809D Hitchhiking in the Baltic States LIS、平衡树
传送门 看到最长上升子序列肯定是DP 设\(f_i\)表示计算到当前,长度为\(i\)的最长上升子序列的最后一项的最小值,显然\(f_i\)是一个单调递增的序列. 转移:对于当前计算的元素\(x\), ...
- 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$,且$, ∀ ...
- E - Emptying the Baltic Kattis - emptyingbaltic (dijkstra堆优化)
题目链接: E - Emptying the Baltic Kattis - emptyingbaltic 题目大意:n*m的地图, 每个格子有一个海拔高度, 当海拔<0的时候有水. 现在在(x ...
随机推荐
- 复(学)习化学时突然的一个 idea
期中考试成功探底...但是某些化学问题还是很有信息学价值的... n 烷同分异构体计数. 这个题 fanhq666 出过,就是一个 dp. 设 f[i] 表示含有 i 个节点的无标号不同构的度数限制为 ...
- [HDU3065]病毒持续侵袭中(AC自动机)
传送门 AC自动机的又一模板,统计每个字符串在文本中的次数. 所以就不需要vis数组了. ——代码 #include <cstdio> #include <cstring> # ...
- hdu 2831
#include<stdio.h> #include<stdlib.h> struct node{ int x,y,j,num; }a[110]; int cmp(const ...
- 汕头市赛srm8 C-3
n<=100000个点m<=300000条边有权无向联通图,给出K<=10000个特殊点求K个点中任意两点最短路的最小值. 方法一:K小,随便搞.先构造最短路树,在最短路树上Dijk ...
- poj1273最大流初破
第一次网络流,学了一天的DINIC算法(个人比较愚),切了这个入门题,开始的时候怎么调连 测试都过不了,后来发现犯了一个低级错误!把判断条件放在for(:)!里面和放在for下面大大 不同啊!里面的话 ...
- C. Wilbur and Points---cf596C
http://codeforces.com/problemset/problem/596/C 题目大意: 给你n个数对 确保如果(x,y)存在这个集合 那么 0<=x'<=x &a ...
- webpack体积优化篇二(GZ压缩)
这里我列举几个常用的能够用于减少包体大小的插件,我们可以根据项目需求选择性的使用: compression-webpack-plugin :该插件能够将资源文件压缩为.gz文件,并且根据客户端的需求按 ...
- 限制input的输入类型
1.只能输入和粘贴汉字 <input onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste= ...
- [React] PureComponent in React
In this lesson, you will learn how to use PureComponent in React to reduce the number of times your ...
- jquery全局变量---同步请求设置
1.同步 $.ajaxSetup({ async: false }); 2.异步 $.ajaxSetup({ async: true }); 3.说明:我们一般使用同步完要恢复异步.由于js默 ...