题目链接  LIS2

经典的三维偏序问题。

考虑$cdq$分治。

不过这题的顺序应该是

$cdq(l, mid)$

$solve(l, r)$

$cdq(mid+1, r)$

因为有个$DP$。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i)
#define MP make_pair
#define fi first
#define se second typedef long long LL; const int N = 1e5 + 10; struct node{
int x, y, z;
int num;
void scan() { scanf("%d%d", &y, &z);}
void print() { printf("%d %d %d\n", x, y, z);}
friend bool operator < (const node &a, const node &b){
return a.y == b.y ? a.z < b.z : a.y < b.y;
}
} p[N], q[N]; int a[N], b[N];
int n;
int cnt;
int c[N];
int ans; void update(int x, int val){
for (; x <= n; x += x & -x) c[x] = max(c[x], val);
} int query(int x){
int ret = 0;
for (; x ; x -= x & -x) ret = max(ret, c[x]);
return ret;
} void recover(int x){
for (; x <= n; x += x & -x) c[x] = 0;
} bool cmp(const node &a, const node &b){
return a.x < b.x;
} void cdq(int l, int r){
if (l == r) return;
int mid = (l + r) >> 1;
cdq(l, mid);
sort(p + l, p + mid + 1);
sort(p + mid + 1, p + r + 1);
int j = l;
for (int i = mid + 1; i <= r; ++i){
for (; j <= mid && p[j].y < p[i].y; ++j){
update(p[j].z, p[j].num);
} p[i].num = max(p[i].num, query(p[i].z - 1) + 1);
} rep(i, l, mid) recover(p[i].z);
sort(p + mid + 1, p + r + 1, cmp);
cdq(mid + 1, r); } int main(){ scanf("%d", &n);
rep(i, 1, n) p[i].scan();
rep(i, 1, n) a[i] = p[i].y;
rep(i, 1, n) p[i].num = 1; sort(a + 1, a + n + 1);
cnt = unique(a + 1, a + n + 1) - a - 1;
rep(i, 1, n) p[i].y = lower_bound(a + 1, a + cnt + 1, p[i].y) - a; rep(i, 1, n) a[i] = p[i].z;
sort(a + 1, a + n + 1);
cnt = unique(a + 1, a + n + 1) - a - 1;
rep(i, 1, n) p[i].z = lower_bound(a + 1, a + cnt + 1, p[i].z) - a; rep(i, 1, n) p[i].x = i; cdq(1, n);
ans = 0;
rep(i, 1, n) ans = max(ans, p[i].num);
printf("%d\n", ans);
return 0;
}

SPOJ LIS2 - Another Longest Increasing Subsequence Problem(CDQ分治优化DP)的更多相关文章

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

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

  2. 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 ...

  3. SPOJ - LIS2 Another Longest Increasing Subsequence Problem

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

  4. [BZOJ2225][SPOJ2371]LIS2 - Another Longest Increasing Subsequence Problem:CDQ分治+树状数组+DP

    分析 这回试了一下三级标题,不知道效果怎么样? 回到正题,二维最长上升子序列......嗯,我会树套树. 考虑\(CDQ\)分治,算法流程: 先递归进入左子区间. 将左,右子区间按\(x\)排序. 归 ...

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

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

  6. 洛谷 P4093 [HEOI2016/TJOI2016]序列 CDQ分治优化DP

    洛谷 P4093 [HEOI2016/TJOI2016]序列 CDQ分治优化DP 题目描述 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他. 玩具上有一个数列,数列中某些项的值可能会 ...

  7. luogu4093 序列 (cdq分治优化dp)

    设f[i]是以i位置为结尾的最长满足条件子序列的长度 那么j能转移到i的条件是,$j<i , max[j]<=a[i] , a[j]<=min[i]$,其中max和min表示这个位置 ...

  8. BZOJ1492 货币兑换 CDQ分治优化DP

    1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec  Memory Limit: 64 MB Description 小Y最近在一家金券交易所工作.该金券交易所只发行交 ...

  9. [LintCode] Longest Increasing Subsequence 最长递增子序列

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

随机推荐

  1. 【mysql】mysql存储过程实例

    ```mysql DELIMITER $$   DROP PROCEDURE IF EXISTS `system_number_update` $$   CREATE DEFINER=`root`@` ...

  2. GoF23种设计模式之行为型模式之备忘录模式

    一.概述         在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象的外部保存这个状态.以便以后可以将该对象恢复到原先保存的状态. 二.适用性 1.当需要保存一个对象在某个时刻的状态( ...

  3. XML映射文件中关系映射

    映射(多)对一.(一)对一的关联关系 1).使用列的别名 ①.若不关联数据表,则可以得到关联对象的id属性 ②.若还希望得到关联对象的其它属性.则必须关联其它的数据表 1.创建表: 员工表: DROP ...

  4. exp分析

    1 from pwn import* 2 3 local =1 4 debug = 1 5 6 if local: 7 p = process('./pwn1') 8 else: 9 p = remo ...

  5. 【LeetCode】Linked List Cycle II(环形链表 II)

    这是LeetCode里的第142道题. 题目要求: 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶:你是否可以不用额外空间解决此题? ...

  6. HDU 5525 Product 数论

    题意: 给出一个长度为\(n(1 \leq n \leq 10^5)\)的序列\(A_i\),\(N=\prod\limits_{i=1}^{n}i^{A_i}\).求\(N\)的所有约数的乘积. 分 ...

  7. python基础学习笔记—— 多继承

    本节主要内容: 1.python多继承 2.python经典类的MRO 3.python新式类的MRO.C3算法 4.super是什么鬼? 一.python多继承 在前⾯的学习过程中. 我们已经知道了 ...

  8. luogu2158 [SDOI2008]仪仗队 欧拉函数

    点 $ (i,j) $ 会看不见当有 $ k|i $ 且 $ k|j$ 时. 然后就成了求欧拉函数了. #include <iostream> #include <cstring&g ...

  9. C#中的扩展方法详解

    “扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型.”这是msdn上说的,也就是你可以对String,Int,DataRow,DataTable等这些类 ...

  10. Eclipse下创建Spring MVC web程序--maven版

    1. 创建一个maven工程: File->New->Other... 2. 创建完成后的结构如下: 3. 配置pom.xml文件,添加spring-webmvc依赖项   <pro ...