HDU 4718 The LCIS on the Tree(树链剖分)

时间:2023-03-08 17:30:28
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("");
}
}