Problem Description
For a sequence S1, S2, ... , SN, and a pair of integers (i, j), if 1 <= i <= j <= N and Si < Si+1 < Si+2 < ... < Sj-1 < Sj , then the sequence Si, Si+1, ... , Sj is a CIS(Continuous Increasing Subsequence). The longest CIS of a sequence is called the LCIS (Longest Continuous Increasing Subsequence).
Now we consider a tree rooted at node 1. Nodes have values. We have Q queries, each with two nodes u and v. You have to find the shortest path from u to v. And write down each nodes' value on the path, from u to v, inclusive. Then you will get a sequence, and please show us the length of its LCIS.
 
Input
The first line has a number T (T <= 10) , indicating the number of test cases.
For each test case, the first line is a number N (N <= 105), the number of nodes in the tree.
The second line comes with N numbers v1, v2, v3 ... , vN, describing the value of node 1 to node N. (1 <= vi <= 109)
The third line comes with N - 1 numbers p2, p3, p4 ... , pN, describing the father nodes of node 2 to node N. Node 1 is the root and will have no father.
Then comes a number Q, it is the number of queries. (Q <= 105)
For next Q lines, each with two numbers u and v. As described above.
 
Output
For test case X, output "Case #X:" at the first line.
Then output Q lines, each with an answer to the query.
There should be a blank line *BETWEEN* each test case.
 
题目大意:给你一棵树,每个点上有一个权值,Q个询问,问u到v的最短路径上的点权都取出来排成一排,这一段的LCIS是多少(最长连续上升子序列)。
思路:还算是比较裸的树链剖分,每条链用一个线段树来维护,不过要同时维护太多的东西难度有点高……反正不会有修改操作,果断把从某个点开始的LCIS等等改成了预处理……反正就是相当的麻烦……手欠开了一条麻烦得要死的题目……
 
