题意:

给定一个空序列

插入n个数(依次插入 1、2、3、4··n)

以下n个数表示i插在哪个位置。

每插入一个数后输出这个序列的lis

然后。。。

由于每次插入的数都是当前序列最大的数

所以不会影响后面的数的dp值

那么这个位置的dp值就是插入位置的前面最大dp值+1

然后输出这个序列最大的dp值。

==

思路:

splay。

。。

Q:为何这题须要用splay,不是简单的线段树吗

A: 由于我智商不够想不出线段树怎么写。。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
template <class T>
inline bool rd(T &ret) {
char c; int sgn;
if(c=getchar(),c==EOF) return 0;
while(c!='-'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
ret*=sgn;
return 1;
}
template <class T>
inline void pt(T x) {
if (x <0) {
putchar('-');
x = -x;
}
if(x>9) pt(x/10);
putchar(x%10+'0');
}
using namespace std;
inline int Mid(int a,int b){return (a+b)>>1;}
#define N 100010
#define L(x) tree[x].ch[0]
#define R(x) tree[x].ch[1]
#define Siz(x) tree[x].siz
#define Father(x) tree[x].fa
#define Max(x) tree[x].max
#define Val(x) tree[x].val
#define Pt(x) tree[x].pt()
struct node{
int ch[2], siz, fa;
int max, val;
void pt(){printf("val:%d max:%d siz:%d fa:%d{%d,%d}\n", val,max,siz,fa,ch[0],ch[1]);}
}tree[N*2];
int tot, root;
void Newnode(int &id, int val, int fa, int siz = 1){
id = ++tot;
L(id) = R(id) = 0;
Father(id) = fa;
Siz(id) = siz;
Max(id) = Val(id) = val;
} void push_up(int id){
Siz(id) = Siz(L(id)) + Siz(R(id)) +1;
Max(id) = max(Max(R(id)), Max(L(id)));
Max(id) = max(Val(id), Max(id));
}
void push_down(int id){} void Rotate(int id, int kind){
int y = Father(id);
push_down(y); push_down(id); //here
tree[y].ch[kind^1] = tree[id].ch[kind];
Father(tree[id].ch[kind]) = y;
if(Father(y))
tree[Father(y)].ch[R(Father(y))==y] = id;
Father(id) = Father(y);
Father(y) = id;
tree[id].ch[kind] = y;
push_up(y);
}
void splay(int id, int goal){
push_down(id);
while(Father(id) != goal){
int y = Father(id);
if(Father(y) == goal)
Rotate(id, L(y)==id);
else
{
int kind = L(Father(y)) == y;
if(tree[y].ch[kind] == id)
{
Rotate(id, kind^1);
Rotate(id, kind);
}
else
{
Rotate(y, kind);
Rotate(id,kind);
}
}
}
push_up(id);
if(goal == 0)root = id;
}
int Get_kth(int kth, int sor){//找到在sor后面的第k个数
push_down(sor);
int id = sor;
while(Siz(L(id)) != kth){
if(Siz(L(id)) > kth)
id = L(id);
else
{
kth -= (Siz(L(id))+1);
id = R(id);
}
push_down(id);
}
return id;
}
void init(){
Father(0) = L(0) = R(0) = Siz(0) = 0;
Max(0) = 0;
tot = 0;
Newnode(root, 0, 0);
Newnode(R(root), 0, root);
push_up(root);
}
void debug(int x){
printf("%d:\n", x);
Pt(x);
if(L(x))
debug(L(x));
if(R(x))
debug(R(x));
}
void insert(int pos){
splay(1, 0);
int u = Get_kth(pos, 1);
// if(pos == 2){cout<<"=="; debug(root);}
int v = Get_kth(pos+1, 1);
splay(u, 0);
splay(v, root);
// if(pos == 2){cout<<"=="; debug(root);}
Newnode(L(v), max(Val(root), Max(L(root))) +1, v);
push_up(v);
push_up(u);
// printf("[%d,%d]\n", u, v);
} int n;
int main() {
int T, Cas = 1; cin>>T;
while(T--){
rd(n);
init();
// debug(root);
printf("Case #%d:\n", Cas++);
for(int i = 1, m; i <= n; i++){
rd(m);
insert(m);
// printf("id:%d, pos:%d\n", i, m); debug(root);
pt(Max(root)); putchar('\n');
splay(tot, 0);
// puts("================");debug(root);
}/**/
puts("");
}
return 0;
}
/*
1
7
0 1 1 1 0 4 1 */

HDU 3564 Another LIS splay(水的更多相关文章

  1. HDU - 3564 Another LIS(LIS+线段树)

    http://acm.hdu.edu.cn/showproblem.php?pid=3564 题意 给出1~n的插入顺序,要求每次插入之后的LIS 分析 首先用线段树还原出最终序列.因为插入的顺序是按 ...

  2. Hdu 3564 Another LIS 线段树+LIS

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  3. HDU 5578 Friendship of Frog 水题

    Friendship of Frog Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.ph ...

  4. HDU 5590 ZYB's Biology 水题

    ZYB's Biology Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid ...

  5. HDU 5538 L - House Building 水题

    L - House Building Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.ph ...

  6. hdu 1890 Robotic Sort(splay 区间反转+删点)

    题目链接:hdu 1890 Robotic Sort 题意: 给你n个数,每次找到第i小的数的位置,然后输出这个位置,然后将这个位置前面的数翻转一下,然后删除这个数,这样执行n次. 题解: 典型的sp ...

  7. HDU 5842 Lweb and String 水题

    Lweb and String 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5842 Description Lweb has a string S ...

  8. HDU 4813 Hard Code(水题,2013年长春现场赛A题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4813 签到题. 把一个字符串按照格式输出就可以了,很水 #include <stdio.h> ...

  9. HDU 1890 - Robotic Sort - [splay][区间反转+删除根节点]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 Time Limit: 6000/2000 MS (Java/Others) Memory Li ...

随机推荐

  1. PHPSTORM杂技

    PHPSTORM技巧 去掉右上角弹出浏览器条 settings->web browsers->show browser popup in the editor前的钩去掉 让class fu ...

  2. kb-01-d<poj3279>--深搜变种,二进制优化;

    poj--3279 题意: 给n*m的矩阵,0 1组成,每次翻转一个格子可以将上下左右的五个节点翻转,求,把所有的格子翻转成0:输出每个个字的翻转次数:最少字数: 做法: 从上到下,第一行翻转的情况确 ...

  3. NOJ——1559Jump to the Top of Mountain(简单暴力DFS+渣渣代码)

    [1559] Jump to the Top of Mountain 时间限制: 1000 ms 内存限制: 65535 K 问题描述 Have you played a game named Min ...

  4. SpringBoot基础之MockMvc单元测试

    SpringBoot创建的Maven项目中,会默认添加spring-boot-starter-test依赖.在<5分钟快速上手SpringBoot>中编写的单元测试使用了MockMvc.本 ...

  5. poj 3293 Rectilinear polygon

    Rectilinear polygon Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2125   Accepted: 24 ...

  6. OpenStack 通用设计思路

    API 前端服务 每个 OpenStack 组件可能包含若干子服务,其中必定有一个 API 服务负责接收客户请求. 以 Nova 为例,nova-api 作为 Nova 组件对外的唯一窗口,向客户暴露 ...

  7. 【CF559C】 Gerald and Giant Chess(计数,方案数DP,数论)

    题意:给出一个棋盘为h*w,现在要从(1,1)到(h,w),其中有n个黑点不能走,问有多少种可能从左上到右下 (1 ≤ h, w ≤ 105, 1 ≤ n ≤ 2000),答案模10^9+7 思路:从 ...

  8. LOJ#2304. 「NOI2017」泳池

    $n \leq 1e9$底边长的泳池,好懒啊泥萌自己看题吧,$k \leq 1000$.答案对998244353取膜. 现在令$P$为安全,$Q$为危险的概率.刚好$K$是极其不好算的,于是来算$\l ...

  9. WebRTC VoiceEngine综合应用示例(二)——音频通话的基本流程(转)

    下面将以实现一个音频通话功能为示例详细介绍VoiceEngine的使用,在文末将附上相应源码的下载地址.这里参考的是voiceengine\voe_cmd_test. 第一步是创建VoiceEngin ...

  10. linux 下共享内存

    一.共享内存相关知识 所谓共享内存,就是多个进程间共同地使用同一段物理内存空间,它是通过将同一段物理内存映射到不同进程的 虚拟空间来实现的.由于映射到不同进程的虚拟空间中,不同进程可以直接使用,不需要 ...