bzoj 2225 [Spoj 2371]Another Longest Increasing
这道题 连续上升的三元组 且已经按照第一维排好序了。
直接上CDQ分治即可 当然也是可以2-Dtree解决这个 问题 但是感觉nlog^2 比nsqrt(n)要快一些。。
算是复习一发CDQ分治吧 也好久没写了。
原来最长三元上升序列 不是裸的CDQ分治。。我以为是 没细想 最后还是细想了一下实现方式。
首先CDQ左边 然后对于右边此时x是无序的 考虑排序 在外面排序没用好吧。。
好吧可能有用但是太过繁琐那种写法 这里推荐暴力sort。。归并没用 因为归并此时复杂度还是nlogn的。
统计完左边对右边的贡献后 再桶排序复原。再CDQ右边 回来的时候可以进行归并不需要再sort了节省常数。。
复杂度nlog^2。
const int MAXN=100010;
int n,top,ans;
struct wy
{
int x,y;
int id;
inline int friend operator <(wy a,wy b){return a.x==b.x?a.id<b.id:a.x<b.x;}
}t[MAXN],ql[MAXN];
int b[MAXN],f[MAXN],c[MAXN];
inline void discrete()
{
sort(b+1,b+1+n);
rep(1,n,i)if(i==1||b[i]!=b[i-1])b[++top]=b[i];
rep(1,n,i)y(i)=lower_bound(b+1,b+1+top,y(i))-b;
}
inline void add(int x,int y)
{
if(y==-1)
{
while(x<=top)
{
c[x]=0;
x+=x&(-x);
}
return;
}
while(x<=top)
{
c[x]=max(c[x],y);
x+=x&(-x);
}
}
inline int ask(int x)
{
int cnt=0;
while(x)
{
cnt=max(cnt,c[x]);
x-=x&(-x);
}
return cnt;
}
inline void CDQ(int l,int r)
{
if(l==r){++f[id(l)];return;}
int mid=(l+r)>>1;
CDQ(l,mid);
sort(t+mid+1,t+r+1);
int i=l,j=mid+1;
for(int k=l;k<=r+1;++k)
{
if(j>r)
{
for(int w=i-1;w>=l;--w)add(y(w),-1);
break;
}
if((i<=mid)&&x(i)<x(j))add(y(i),f[id(i)]),++i;
else f[id(j)]=max(f[id(j)],ask(y(j)-1)),++j;
}
for(int k=mid+1;k<=r;++k)ql[id(k)]=t[k];
for(int k=mid+1;k<=r;++k)t[k]=ql[k];
CDQ(mid+1,r);
i=l;j=mid+1;
for(int k=l;k<=r;++k)
{
if(i<=mid&&x(i)<x(j)||j>r)ql[k]=t[i],++i;
else ql[k]=t[j],++j;
}
for(int k=l;k<=r;++k)t[k]=ql[k];
}
int main()
{
freopen("1.in","r",stdin);
get(n);
rep(1,n,i)get(x(i)),b[i]=get(y(i)),id(i)=i;
discrete();
CDQ(1,n);
for(int i=1;i<=n;++i)ans=max(ans,f[i]);
printf("%d\n",ans);
return 0;
}
这个树状数组清空的时候注意不要暴力清空 再来一遍序列 清成0即可。
bzoj 2225 [Spoj 2371]Another Longest Increasing的更多相关文章
- BZOJ 2225 [Spoj 2371]Another Longest Increasing(CDQ分治)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2225 [题目大意] 给定N个数对(xi,yi),求最长上升子序列的长度. 上升序列定义 ...
- BZOJ 2225: [Spoj 2371]Another Longest Increasing (CDQ分治+dp)
题面 Description 给定N个数对(xi, yi),求最长上升子序列的长度.上升序列定义为{(xi, yi)}满足对i<j有xi<xj且yi<yj. Input Output ...
- 【bzoj2225】[Spoj 2371]Another Longest Increasing CDQ分治+树状数组
题目描述 给定N个数对(xi, yi),求最长上升子序列的长度.上升序列定义为{(xi, yi)}满足对i<j有xi<xj且yi<yj. 样例输入 8 1 3 3 2 1 1 4 5 ...
- BZOJ2225: [Spoj 2371]Another Longest Increasing CDQ分治,3维LIS
Code: #include <cstdio> #include <algorithm> #include <cstring> #define maxn 20000 ...
- BZOJ_2225_[Spoj 2371]Another Longest Increasing_CDQ 分治+树状数组
BZOJ_2225_[Spoj 2371]Another Longest Increasing_CDQ 分治+树状数组 Description 给定N个数对(xi, yi),求最长上升子 ...
- SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治
Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://a ...
- SPOJ - LIS2 Another Longest Increasing Subsequence Problem
cdq分治,dp(i)表示以i为结尾的最长LIS,那么dp的递推是依赖于左边的. 因此在分治的时候需要利用左边的子问题来递推右边. (345ms? 区间树TLE /****************** ...
- SPOJ LIS2 - Another Longest Increasing Subsequence Problem(CDQ分治优化DP)
题目链接 LIS2 经典的三维偏序问题. 考虑$cdq$分治. 不过这题的顺序应该是 $cdq(l, mid)$ $solve(l, r)$ $cdq(mid+1, r)$ 因为有个$DP$. #i ...
- SPOJ Another Longest Increasing Subsequence Problem 三维最长链
SPOJ Another Longest Increasing Subsequence Problem 传送门:https://www.spoj.com/problems/LIS2/en/ 题意: 给 ...
随机推荐
- css3-pointer-events_demo
该demo定义了一个菜单,点击了一次之后就不能再点击,另外其中也用到了flex布局,可直接将代码复制运行即可 <html> <head> <meta name=" ...
- OldTrafford after 102 days
THE RED GO MARCHING ON One Team One Love Through the highs and the lows One hundred and two long ...
- 状压DP之Bill的挑战
题目 P2167 [SDOI2009]Bill的挑战 Sheng bill不仅有惊人的心算能力,还可以轻松地完成各种统计.在昨天的比赛中,你凭借优秀的程序与他打成了平局,这导致Sheng bill极度 ...
- 「区间DP」「洛谷PP3146 」[USACO16OPEN]248 G
[USACO16OPEN]248 G 题目: 题目描述 Bessie likes downloading games to play on her cell phone, even though sh ...
- Azure Web App (一)发布你的Net Core Web 项目
一,引言 今天我们看一下Azure上的一个服务-----Web 应用,我们都知道云计算的三大模式:Iaas(基础设施即服务),Paas(平台即服务),Saas(软件即服务). Iass,其实就是虚拟主 ...
- day07总结
元组常用操作 # ======================================基本使用======================================# 1.用途:元组就是 ...
- Spring Boot 2 实战:常用读取配置的方式
1. 前言 在Spring Boot项目中我们经常需要读取application.yml配置文件的自定义配置,今天就来罗列一下从yaml读取配置文件的一些常用手段和方法. 2. 使用@Value注解 ...
- Unity-JobSystem
NativeContainer 为什么 为解决Job之间数据处理结果的隔离问题. 是什么 NativeContainer是托管的值类型,可以相对安全的操作native内存. 注意 不能两个job同时写 ...
- Xamarin.Android调用百度地图
下载百度地图API Android SDK 在Visual Studio中创建绑定库(Android)项目 将jar文件添加到Jars文件夹中 生成该项目,如果遇到变量名称之类的问题,可在Transf ...
- 2020年Dubbo30道高频面试题!还在为面试烦恼赶快来看看!
前言 Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式 ...