代码(2062MS):
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int MAXV = ;
const int MAXE = MAXV;
const int MAXT = MAXV << ; int lmaxi[MAXT], rmaxi[MAXT], mmaxi[MAXT];
int lmaxd[MAXT], rmaxd[MAXT], mmaxd[MAXT];
int val[MAXV], n, m, T; int inc_ord[MAXV], dec_ord[MAXV], inc_rev[MAXV], dec_rev[MAXV]; void init_inc_dec() {
for(int i = , t = ; i <= n; ++i) {
if(t < i) t = i;
while(t < n && val[t] < val[t + ]) ++t;
inc_ord[i] = t - i + ;
}
for(int i = , t = ; i <= n; ++i) {
if(t < i) t = i;
while(t < n && val[t] > val[t + ]) ++t;
dec_ord[i] = t - i + ;
}
for(int i = n, t = n; i > ; --i) {
if(t > i) t = i;
while(t > && val[t] < val[t - ]) --t;
inc_rev[i] = i - t + ;
}
for(int i = n, t = n; i > ; --i) {
if(t > i) t = i;
while(t > && val[t] > val[t - ]) --t;
dec_rev[i] = i - t + ;
}
} int queryI(int x, int l, int r, int a, int b) {
if(a <= l && r <= b) {
return mmaxi[x];
} else {
int ll = x << , rr = ll | , mid = (l + r) >> ;
int ans = ;
if(a <= mid) ans = max(ans, queryI(ll, l, mid, a, b));
if(mid < b) ans = max(ans, queryI(rr, mid + , r, a, b));
if(val[mid] < val[mid + ]) {
ans = max(ans, min(rmaxi[ll], mid - a + ) + min(lmaxi[rr], b - mid));
}
return ans;
}
} int queryD(int x, int l, int r, int a, int b) {
if(a <= l && r <= b) {
return mmaxd[x];
} else {
int ll = x << , rr = ll | , mid = (l + r) >> ;
int ans = ;
if(a <= mid) ans = max(ans, queryD(ll, l, mid, a, b));
if(mid < b) ans = max(ans, queryD(rr, mid + , r, a, b));
if(val[mid] > val[mid + ]) {
ans = max(ans, min(rmaxd[ll], mid - a + ) + min(lmaxd[rr], b - mid));
}
return ans;
}
} void maintainI(int x, int l, int r) {
int ll = x << , rr = ll | , mid = (l + r) >> ;
if(val[mid] < val[mid + ]) {
lmaxi[x] = lmaxi[ll] + (lmaxi[ll] == mid - l + ) * lmaxi[rr];
rmaxi[x] = rmaxi[rr] + (rmaxi[rr] == r - mid) * rmaxi[ll];
mmaxi[x] = max(rmaxi[ll] + lmaxi[rr], max(mmaxi[ll], mmaxi[rr]));
} else {
lmaxi[x] = lmaxi[ll];
rmaxi[x] = rmaxi[rr];
mmaxi[x] = max(mmaxi[ll], mmaxi[rr]);
}
} void maintainD(int x, int l, int r) {
int ll = x << , rr = ll | , mid = (l + r) >> ;
if(val[mid] > val[mid + ]) {
lmaxd[x] = lmaxd[ll] + (lmaxd[ll] == mid - l + ) * lmaxd[rr];
rmaxd[x] = rmaxd[rr] + (rmaxd[rr] == r - mid) * rmaxd[ll];
mmaxd[x] = max(rmaxd[ll] + lmaxd[rr], max(mmaxd[ll], mmaxd[rr]));
} else {
lmaxd[x] = lmaxd[ll];
rmaxd[x] = rmaxd[rr];
mmaxd[x] = max(mmaxd[ll], mmaxd[rr]);
}
} void build(int x, int l, int r) {
if(l == r) {
lmaxi[x] = rmaxi[x] = mmaxi[x] = ;
lmaxd[x] = rmaxd[x] = mmaxd[x] = ;
} else {
int ll = x << , rr = ll | , mid = (l + r) >> ;
build(ll, l, mid);
build(rr, mid + , r);
maintainI(x, l, r);
maintainD(x, l, r);
}
} int v[MAXV];
int fa[MAXV], son[MAXV], size[MAXV], tid[MAXV], top[MAXV], dep[MAXV];
int head[MAXV], ecnt, dfs_clock;
int to[MAXE], next[MAXE]; void init(int n) {
memset(head, -, (n + ) * sizeof(int));
ecnt = dfs_clock = ;
} void add_edge(int u, int v) {
to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
} void dfs_size(int u, int depth) {
size[u] = ; son[u] = ; dep[u] = depth;
int maxsize = ;
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
dfs_size(v, depth + );
size[u] += size[v];
if(size[v] > maxsize) {
son[u] = v;
maxsize = size[v];
}
}
} void dfs_heavy_edge(int u, int ancestor) {
val[tid[u] = ++dfs_clock] = v[u];
top[u] = ancestor;
if(son[u]) dfs_heavy_edge(son[u], ancestor);
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
if(v == son[u]) continue;
dfs_heavy_edge(v, v);
}
} int query(int x, int y) {
int res = , maxx = , prex = , maxy = , prey = ;
while(top[x] != top[y]) {
if(dep[top[x]] > dep[top[y]]) {
int sz = dep[x] - dep[top[x]] + ;
int up_ans = min(inc_rev[tid[x]], sz);
int down_ans = min(dec_ord[tid[top[x]]], sz);
res = max(res, queryD(, , n, tid[top[x]], tid[x]));
if(prex && v[prex] >= v[x]) maxx = ;
res = max(res, up_ans + maxx);
maxx = down_ans + (sz == down_ans) * maxx;
prex = top[x];
x = fa[top[x]];
} else {
int sz = dep[y] - dep[top[y]] + ;
int up_ans = min(dec_rev[tid[y]], sz);
int down_ans = min(inc_ord[tid[top[y]]], sz);
res = max(res, queryI(, , n, tid[top[y]], tid[y]));
if(prey && v[prey] <= v[y]) maxy = ;
res = max(res, up_ans + maxy);
maxy = down_ans + (sz == down_ans) * maxy;
prey = top[y];
y = fa[top[y]];
}
}
if(dep[x] > dep[y]) {
int sz = dep[x] - dep[y] + ;
int up_ans = min(inc_rev[tid[x]], sz);
int down_ans = min(dec_ord[tid[y]], sz);
res = max(res, queryD(, , n, tid[y], tid[x]));
if(prex && v[prex] >= v[x]) maxx = ;
if(prey && v[prey] <= v[y]) maxy = ;
res = max(res, up_ans + maxx);
res = max(res, down_ans + maxy);
if(up_ans == sz) res = max(res, maxx + up_ans + maxy);
} else {
int sz = dep[y] - dep[x] + ;
int up_ans = min(dec_rev[tid[y]], sz);
int down_ans = min(inc_ord[tid[x]], sz);
res = max(res, queryI(, , n, tid[x], tid[y]));
if(prex && v[prex] >= v[x]) maxx = ;
if(prey && v[prey] <= v[y]) maxy = ;
res = max(res, down_ans + maxx);
res = max(res, up_ans + maxy);
if(up_ans == sz) res = max(res, maxx + up_ans + maxy);
}
return res;
} int main() {
scanf("%d", &T);
for(int t = ; t <= T; ++t) {
scanf("%d", &n);
init(n);
for(int i = ; i <= n; ++i) scanf("%d", &v[i]);
for(int i = ; i <= n; ++i) {
scanf("%d", &fa[i]);
add_edge(fa[i], i);
}
dfs_size(, );
dfs_heavy_edge(, );
build(, , n);
init_inc_dec();
printf("Case #%d:\n", t);
scanf("%d", &m);
while(m--) {
int u, v;
scanf("%d%d", &u, &v);
printf("%d\n", query(u, v));
}
if(t != T) puts("");
}
}

