# hdu4718

http://www.elijahqi.win/archives/839
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.

Sample Input
1
5
1 2 3 4 5
1 1 3 3
3
1 5
4 5
2 5

Sample Output
Case #1:
3
2
3

#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 110000
#define inf 0x7fffffff
using namespace std;
inline int read(){
int x=0;char ch=getchar();
while (ch<'0'||ch>'9') ch=getchar();
while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
return x;
}
struct node{
int y,next;
}data[N<<1];
struct node1{
int len,lmax,rmax,lmax1,rmax1,ln,rn,ans,ans1;
node1(){
len=lmax1=rmax1=lmax=rmax=ln=rn=ans=ans1=0;
}
};
struct node2{
int l,r,left,right,lmax,lmax1,rmax1,rmax,ans,ans1,ln,rn;
}tree[N<<2];
int size[N],id[N],fa[N],son[N],tp[N],a[N],dep[N],h[N],cnt,root,n,num,m,w[N];
void dfs1(int x){
size[x]=1;
for (int i=h[x];i;i=data[i].next){
int y=data[i].y;
if(fa[x]==y) continue;
dep[y]=dep[x]+1;fa[y]=x;dfs1(y);size[x]+=size[y];
if (size[y]>size[son[x]]) son[x]=y;
}
}
void dfs2(int x,int top){
id[x]=++cnt;tp[x]=top;w[cnt]=a[x];
if (son[x]) dfs2(son[x],top);
for (int i=h[x];i;i=data[i].next){
int y=data[i].y;
if (y==fa[x]||y==son[x]) continue;
dfs2(y,y);
}
}
inline void update(int x){
int l=tree[x].left,r=tree[x].right;int tmp=0,tmp1=0;
if (tree[l].rn<tree[r].ln) tmp=tree[l].rmax1+tree[r].lmax;
if (tree[l].rn>tree[r].ln) tmp1=tree[l].rmax+tree[r].lmax1;
tree[x].ans=max(tmp,max(tree[l].ans,tree[r].ans));
tree[x].ans1=max(tmp1,max(tree[l].ans1,tree[r].ans1));int len1=tree[l].r-tree[l].l+1,len2=tree[r].r-tree[r].l+1;
if(len1==tree[l].lmax&&tree[l].rn<tree[r].ln) tree[x].lmax=len1+tree[r].lmax;else tree[x].lmax=tree[l].lmax;
if (len1==tree[l].lmax1&&tree[l].rn>tree[r].ln) tree[x].lmax1=len1+tree[r].lmax1;else tree[x].lmax1=tree[l].lmax1;
if(len2==tree[r].rmax&&tree[l].rn>tree[r].ln) tree[x].rmax=len2+tree[l].rmax;else tree[x].rmax=tree[r].rmax;
if (len2==tree[r].rmax1&&tree[l].rn<tree[r].ln) tree[x].rmax1=len2+tree[l].rmax1;else tree[x].rmax1=tree[r].rmax1;
tree[x].ln=tree[l].ln;tree[x].rn=tree[r].rn;
}
void build(int &x,int l,int r){
x=++cnt;tree[x].l=l;tree[x].r=r;
if(l==r){
tree[x].ln=tree[x].rn=w[l];tree[x].lmax=tree[x].rmax=tree[x].lmax1=tree[x].rmax1=1;tree[x].left=tree[x].right=0;
tree[x].ans=tree[x].ans1=1;return;
}
int mid=l+r>>1;
build(tree[x].left,l,mid);build(tree[x].right,mid+1,r);
update(x);
}
inline node1 update1(node1 tmp1,node1 tmp2){
node1 ans1;int tmp=0,tmpp=0;
if (!tmp1.len) return tmp2;
if (!tmp2.len) return tmp1;
ans1.len=tmp1.len+tmp2.len;
if (tmp1.rn<tmp2.ln) tmp=tmp1.rmax1+tmp2.lmax;
if (tmp1.rn>tmp2.ln) tmpp=tmp1.rmax+tmp2.lmax1;
ans1.ans=max(tmp,max(tmp1.ans,tmp2.ans));
ans1.ans1=max(tmpp,max(tmp1.ans1,tmp2.ans1));
if(tmp1.len==tmp1.lmax&&tmp1.rn<tmp2.ln) ans1.lmax=tmp1.len+tmp2.lmax;else ans1.lmax=tmp1.lmax;
if (tmp1.len==tmp1.lmax1&&tmp1.rn>tmp2.ln) ans1.lmax1=tmp1.len+tmp2.lmax1;else ans1.lmax1=tmp1.lmax1;
if(tmp2.len==tmp2.rmax&&tmp1.rn>tmp2.ln) ans1.rmax=tmp2.len+tmp1.rmax;else ans1.rmax=tmp2.rmax;
if (tmp2.len==tmp2.rmax1&&tmp1.rn<tmp2.ln) ans1.rmax1=tmp2.len+tmp1.rmax1;else ans1.rmax1=tmp2.rmax1;
ans1.ln=tmp1.ln;ans1.rn=tmp2.rn;
return ans1;
}
node1 query(int rt,int l,int r){
if(l<=tree[rt].l&&r>=tree[rt].r){
node1 tmp;tmp.lmax=tree[rt].lmax;tmp.lmax1=tree[rt].lmax1;tmp.ans=tree[rt].ans;tmp.ans1=tree[rt].ans1;
tmp.rmax=tree[rt].rmax;tmp.rmax1=tree[rt].rmax1;tmp.ln=tree[rt].ln;tmp.rn=tree[rt].rn;
tmp.len=tree[rt].r-tree[rt].l+1;
return tmp;
}
int mid=(tree[rt].l+tree[rt].r)>>1;
node1 tmp1;node1 tmp2;
if (l<=mid) tmp1=query(tree[rt].left,l,r);
if (r>mid) tmp2=query(tree[rt].right,l,r);
return update1(tmp1,tmp2);
}
void solve(int x,int y){
node1 ansl,ansr;
while (tp[x]!=tp[y]){
if(dep[tp[x]]>dep[tp[y]]){
ansl=update1(query(root,id[tp[x]],id[x]),ansl);
x=fa[tp[x]];
}else{
ansr=update1(query(root,id[tp[y]],id[y]),ansr);
y=fa[tp[y]];
}
}
swap(ansl.ans,ansl.ans1);swap(ansl.ln,ansl.rn);swap(ansl.lmax,ansl.rmax);swap(ansl.lmax1,ansl.rmax1);
if (id[x]<id[y]){
ansl=update1(ansl,query(root,id[x],id[y]));
ansl=update1(ansl,ansr);
}else{
node1 tmpp=query(root,id[y],id[x]);
swap(tmpp.ans,tmpp.ans1);swap(tmpp.ln,tmpp.rn);swap(tmpp.lmax,tmpp.rmax);swap(tmpp.lmax1,tmpp.rmax1);
ansr=update1(tmpp,ansr);
ansl=update1(ansl,ansr);
}
printf("%d\n",ansl.ans);
//    printf("sdfsdf\n");
//print(root);
}
int main(){
freopen("hdu4718.in","r",stdin);
//freopen("hdu4718.out","w",stdout);
int T,tot=0;
T=read();
while (T--){
printf("Case #%d:\n",++tot);
n=read();memset(h,0,sizeof(h));memset(son,0,sizeof(son));
for (int i=1;i<=n;++i) a[i]=read();num=0;
for (int i=2;i<=n;++i){
int x=read();
data[++num].y=x;data[num].next=h[i];h[i]=num;
data[++num].y=i;data[num].next=h[x];h[x]=num;
}dep[1]=1;cnt=0;
dfs1(1);dfs2(1,1);
//for (int i=1;i<=n;++i) printf("%d ",id[i]);printf("\n");
cnt=0;
build(root,1,n);//print(root);
m=read();
for (int i=1;i<=m;++i){
int x=read(),y=read();
solve(x,y);
}
if(T)printf("\n");
}
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120