分析

这回试了一下三级标题,不知道效果怎么样?

回到正题,二维最长上升子序列......嗯,我会树套树。

考虑\(CDQ\)分治,算法流程:

  1. 先递归进入左子区间。

  2. 将左,右子区间按\(x\)排序。

  3. 归并处理左右子区间,在过程中使用树状数组加速\(DP\)。

  4. 还原右区间,清空树状数组。

  5. 递归进入右子区间。

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#define lowbit(x) ((x)&(-(x)))
#define rin(i,a,b) for(int i=(a);i<=(b);i++)
#define rec(i,a,b) for(int i=(a);i>=(b);i--)
#define trav(i,a) for(int i=head[(a)];i;i=e[i].nxt)
using std::cin;
using std::cout;
using std::endl;
typedef long long LL; inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*f;
} const int MAXN=100005;
int n,b[MAXN];
int siz;
int bit[MAXN];
struct Node{
int x,y,pos,f;
Node(){
f=1;
}
friend bool operator < (Node x,Node y){
return x.x==y.x?x.y<y.y:x.x<y.x;
}
}a[MAXN],c[MAXN]; inline void Add(int x,int kk){
for(int i=x;i<=siz;i+=lowbit(i)) bit[i]=(kk==-1?0:std::max(bit[i],kk));
} inline int Ask(int x){
int ret=0;
for(int i=x;i;i-=lowbit(i)) ret=std::max(ret,bit[i]);
return ret;
} inline bool cmp(Node x,Node y){
return x.pos<y.pos;
} void cdq(int l,int r){
if(l>=r) return;
int mid=((l+r)>>1);
cdq(l,mid);
std::sort(a+l,a+mid+1);
std::sort(a+mid+1,a+r+1);
int lptr=l;
rin(rptr,mid+1,r){
while(lptr<=mid&&a[lptr].x<a[rptr].x){
Add(a[lptr].y,a[lptr].f);
lptr++;
}
a[rptr].f=std::max(a[rptr].f,Ask(a[rptr].y-1)+1);
}
rin(i,l,lptr-1) Add(a[i].y,-1);
rin(i,mid+1,r) c[i]=a[i];
rin(i,mid+1,r) a[c[i].pos]=c[i];
// std::sort(a+mid+1,a+r+1,cmp);
cdq(mid+1,r);
} int main(){
n=read();
rin(i,1,n){
a[i].x=read();
b[i]=a[i].y=read();
a[i].pos=i;
}
std::sort(b+1,b+n+1);
siz=std::unique(b+1,b+n+1)-b-1;
rin(i,1,n) a[i].y=std::lower_bound(b+1,b+n+1,a[i].y)-b;
cdq(1,n);
int ans=1;
rin(i,1,n) ans=std::max(ans,a[i].f);
printf("%d\n",ans);
return 0;
}

[BZOJ2225][SPOJ2371]LIS2 - Another Longest Increasing Subsequence Problem:CDQ分治+树状数组+DP的更多相关文章

  1. SPOJ:Another Longest Increasing Subsequence Problem(CDQ分治求三维偏序)

    Given a sequence of N pairs of integers, find the length of the longest increasing subsequence of it ...

  2. SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治

    Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://a ...

  3. SPOJ LIS2 - Another Longest Increasing Subsequence Problem(CDQ分治优化DP)

    题目链接  LIS2 经典的三维偏序问题. 考虑$cdq$分治. 不过这题的顺序应该是 $cdq(l, mid)$ $solve(l, r)$ $cdq(mid+1, r)$ 因为有个$DP$. #i ...

  4. SPOJ - LIS2 Another Longest Increasing Subsequence Problem

    cdq分治,dp(i)表示以i为结尾的最长LIS,那么dp的递推是依赖于左边的. 因此在分治的时候需要利用左边的子问题来递推右边. (345ms? 区间树TLE /****************** ...

  5. 【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 ...

  6. HDU 5618:Jam's problem again(CDQ分治+树状数组处理三维偏序)

    http://acm.hdu.edu.cn/showproblem.php?pid=5618 题意:…… 思路:和NEUOJ那题一样的.重新写了遍理解了一下,算作处理三维偏序的模板了. #includ ...

  7. SPOJ Another Longest Increasing Subsequence Problem 三维最长链

    SPOJ Another Longest Increasing Subsequence Problem 传送门:https://www.spoj.com/problems/LIS2/en/ 题意: 给 ...

  8. BZOJ_2225_[Spoj 2371]Another Longest Increasing_CDQ 分治+树状数组

    BZOJ_2225_[Spoj 2371]Another Longest Increasing_CDQ 分治+树状数组 Description        给定N个数对(xi, yi),求最长上升子 ...

  9. hdu 3030 Increasing Speed Limits (离散化+树状数组+DP思想)

    Increasing Speed Limits Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

随机推荐

  1. 微PE.PE工具

    1.ZC:想要 干掉Win7x64的密码(没人用的机子,不知道密码,不想重装OS) 1.1.超详细微pe工具箱使用教程 _ 微pe工具箱怎么用.html(http://www.360doc.com/c ...

  2. JS刷新后回到页面顶部

    window.location.href = location.href; 方法一: $(window).scrollTop(0); 方法二:$('html ,body').animate({ scr ...

  3. 七、Zabbix-模板,应用集,监控项,触发器

    本篇内容,将模板,应用集,监控项,触发器放在一起,因为我们建立使用的监控项和触发器,大多数都是对多台机器使用的,很少有一个监控项对应一个主机的情况. 一.模板 1.什么是模板? 个人理解,模板就是模板 ...

  4. Dos - 学习总结(1)

    1.控制台复制 1>鼠标右键,标记. 2>选定复制内容后,鼠标右键,完成复制. 2.

  5. Linux0.11之初识Makefile/build.c

    前言 Makefile对于从来没有接触过的人来说是相当别扭的(比如我),但它确实又是非常重要的,它描述了一个Image是如何形成的,理解它也许并不能帮我解决实际问题,而且编写Makefile的工作也许 ...

  6. textarea标签输出内容时不能顶格(左对齐)输出

    我用textarea输出文本内容的时候出现下面的问题:文本内容在action里测试没有问题(文本内容前面没有空格),但是在jsp页面textare标签中输出文本内容时,文本内容前面却出现一些多余的空格 ...

  7. git的配置设置

    git的基本配置 git是一个版本控制工具,既然是工具,那么就可以根据人的个人喜好来进行设置,git也提供了配置,可以根据自己的喜好来对它进行个性化的设计,以让自己舒服的玩. git有三个配置文件 / ...

  8. maven项目出现红叉

    现象:maven项目出现红叉,同时项目中没出现代码错误 解决方案: 可尝试在本地maven项目pom.xml中添加如下: <build> <plugins> <plugi ...

  9. 关于cron4j的使用

    cron4j的主要实体是调度程序.使用it.sauronsoftware.cron4j.scheduler实例,您可以在一年中的固定时间执行任务.调度程序可以每分钟执行一次任务,每五分钟执行一次,星期 ...

  10. Nginx服务项的基本配置

    由于Nginx配置项较多,把他们按照用户使用时的预期功能分为以下4类: 1.调试,定位问题的配置项 2.正常运行必备配置项 3.优化性能配置项 4.事件类配置项 1. 用于调试进程,定位问题的配置项 ...