HDU 4718 The LCIS on the Tree(树链剖分)的更多相关文章

  1. hdu_4718_The LCIS on the Tree(树链剖分+线段树合并)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4718 题意:给你一棵树,每个节点有一个值,然后任给树上的两点,问这两点的最长连续递增区间是多少 题解: ...

  2. HDU 5614 Baby Ming and Matrix tree 树链剖分

    题意: 给出一棵树,每个顶点上有个\(2 \times 2\)的矩阵,矩阵有两种操作: 顺时针旋转90°,花费是2 将一种矩阵替换为另一种矩阵,花费是10 树上有一种操作,将一条路经上的所有矩阵都变为 ...

  3. Hdu 5274 Dylans loves tree (树链剖分模板)

    Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...

  4. POJ3237 Tree 树链剖分 边权

    POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...

  5. HDU 5044 Tree --树链剖分

    题意:给一棵树,两种操作: ADD1: 给u-v路径上所有点加上值k, ADD2:给u-v路径上所有边加上k,初始值都为0,问最后每个点和每条边的值,输出. 解法:树链剖分可做,剖出来如果直接用线段树 ...

  6. HDU 3966:Aragorn's Story(树链剖分)

    http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:有n个点n-1条边,每个点有一个权值,有两种操作:询问一个点上权值是多少和修改u到v这条链上的权值. ...

  7. HDU 5840 This world need more Zhu 树链剖分+暴力

    This world need more Zhu 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5840 Description As we all ...

  8. Query on a tree——树链剖分整理

    树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...

  9. 【BZOJ-4353】Play with tree 树链剖分

    4353: Play with tree Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 31  Solved: 19[Submit][Status][ ...

随机推荐

  1. bootstrap响应式实用工具

  2. 在serviceImpl里使用自身的方法

    @Service("tbLeaveRegisterService")@Transactionalpublic class TbLeaveRegisterServiceImpl ex ...

  3. javascript的alert()的消息框不弹出或者弹出信息有误

    有时不知道什么,有时javascript的alert()的消息框不弹出或者弹出信息有误,代码是这么写的: //提示信息 public static void alert(TemplateControl ...

  4. 蓝牙4.0的LM层说明

    1.概念 The Link Manager Protocol (LMP) is used to control and negotiate all aspects of the operation o ...

  5. java项目——数据结构实验报告

    java项目——数据结构总结报告 20135315  宋宸宁 实验要求 1.用java语言实现数据结构中的线性表.哈希表.树.图.队列.堆栈.排序查找算法的类. 2.设计集合框架,使用泛型实现各类. ...

  6. HBase的数据迁移(含HDFS的数据迁移)

    1.启动两个HDFS集群 hadoop0,hadoop1,都是伪分布式的集群 2.启动hadoop3的zookeeper与hbase 注意点:需要开启yarn服务,因为distcp需要yarn. 3. ...

  7. Qt 无边框窗体改变大小 完美实现(全部自己实现)

    近期,做项目用到无边框窗体,令人蛋疼的是无边框窗体大小的改变要像右边框那样,上下左右四周,而且要流畅. 网上也找了些代码,发现居然还要连接到windows事件,这显然不合常理,后来自己新建了demo, ...

  8. Object的属性property详细解释(自动生成成员变量)

    类Class中的属性property: 在ios第一版中,我们为输出口同时声明了属性和底层实例变量,那时,属性是oc语言的一个新的机制,并且要求你必须声明与之对应的实例变量,例如: @interfac ...

  9. 常用yum命令

    yum list 查询所有可用软件包 yum search 关键字 查询和关键字相关的包 yum -y install 包名 加上-y自动回答yes yum -y update 包名 升级  yum ...

  10. jQuery获取自身HTML

    <html><head> <title>jQuery获取自身HTML</title> <meta http-equiv="Content ...